| 1 | package Broker::Async::Worker; | ||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Scalar::Util qw( blessed weaken ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Broker::Async::Worker | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Used by L<Broker::Async> for tracking the state of asynchronous work. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | our $VERSION = "0.0.5"; # __VERSION__ | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head2 code | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | The code reference used to start the work. | ||||||||||||||||||||||||||||
| 24 | This will be invoked with the arguments passed to C<do>. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Must return a L<Future> subclass. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 concurrency | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | The number of concurrent tasks a worker can execute. | ||||||||||||||||||||||||||||
| 31 | Do'ing more tasks than this limit is a fatal error. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Defaults to 1. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | use Class::Tiny qw( code ), { | ||||||||||||||||||||||||||||
| 38 | 17 | ▶ | concurrency => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 19 | ▶ | futures => sub { +{} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 19 | ▶ | available => sub { shift->concurrency }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 7 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 METHODS | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 new | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | my $worker = Broker::Async::Worker->new( | ||||||||||||||||||||||||||||
| 48 | code => sub { ... }, | ||||||||||||||||||||||||||||
| 49 | concurrency => $max, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head2 available | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Indicates whether the worker is available to C<do> tasks. | ||||||||||||||||||||||||||||
| 55 | It is a fatal error to invoke C<do> when this is false. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head2 do | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | my $future = $worker->do($task); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Invokes the code attribute with the given arguments. | ||||||||||||||||||||||||||||
| 62 | Returns a future that will be resolved when the work is done. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub active { | ||||||||||||||||||||||||||||
| 67 | 1987 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
active: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 1987 | return values %{ $self->futures }; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub BUILD { | ||||||||||||||||||||||||||||
| 72 | 26 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 26 | for my $name (qw( code )) { | |||||||||||||||||||||||||||
| 74 | 26 | ▶ | croak "$name attribute required" unless defined $self->$name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub do { | ||||||||||||||||||||||||||||
| 79 | 124 | ▶ | weaken(my $self = shift); | ||||||||||||||||||||||||||
|
Subroutine
do: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 124 | my (@args) = @_; | |||||||||||||||||||||||||||
| 81 | 124 | ▶ | if (not( $self->available )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 1 | croak "worker $self is not available for work"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 123 | my $f = $self->code->(@args); | |||||||||||||||||||||||||||
| 86 | 123 | ▶ | if (not( blessed($f) and $f->isa('Future') )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($f) and $f->isa("Future")
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f)
$f->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
| |||||||||||||||||||||||||||||
| 87 | 0 | croak "code for worker $self did not return a Future: returned $f"; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 123 | $self->available( $self->available - 1 ); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | return $self->futures->{$f} = $f->on_ready(sub{ | ||||||||||||||||||||||||||||
| 92 | 118 | ▶ | delete $self->futures->{$f}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 118 | $self->available( $self->available + 1 ); | |||||||||||||||||||||||||||
| 94 | 123 | }); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Mark Flickinger E<lt>maf@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | This software is licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 7 | 1; | |||||||||||||||||||||||||||