File Coverage

blib/lib/Job/Async.pm
Criterion Covered Total %
statement 27 27 100.0
branch 4 8 50.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 40 44 90.9


line stmt bran cond sub pod time code
1             package Job::Async;
2             # ABSTRACT: Asynchronous job queue for IO::Async
3              
4 1     1   7 use strict;
  1         1  
  1         26  
5 1     1   4 use warnings;
  1         2  
  1         30  
6              
7 1     1   6 use parent qw(IO::Async::Notifier);
  1         1  
  1         5  
8              
9             our $VERSION = '0.004';
10              
11             =head1 NAME
12              
13             Job::Async - L abstraction for dispatching tasks to workers and receiving results
14              
15             =head1 DESCRIPTION
16              
17             More API details are in the respective base classes:
18              
19             =over 4
20              
21             =item * L - queues jobs for workers to process
22              
23             =item * L - handles the job processing part
24              
25             =back
26              
27             Normally, clients and workers would be in separate processes, probably distributed across
28             multiple servers.
29              
30             =cut
31              
32 1     1   379 use Job::Async::Utils;
  1         2  
  1         29  
33 1     1   388 use Module::Load ();
  1         885  
  1         309  
34              
35             =head2 worker
36              
37             Attaches a L instance as a child of this manager object,
38             and returns the new worker instance.
39              
40             Takes two parameters:
41              
42             =over 4
43              
44             =item * C<$type> - used to select the worker class, e.g. C or C
45              
46             =item * C<$cfg> - the configuration parameters to pass to the new worker, as a hashref
47              
48             =back
49              
50             Example:
51              
52             my $worker = $jobman->worker(
53             redis => { uri => 'redis://server', mode => 'reliable' }
54             );
55             $worker->start;
56             $worker->jobs->each(sub { $_->done($_->data('x') . $_->data('y')) });
57             $worker->trigger;
58              
59             =cut
60              
61             sub worker {
62 1     1 1 3 my ($self, $type, $cfg) = @_;
63 1 50       7 die 'need a type' unless $type =~ /^[\w:]+$/;
64 1         5 my $class = 'Job::Async::Worker::' . ucfirst $type;
65 1 50       12 Module::Load::load($class) unless $class->can('new');
66 1         30 $self->add_child(
67             my $worker = $class->new(%$cfg)
68             );
69 1         150 $worker
70             }
71              
72             =head2 client
73              
74             Attaches a L instance as a child of this manager object,
75             and returns the new client instance.
76              
77             Takes two parameters:
78              
79             =over 4
80              
81             =item * C<$type> - used to select the worker class, e.g. C or C
82              
83             =item * C<$cfg> - the configuration parameters to pass to the new worker, as a hashref
84              
85             =back
86              
87             Example:
88              
89             print "Job result was " . $jobman->client(
90             redis => { uri => 'redis://server', mode => 'reliable' }
91             )->submit(
92             x => 123,
93             y => 456
94             )->get;
95              
96             =cut
97              
98             sub client {
99 1     1 1 4 my ($self, $type, $cfg) = @_;
100 1 50       8 die 'need a type' unless $type =~ /^[\w:]+$/;
101 1         5 my $class = 'Job::Async::Client::' . ucfirst $type;
102 1 50       15 Module::Load::load($class) unless $class->can('new');
103 1         17 $self->add_child(
104             my $client = $class->new(%$cfg)
105             );
106 1         96 $client
107             }
108              
109             1;
110              
111             =head1 SEE ALSO
112              
113             The main feature missing from the other alternatives is job completion notification - seems that
114             "fire and forget" is a popular model.
115              
116             =over 4
117              
118             =item * L - venerable contender for background job handling, usually database-backed
119              
120             =item * L - reliable job queuing, database-backed again
121              
122             =item * L - integrates with L, normally seems to be used with a PostgreSQL
123             backend. Has some useful routing and admin features. Does have some support for notification -
124             see L for example - but at the time of writing this came with significant
125             overhead.
126              
127             =item * L - a curious hybrid of L and L, using
128             pub/sub and a race on C calls to handle multiple instances possibly trying to queue
129             the same job at once.
130              
131             =item * L
132              
133             =item * L
134              
135             =item * L
136              
137             =item * L
138              
139             =item * L
140              
141             =item * L
142              
143             =item * L
144              
145             =back
146              
147             =head1 AUTHOR
148              
149             Tom Molesworth C<< >>
150              
151             =head1 LICENSE
152              
153             Copyright Tom Molesworth 2015-2017. Licensed under the same terms as Perl itself.
154