File Coverage

lib/MooseX/Workers/Job.pm
Criterion Covered Total %
statement 2 4 50.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 4 6 66.6


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