File Coverage

lib/MooseX/Workers/Job.pm
Criterion Covered Total %
statement 15 20 75.0
branch 0 2 0.0
condition 0 6 0.0
subroutine 5 7 71.4
pod 1 1 100.0
total 21 36 58.3


line stmt bran cond sub pod time code
1             package MooseX::Workers::Job;
2             our $AUTHORITY = 'cpan:PERIGRIN';
3             $MooseX::Workers::Job::VERSION = '0.24';
4 17     17   60089 use Moose;
  17         1113080  
  17         156  
5 17     17   89444 use overload ();
  17         34  
  17         295  
6 17     17   68 use Scalar::Util 'reftype';
  17         24  
  17         1455  
7              
8             use overload
9 0     0   0 '""' => sub { $_[0]->ID }, # stringify to wheel id for backcompat
10 17         167 fallback => 1
11 17     17   76 ;
  17         20  
12              
13             has 'ID' => ( is => 'rw', isa => 'Int' ); # POE::Wheel::Run->ID
14             has 'PID' => ( is => 'rw', isa => 'Int' ); # POE::Wheel::Run->PID
15             has 'name' => ( is => 'rw', isa => 'Str' );
16             has 'command' => ( is => 'rw' ); # See POE::Wheel::Run POD (Program)
17             has 'args' => ( is => 'rw', isa => 'ArrayRef' ); # See POE::Wheel::Run POD (ProgramArgs)
18             has 'timeout' => ( is => 'rw', isa => 'Int' ); # abort after this many seconds
19              
20             sub is_coderef {
21 0     0 1   my $self = shift;
22              
23 0           my $cmd = $self->command;
24              
25 0 0 0       return 1 if ref $cmd && (reftype $cmd eq 'CODE' || overload::Method($cmd, '&{}'));
      0        
26              
27 0           return undef;
28             }
29              
30             __PACKAGE__->meta->make_immutable;
31              
32 17     17   3178 no Moose;
  17         25  
  17         77  
33              
34             =head1 NAME
35              
36             MooseX::Workers::Job - One of the jobs MooseX::Workers is running
37              
38             =head1 SYNOPSIS
39              
40             package Manager;
41             use Moose;
42             with qw(MooseX::Workers);
43              
44             sub worker_stdout {
45             my ( $self, $output, $job ) = @_;
46             printf(
47             "%s(%s,%s) said '%s'\n",
48             $job->name, $job->ID, $job->PID, $output
49             );
50             }
51             sub run {
52             foreach (qw( foo bar baz )) {
53             my $job = MooseX::Workers::Job->new(
54             name => $_,
55             command => sub { print "Hello World\n" },
56             timeout => 30,
57             );
58             $_[0]->spawn( $job );
59             }
60             POE::Kernel->run();
61             }
62             no Moose;
63              
64             Manager->new()->run();
65             # bar(2,32423) said 'Hello World'
66             # foo(1,32422) said 'Hello World'
67             # baz(3,32424) said 'Hello World'
68              
69             =head1 DESCRIPTION
70              
71             MooseX::Workers::Job objects are convenient if you want to name each
72             L<MooseX::Workers> job, or if you want them to timeout (abort)
73             after a certain number of seconds.
74              
75             =head1 METHODS
76              
77             The accessors, as well as:
78              
79             =head2 is_coderef
80              
81             Returns true if the command is some type of coderef, undef otherwise.
82              
83             =cut
84              
85             1;
86