1Minion::Worker(3)     User Contributed Perl Documentation    Minion::Worker(3)
2
3
4

NAME

6       Minion::Worker - Minion worker
7

SYNOPSIS

9         use Minion::Worker;
10
11         my $worker = Minion::Worker->new(minion => $minion);
12

DESCRIPTION

14       Minion::Worker performs jobs for Minion.
15

WORKER SIGNALS

17       The Minion::Worker process can be controlled at runtime with the
18       following signals.
19
20   INT, TERM
21       Stop gracefully after finishing the current jobs.
22
23   QUIT
24       Stop immediately without finishing the current jobs.
25

JOB SIGNALS

27       The job processes spawned by the Minion::Worker process can be
28       controlled at runtime with the following signals.
29
30   INT
31       This signal starts out with the operating system default and allows for
32       jobs to install a custom signal handler to stop gracefully.
33
34   USR1, USR2
35       These signals start out being ignored and allow for jobs to install
36       custom signal handlers.
37

EVENTS

39       Minion::Worker inherits all events from Mojo::EventEmitter and can emit
40       the following new ones.
41
42   busy
43         $worker->on(busy => sub {
44           my $worker = shift;
45           ...
46         });
47
48       Emitted in the worker process when it is performing the maximum number
49       of jobs in parallel.
50
51         $worker->on(busy => sub {
52           my $worker = shift;
53           my $max = $worker->status->{jobs};
54           say "Performing $max jobs.";
55         });
56
57   dequeue
58         $worker->on(dequeue => sub {
59           my ($worker, $job) = @_;
60           ...
61         });
62
63       Emitted in the worker process after a job has been dequeued.
64
65         $worker->on(dequeue => sub {
66           my ($worker, $job) = @_;
67           my $id = $job->id;
68           say "Job $id has been dequeued.";
69         });
70
71   wait
72         $worker->on(wait => sub {
73           my $worker = shift;
74           ...
75         });
76
77       Emitted in the worker process before it tries to dequeue a job.
78
79         $worker->on(wait => sub {
80           my $worker = shift;
81           my $max = $worker->status->{dequeue_timeout};
82           say "Waiting up to $max seconds for a new job.";
83         });
84

ATTRIBUTES

86       Minion::Worker implements the following attributes.
87
88   commands
89         my $commands = $worker->commands;
90         $worker      = $worker->commands({jobs => sub {...}});
91
92       Registered worker remote control commands.
93
94   id
95         my $id  = $worker->id;
96         $worker = $worker->id($id);
97
98       Worker id.
99
100   minion
101         my $minion = $worker->minion;
102         $worker    = $worker->minion(Minion->new);
103
104       Minion object this worker belongs to.
105
106   status
107         my $status = $worker->status;
108         $worker    = $worker->status({queues => ['default', 'important']);
109
110       Status information to configure workers started with "run" and to share
111       every time "register" is called.
112

METHODS

114       Minion::Worker inherits all methods from Mojo::EventEmitter and
115       implements the following new ones.
116
117   add_command
118         $worker = $worker->add_command(jobs => sub {...});
119
120       Register a worker remote control command.
121
122         $worker->add_command(foo => sub {
123           my ($worker, @args) = @_;
124           ...
125         });
126
127   dequeue
128         my $job = $worker->dequeue(0.5);
129         my $job = $worker->dequeue(0.5 => {queues => ['important']});
130
131       Wait a given amount of time in seconds for a job, dequeue Minion::Job
132       object and transition from "inactive" to "active" state, or return
133       "undef" if queues were empty.
134
135       These options are currently available:
136
137       id
138           id => '10023'
139
140         Dequeue a specific job.
141
142       queues
143           queues => ['important']
144
145         One or more queues to dequeue jobs from, defaults to "default".
146
147   info
148         my $info = $worker->info;
149
150       Get worker information.
151
152         # Check worker host
153         my $host = $worker->info->{host};
154
155       These fields are currently available:
156
157       host
158           host => 'localhost'
159
160         Worker host.
161
162       jobs
163           jobs => ['10023', '10024', '10025', '10029']
164
165         Ids of jobs the worker is currently processing.
166
167       notified
168           notified => 784111777
169
170         Epoch time worker sent the last heartbeat.
171
172       pid
173           pid => 12345
174
175         Process id of worker.
176
177       started
178           started => 784111777
179
180         Epoch time worker was started.
181
182       status
183           status => {queues => ['default', 'important']}
184
185         Hash reference with whatever status information the worker would like
186         to share.
187
188   new
189         my $worker = Minion::Worker->new;
190         my $worker = Minion::Worker->new(status => {foo => 'bar'});
191         my $worker = Minion::Worker->new({status => {foo => 'bar'}});
192
193       Construct a new Minion::Worker object and subscribe to "busy" event
194       with default handler that sleeps for one second.
195
196   process_commands
197         $worker = $worker->process_commands;
198
199       Process worker remote control commands.
200
201   register
202         $worker = $worker->register;
203
204       Register worker or send heartbeat to show that this worker is still
205       alive.
206
207   run
208         $worker->run;
209
210       Run worker and wait for "WORKER SIGNALS".
211
212       These "status" options are currently available:
213
214       command_interval
215           command_interval => 20
216
217         Worker remote control command interval, defaults to 10.
218
219       dequeue_timeout
220           dequeue_timeout => 5
221
222         Maximum amount time in seconds to wait for a job, defaults to 5.
223
224       heartbeat_interval
225           heartbeat_interval => 60
226
227         Heartbeat interval, defaults to 300.
228
229       jobs
230           jobs => 12
231
232         Maximum number of jobs to perform parallel in forked worker
233         processes, defaults to 4.
234
235       queues
236           queues => ['test']
237
238         One or more queues to get jobs from, defaults to "default".
239
240       repair_interval
241           repair_interval => 3600
242
243         Repair interval, up to half of this value can be subtracted randomly
244         to make sure not all workers repair at the same time, defaults to
245         21600 (6 hours).
246
247       These remote control "commands" are currently available:
248
249       jobs
250           $minion->broadcast('jobs', [10]);
251           $minion->broadcast('jobs', [10], [$worker_id]);
252
253         Instruct one or more workers to change the number of jobs to perform
254         concurrently. Setting this value to 0 will effectively pause the
255         worker. That means all current jobs will be finished, but no new ones
256         accepted, until the number is increased again.
257
258       kill
259           $minion->broadcast('kill', ['INT', 10025]);
260           $minion->broadcast('kill', ['INT', 10025], [$worker_id]);
261
262         Instruct one or more workers to send a signal to a job that is
263         currently being performed. This command will be ignored by workers
264         that do not have a job matching the id. That means it is safe to
265         broadcast this command to all workers.
266
267       stop
268           $minion->broadcast('stop', [10025]);
269           $minion->broadcast('stop', [10025], [$worker_id]);
270
271         Instruct one or more workers to stop a job that is currently being
272         performed immediately. This command will be ignored by workers that
273         do not have a job matching the id. That means it is safe to broadcast
274         this command to all workers.
275
276   unregister
277         $worker = $worker->unregister;
278
279       Unregister worker.
280

SEE ALSO

282       Minion, Mojolicious::Guides, <https://mojolicious.org>.
283
284
285
286perl v5.30.0                      2019-09-01                 Minion::Worker(3)
Impressum