Minion/Worker.pm

filter: Statementstmt 12.9% 15 / 116 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 67 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 18.1% 4 / 22 Podpod 100% 8 / 8 totaltotal 9.3% 27 / 289 CC 50 SCAR 52.2
CC
50
Cov
7%
_work
420.0
run
272.0
_kill
30.0
CRAP 185.0
« Minion/Command/minion/job.pm ↑ Summary Minion/Command/minion/schedule.pm »
1 package Minion::Worker;
2 2 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Minion::Util qw(desired_tasks);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::Util qw(steady_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶has [qw(commands status)] => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
9 2 has [qw(id minion)];
10  
11 0 ▶sub add_command { $_[0]->commands->{$_[1]} = $_[2] and return $_[0] }
Subroutine add_command: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->commands->{$_[1]} = $_[2] and return $_[0] 0 0 0
12  
13 0 ▶sub dequeue {
Subroutine dequeue: not called CC=3 SCAR=24.8
Pod documented
14 0 my ($self, $wait, $options) = @_;
15  
16 # Worker not registered
17 0 ▶ return undef unless my $id = $self->id;
Branch: 1 branch
BranchTrueFalseTotal
unless my $id = $self->id 0 0 0
18  
19 0 my $minion = $self->minion;
20 0 ▶ return undef unless my $job = $minion->backend->dequeue($id, $wait, $options);
Branch: 1 branch
BranchTrueFalseTotal
unless my $job = $minion->backend->dequeue($id, $wait, $options) 0 0 0
21 $job = $minion->class_for_task($job->{task})
22 0 ->new(args => $job->{args}, id => $job->{id}, minion => $minion, retries => $job->{retries}, task => $job->{task});
23 0 $self->emit(dequeue => $job);
24 0 return $job;
25 }
26  
27 0 ▶sub info { $_[0]->minion->backend->list_workers(0, 1, {ids => [$_[0]->id]})->{workers}[0] }
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
28  
29 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
30 0 my $self = shift->SUPER::new(@_);
31 0 ▶ $self->on(busy => sub { sleep 1 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 return $self;
33 }
34  
35 0 ▶sub process_commands {
Subroutine process_commands: not called CC=4 SCAR=30.0
Pod documented
36 0 my $self = shift;
37  
38 0 my $called = 0;
39 0 for my $command (@{$self->minion->backend->receive($self->id)}) {
40 0 ▶ next unless my $cb = $self->commands->{shift @$command};
Branch: 1 branch
BranchTrueFalseTotal
unless my $cb = $self->commands->{shift @$command} 0 0 0
41 0 $self->$cb(@$command);
42 0 $called++;
43 }
44 0 ▶ $self->register if $called;
Branch: 1 branch
BranchTrueFalseTotal
if $called 0 0 0
45  
46 0 return $self;
47 }
48  
49 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
50 0 my $self = shift;
51 0 my $status = {status => $self->status};
52 0 return $self->id($self->minion->backend->register_worker($self->id, $status));
53 }
54  
55 0 ▶sub run {
Subroutine run: not called CC=16 SCAR=56.1
Pod documented
56 0 my $self = shift;
57  
58 0 my $status = $self->status;
59 0 ▶ $status->{command_interval} //= 10;
Condition0%0/2
$status->{"command_interval"} //= 10
l!l
00
MC/DC0%0/1
$status->{"command_interval"} //= 10
$status->{"command_interval"}
Truth table0%0/2
A
A= $status->{"command_interval"}
Aresult
11
00
60 0 ▶ $status->{dequeue_timeout} //= 5;
Condition0%0/2
$status->{"dequeue_timeout"} //= 5
l!l
00
MC/DC0%0/1
$status->{"dequeue_timeout"} //= 5
$status->{"dequeue_timeout"}
Truth table0%0/2
A
A= $status->{"dequeue_timeout"}
Aresult
11
00
61 0 ▶ $status->{dispatch_interval} //= 30;
Condition0%0/2
$status->{"dispatch_interval"} //= 30
l!l
00
MC/DC0%0/1
$status->{"dispatch_interval"} //= 30
$status->{"dispatch_interval"}
Truth table0%0/2
A
A= $status->{"dispatch_interval"}
Aresult
11
00
62 0 ▶ $status->{heartbeat_interval} //= 300;
Condition0%0/2
$status->{"heartbeat_interval"} //= 300
l!l
00
MC/DC0%0/1
$status->{"heartbeat_interval"} //= 300
$status->{"heartbeat_interval"}
Truth table0%0/2
A
A= $status->{"heartbeat_interval"}
Aresult
11
00
63 0 ▶ $status->{jobs} //= 4;
Condition0%0/2
$status->{"jobs"} //= 4
l!l
00
MC/DC0%0/1
$status->{"jobs"} //= 4
$status->{"jobs"}
Truth table0%0/2
A
A= $status->{"jobs"}
Aresult
11
00
64 0 ▶ $status->{limits} //= {};
Condition0%0/2
$status->{"limits"} //= {}
l!l
00
MC/DC0%0/1
$status->{"limits"} //= {}
$status->{"limits"}
Truth table0%0/2
A
A= $status->{"limits"}
Aresult
11
00
65 0 ▶ $status->{queues} ||= ['default'];
Condition0%0/2
$status->{"queues"} ||= ["default"]
l!l
00
MC/DC0%0/1
$status->{"queues"} ||= ["default"]
$status->{"queues"}
Truth table0%0/2
A
A= $status->{"queues"}
Aresult
11
00
66 0 ▶ $status->{performed} //= 0;
Condition0%0/2
$status->{"performed"} //= 0
l!l
00
MC/DC0%0/1
$status->{"performed"} //= 0
$status->{"performed"}
Truth table0%0/2
A
A= $status->{"performed"}
Aresult
11
00
67 0 ▶ $status->{repair_interval} //= 21600;
Condition0%0/2
$status->{"repair_interval"} //= 21600
l!l
00
MC/DC0%0/1
$status->{"repair_interval"} //= 21600
$status->{"repair_interval"}
Truth table0%0/2
A
A= $status->{"repair_interval"}
Aresult
11
00
68 0 $status->{repair_interval} -= int rand $status->{repair_interval} / 2;
69 0 ▶ $status->{spare} //= 1;
Condition0%0/2
$status->{"spare"} //= 1
l!l
00
MC/DC0%0/1
$status->{"spare"} //= 1
$status->{"spare"}
Truth table0%0/2
A
A= $status->{"spare"}
Aresult
11
00
70 0 ▶ $status->{spare_min_priority} //= 1;
Condition0%0/2
$status->{"spare_min_priority"} //= 1
l!l
00
MC/DC0%0/1
$status->{"spare_min_priority"} //= 1
$status->{"spare_min_priority"}
Truth table0%0/2
A
A= $status->{"spare_min_priority"}
Aresult
11
00
71 0 ▶ $status->{type} //= 'Perl';
Condition0%0/2
$status->{"type"} //= "Perl"
l!l
00
MC/DC0%0/1
$status->{"type"} //= "Perl"
$status->{"type"}
Truth table0%0/2
A
A= $status->{"type"}
Aresult
11
00
72  
73 # Reset event loop
74 0 Mojo::IOLoop->reset;
75 0 ▶ local $SIG{CHLD} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 ▶ local $SIG{INT} = local $SIG{TERM} = sub { $self->{finished}++ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 ▶ local $SIG{QUIT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
78 0 ▶ ++$self->{finished} and kill 'KILL', map { $_->pid } @{$self->{jobs}};
Branch: 1 branch
BranchTrueFalseTotal
++$self->{"finished"} and kill "KILL", map({$_->pid;} @{$$self{"jobs"};}) 0 0 0
79 0 };
80  
81 # Remote control commands need to validate arguments carefully
82 0 my $commands = $self->commands;
83 0 ▶ local $commands->{jobs} = sub { $status->{jobs} = $_[1] if ($_[1] // '') =~ /^\d+$/ };
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] // "") =~ /^\d+$/u 0 0 0
Condition0%0/2
$_[1] // ""
l!l
00
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
84 0 local $commands->{kill} = \&_kill;
85 0 ▶ local $commands->{spare} = sub { $status->{spare} = $_[1] if ($_[1] // '') =~ /^\d+$/ };
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] // "") =~ /^\d+$/u 0 0 0
Condition0%0/2
$_[1] // ""
l!l
00
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
86 0 ▶ local $commands->{stop} = sub { $self->_kill('KILL', $_[1]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
87  
88 0 ▶ eval { $self->_work until $self->{finished} && !@{$self->{jobs}} };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"finished"} and not @{$self->{"jobs"};} or do { $self->_work } 0 0 0
Condition0%0/3
$self->{"finished"} and not @{$self->{"jobs"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"finished"} and not @{$self->{"jobs"};}
$self->{"finished"} not @{$self->{"jobs"};}
Truth table0%0/3
A and not(B)
A= $self->{"finished"}B= not @{$self->{"jobs"};}
ABresult
0X0
100
111
89 0 my $err = $@;
90 0 $self->unregister;
91 0 ▶ croak $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
92 }
93  
94 0 ▶sub unregister {
Subroutine unregister: not called CC=1 SCAR=6.9
Pod documented
95 0 my $self = shift;
96 0 $self->minion->backend->unregister_worker(delete $self->{id});
97 0 return $self;
98 }
99  
100 0 ▶sub _kill {
Subroutine _kill: not called CC=5 SCAR=34.0
101 0 ▶ my ($self, $signal, $id) = (shift, shift // '', shift // '');
Condition0%0/4
shift() // ""
l!l
00
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
Truth table0%0/2
A
A= shift()
Aresult
11
00
102 0 ▶ return unless grep { $signal eq $_ } qw(INT TERM KILL USR1 USR2);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$signal eq $_;} "INT", "TERM", "KILL", "USR1", "USR2" 0 0 0
103 0 $_->kill($signal) for grep { $_->id eq $id } @{$self->{jobs}};
104 }
105  
106 0 ▶sub _work {
Subroutine _work: not called CC=20 SCAR=60.4
107 0 my $self = shift;
108  
109 # Send heartbeats in regular intervals
110 0 my $status = $self->status;
111 0 ▶ $self->{last_heartbeat} ||= -$status->{heartbeat_interval};
Condition0%0/3
$self->{"last_heartbeat"} ||= -$status->{"heartbeat_interval"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"last_heartbeat"} ||= -$status->{"heartbeat_interval"}
$self->{"last_heartbeat"} -$status->{"heartbeat_interval"}
Truth table0%0/3
A ||= B
A= $self->{"last_heartbeat"}B= -$status->{"heartbeat_interval"}
ABresult
1X1
011
000
112 $self->register and $self->{last_heartbeat} = steady_time
113 0 ▶ if ($self->{last_heartbeat} + $status->{heartbeat_interval}) < steady_time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_heartbeat"} + $status->{"heartbeat_interval"} < &steady_time() 0 0 0
Condition0%0/6
$self->{'last_heartbeat'} + $status->{'heartbeat_interval'} < &steady_time() && ($self->register && ($self->{'last_heartbeat'} = &steady_time()))
!ll&&!rl&&r
000
$self->register and $self->{"last_heartbeat"} = &steady_time()
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{'last_heartbeat'} + $status->{'heartbeat_interval'} < &steady_time() && ($self->register && ($self->{'last_heartbeat'} = &steady_time()))
$self->{'last_heartbeat'} + $status->{'heartbeat_interval'} < &steady_time() $self->register $self->{"last_heartbeat"} = &steady_time()
Truth table0%0/4
A && B and C
A= $self->{'last_heartbeat'} + $status->{'heartbeat_interval'} < &steady_time()B= $self->registerC= $self->{"last_heartbeat"} = &steady_time()
ABCresult
0XX0
10X0
1100
1111
114  
115 # Process worker remote control commands in regular intervals
116 0 ▶ $self->{last_command} ||= 0;
Condition0%0/2
$self->{"last_command"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"last_command"} ||= 0
$self->{"last_command"}
Truth table0%0/2
A
A= $self->{"last_command"}
Aresult
11
00
117 $self->process_commands and $self->{last_command} = steady_time
118 0 ▶ if ($self->{last_command} + $status->{command_interval}) < steady_time;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_command"} + $status->{"command_interval"} < &steady_time() 0 0 0
Condition0%0/6
$self->{'last_command'} + $status->{'command_interval'} < &steady_time() && ($self->process_commands && ($self->{'last_command'} = &steady_time()))
!ll&&!rl&&r
000
$self->process_commands and $self->{"last_command"} = &steady_time()
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{'last_command'} + $status->{'command_interval'} < &steady_time() && ($self->process_commands && ($self->{'last_command'} = &steady_time()))
$self->{'last_command'} + $status->{'command_interval'} < &steady_time() $self->process_commands $self->{"last_command"} = &steady_time()
Truth table0%0/4
A && B and C
A= $self->{'last_command'} + $status->{'command_interval'} < &steady_time()B= $self->process_commandsC= $self->{"last_command"} = &steady_time()
ABCresult
0XX0
10X0
1100
1111
119  
120 # Repair in regular intervals (randomize to avoid congestion)
121 0 ▶ $self->{last_repair} ||= 0;
Condition0%0/2
$self->{"last_repair"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"last_repair"} ||= 0
$self->{"last_repair"}
Truth table0%0/2
A
A= $self->{"last_repair"}
Aresult
11
00
122 0 ▶ if (($self->{last_repair} + $status->{repair_interval}) < steady_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"last_repair"} + $status->{"repair_interval"} < &steady_time()) 0 0 0
123 0 $self->minion->repair;
124 0 $self->{last_repair} = steady_time;
125 }
126  
127 # Dispatch schedules in regular intervals (database advisory lock prevents duplicates)
128 0 ▶ $self->{last_dispatch} ||= 0;
Condition0%0/2
$self->{"last_dispatch"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"last_dispatch"} ||= 0
$self->{"last_dispatch"}
Truth table0%0/2
A
A= $self->{"last_dispatch"}
Aresult
11
00
129 0 ▶ if ($status->{dispatch_interval} && ($self->{last_dispatch} + $status->{dispatch_interval}) < steady_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->{"dispatch_interval"} and $self->{"last_dispatch"} + $status->{"dispatch_interval"} < &steady_time()) 0 0 0
Condition0%0/3
$status->{"dispatch_interval"} and $self->{"last_dispatch"} + $status->{"dispatch_interval"} < &steady_time()
!ll&&!rl&&r
000
MC/DC0%0/2
$status->{"dispatch_interval"} and $self->{"last_dispatch"} + $status->{"dispatch_interval"} < &steady_time()
$status->{"dispatch_interval"} $self->{"last_dispatch"} + $status->{"dispatch_interval"} < &steady_time()
Truth table0%0/3
A and B
A= $status->{"dispatch_interval"}B= $self->{"last_dispatch"} + $status->{"dispatch_interval"} < &steady_time()
ABresult
0X0
100
111
130 0 $self->minion->dispatch_schedules;
131 0 $self->{last_dispatch} = steady_time;
132 }
133  
134 # Check if jobs are finished
135 0 ▶ my $jobs = $self->{jobs} ||= [];
Condition0%0/2
$self->{"jobs"} ||= []
l!l
00
MC/DC0%0/1
$self->{"jobs"} ||= []
$self->{"jobs"}
Truth table0%0/2
A
A= $self->{"jobs"}
Aresult
11
00
136 0 ▶ @$jobs = map { $_->is_finished && ++$status->{performed} ? () : $_ } @$jobs;
Branch: 1 branch
BranchTrueFalseTotal
$_->is_finished && ++$status->{'performed'} ? : 0 0 0
Condition0%0/3
$_->is_finished and ++$status->{"performed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->is_finished and ++$status->{"performed"}
$_->is_finished ++$status->{"performed"}
Truth table0%0/3
A and B
A= $_->is_finishedB= ++$status->{"performed"}
ABresult
0X0
100
111
137  
138 # Job limit has been reached or worker is stopping
139 0 my @extra;
140 0 ▶ if ($self->{finished} || ($status->{jobs} + $status->{spare}) <= @$jobs) { return $self->emit('busy') }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'finished'} or $status->{'jobs'} + $status->{'spare'} <= @$jobs) { } 0 0 0
elsif ($status->{'jobs'} <= @$jobs) { } 0 0 0
Condition0%0/3
$self->{"finished"} or $status->{"jobs"} + $status->{"spare"} <= @$jobs
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"finished"} or $status->{"jobs"} + $status->{"spare"} <= @$jobs
$self->{"finished"} $status->{"jobs"} + $status->{"spare"} <= @$jobs
Truth table0%0/3
A or B
A= $self->{"finished"}B= $status->{"jobs"} + $status->{"spare"} <= @$jobs
ABresult
1X1
011
000
141 0 elsif ($status->{jobs} <= @$jobs) { @extra = (min_priority => $status->{spare_min_priority}) }
142  
143 # Try to get more jobs
144 0 my $tasks = desired_tasks($status->{limits}, [keys %{$self->minion->tasks}], [map { $_->task } @$jobs]);
145 0 ▶ return unless @$tasks;
Branch: 1 branch
BranchTrueFalseTotal
unless @$tasks 0 0 0
146 0 my ($max, $queues) = @{$status}{qw(dequeue_timeout queues)};
147 0 my $job = $self->emit('wait')->dequeue($max => {queues => $queues, tasks => $tasks, @extra});
148 0 ▶ push @$jobs, $job->start if $job;
Branch: 1 branch
BranchTrueFalseTotal
if $job 0 0 0
149 }
150  
151 2 1;
152  
153 =encoding utf8
154  
155 =head1 NAME
156  
157 Minion::Worker - Minion worker
158  
159 =head1 SYNOPSIS
160  
161 use Minion::Worker;
162  
163 my $worker = Minion::Worker->new(minion => $minion);
164  
165 =head1 DESCRIPTION
166  
167 L<Minion::Worker> performs jobs for L<Minion>.
168  
169 =head1 WORKER SIGNALS
170  
171 The L<Minion::Worker> process can be controlled at runtime with the following signals.
172  
173 =head2 INT, TERM
174  
175 Stop gracefully after finishing the current jobs.
176  
177 =head2 QUIT
178  
179 Stop immediately without finishing the current jobs.
180  
181 =head1 JOB SIGNALS
182  
183 The job processes spawned by the L<Minion::Worker> process can be controlled at runtime with the following signals.
184  
185 =head2 INT, TERM
186  
187 This signal starts out with the operating system default and allows for jobs to install a custom signal handler to stop
188 gracefully.
189  
190 =head2 USR1, USR2
191  
192 These signals start out being ignored and allow for jobs to install custom signal handlers.
193  
194 =head1 EVENTS
195  
196 L<Minion::Worker> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones.
197  
198 =head2 busy
199  
200 $worker->on(busy => sub ($worker) {
201 ...
202 });
203  
204 Emitted in the worker process when it is performing the maximum number of jobs in parallel.
205  
206 $worker->on(busy => sub ($worker) {
207 my $max = $worker->status->{jobs};
208 say "Performing $max jobs.";
209 });
210  
211 =head2 dequeue
212  
213 $worker->on(dequeue => sub ($worker, $job) {
214 ...
215 });
216  
217 Emitted in the worker process after a job has been dequeued.
218  
219 $worker->on(dequeue => sub ($worker, $job) {
220 my $id = $job->id;
221 say "Job $id has been dequeued.";
222 });
223  
224 =head2 wait
225  
226 $worker->on(wait => sub ($worker) {
227 ...
228 });
229  
230 Emitted in the worker process before it tries to dequeue a job.
231  
232 $worker->on(wait => sub ($worker) {
233 my $max = $worker->status->{dequeue_timeout};
234 say "Waiting up to $max seconds for a new job.";
235 });
236  
237 =head1 ATTRIBUTES
238  
239 L<Minion::Worker> implements the following attributes.
240  
241 =head2 commands
242  
243 my $commands = $worker->commands;
244 $worker = $worker->commands({jobs => sub {...}});
245  
246 Registered worker remote control commands.
247  
248 =head2 id
249  
250 my $id = $worker->id;
251 $worker = $worker->id($id);
252  
253 Worker id.
254  
255 =head2 minion
256  
257 my $minion = $worker->minion;
258 $worker = $worker->minion(Minion->new);
259  
260 L<Minion> object this worker belongs to.
261  
262 =head2 status
263  
264 my $status = $worker->status;
265 $worker = $worker->status({queues => ['default', 'important']);
266  
267 Status information to configure workers started with L</"run"> and to share every time L</"register"> is called.
268  
269 =head1 METHODS
270  
271 L<Minion::Worker> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones.
272  
273 =head2 add_command
274  
275 $worker = $worker->add_command(jobs => sub {...});
276  
277 Register a worker remote control command.
278  
279 $worker->add_command(foo => sub ($worker, @args) {
280 ...
281 });
282  
283 =head2 dequeue
284  
285 my $job = $worker->dequeue(0.5);
286 my $job = $worker->dequeue(0.5 => {queues => ['important']});
287  
288 Wait a given amount of time in seconds for a job, dequeue L<Minion::Job> object and transition from C<inactive> to
289 C<active> state, or return C<undef> if queues were empty.
290  
291 These options are currently available:
292  
293 =over 2
294  
295 =item id
296  
297 id => '10023'
298  
299 Dequeue a specific job.
300  
301 =item min_priority
302  
303 min_priority => 3
304  
305 Do not dequeue jobs with a lower priority.
306  
307 =item queues
308  
309 queues => ['important']
310  
311 One or more queues to dequeue jobs from, defaults to C<default>.
312  
313 =item tasks
314  
315 tasks => ['foo', 'bar']
316  
317 One or more tasks to dequeue jobs from, defaults to all tasks.
318  
319 =back
320  
321 =head2 info
322  
323 my $info = $worker->info;
324  
325 Get worker information.
326  
327 # Check worker host
328 my $host = $worker->info->{host};
329  
330 These fields are currently available:
331  
332 =over 2
333  
334 =item host
335  
336 host => 'localhost'
337  
338 Worker host.
339  
340 =item jobs
341  
342 jobs => ['10023', '10024', '10025', '10029']
343  
344 Ids of jobs the worker is currently processing.
345  
346 =item notified
347  
348 notified => 784111777
349  
350 Epoch time worker sent the last heartbeat.
351  
352 =item pid
353  
354 pid => 12345
355  
356 Process id of worker.
357  
358 =item started
359  
360 started => 784111777
361  
362 Epoch time worker was started.
363  
364 =item status
365  
366 status => {queues => ['default', 'important']}
367  
368 Hash reference with whatever status information the worker would like to share.
369  
370 =back
371  
372 =head2 new
373  
374 my $worker = Minion::Worker->new;
375 my $worker = Minion::Worker->new(status => {foo => 'bar'});
376 my $worker = Minion::Worker->new({status => {foo => 'bar'}});
377  
378 Construct a new L<Minion::Worker> object and subscribe to L</"busy"> event with default handler that sleeps for one
379 second.
380  
381 =head2 process_commands
382  
383 $worker = $worker->process_commands;
384  
385 Process worker remote control commands.
386  
387 =head2 register
388  
389 $worker = $worker->register;
390  
391 Register worker or send heartbeat to show that this worker is still alive.
392  
393 =head2 run
394  
395 $worker->run;
396  
397 Run worker and wait for L</"WORKER SIGNALS">.
398  
399 # Start a worker for a special named queue
400 my $worker = $minion->worker;
401 $worker->status->{queues} = ['important'];
402 $worker->run;
403  
404 These L</"status"> options are currently available:
405  
406 =over 2
407  
408 =item command_interval
409  
410 command_interval => 20
411  
412 Worker remote control command interval, defaults to C<10>.
413  
414 =item dequeue_timeout
415  
416 dequeue_timeout => 5
417  
418 Maximum amount time in seconds to wait for a job, defaults to C<5>.
419  
420 =item dispatch_interval
421  
422 dispatch_interval => 60
423  
424 Interval in seconds for dispatching due L<Minion/"schedule">d jobs, defaults to C<30>. Set to C<0> to disable schedule
425 dispatching on this worker (for example when a dedicated scheduler worker is used).
426  
427 =item heartbeat_interval
428  
429 heartbeat_interval => 60
430  
431 Heartbeat interval, defaults to C<300>.
432  
433 =item jobs
434  
435 jobs => 12
436  
437 Maximum number of jobs to perform parallel in forked worker processes (not including spare processes), defaults to C<4>.
438  
439 =item queues
440  
441 queues => ['test']
442  
443 One or more queues to get jobs from, defaults to C<default>.
444  
445 =item repair_interval
446  
447 repair_interval => 3600
448  
449 Repair interval, up to half of this value can be subtracted randomly to make sure not all workers repair at the same
450 time, defaults to C<21600> (6 hours).
451  
452 =item spare
453  
454 spare => 2
455  
456 Number of spare worker processes to reserve for high priority jobs, defaults to C<1>.
457  
458 =item spare_min_priority
459  
460 spare_min_priority => 7
461  
462 Minimum priority of jobs to use spare worker processes for, defaults to C<1>.
463  
464 =back
465  
466 These remote control L</"commands"> are currently available:
467  
468 =over 2
469  
470 =item jobs
471  
472 $minion->broadcast('jobs', [10]);
473 $minion->broadcast('jobs', [10], [$worker_id]);
474  
475 Instruct one or more workers to change the number of jobs to perform concurrently. Setting this value to C<0> will
476 effectively pause the worker. That means all current jobs will be finished, but no new ones accepted, until the number
477 is increased again.
478  
479 =item kill
480  
481 $minion->broadcast('kill', ['INT', 10025]);
482 $minion->broadcast('kill', ['INT', 10025], [$worker_id]);
483  
484 Instruct one or more workers to send a signal to a job that is currently being performed. This command will be ignored
485 by workers that do not have a job matching the id. That means it is safe to broadcast this command to all workers.
486  
487 =item stop
488  
489 $minion->broadcast('stop', [10025]);
490 $minion->broadcast('stop', [10025], [$worker_id]);
491  
492 Instruct one or more workers to stop a job that is currently being performed immediately. This command will be ignored
493 by workers that do not have a job matching the id. That means it is safe to broadcast this command to all workers.
494  
495 =back
496  
497 =head2 unregister
498  
499 $worker = $worker->unregister;
500  
501 Unregister worker.
502  
503 =head1 SEE ALSO
504  
505 L<Minion>, L<Minion::Guide>, L<https://minion.pm>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
506  
507 =cut
« Minion/Command/minion/job.pm ↑ Summary Minion/Command/minion/schedule.pm »