1Minion::Worker(3) User Contributed Perl Documentation Minion::Worker(3)
2
3
4
6 Minion::Worker - Minion worker
7
9 use Minion::Worker;
10
11 my $worker = Minion::Worker->new(minion => $minion);
12
14 Minion::Worker performs jobs for Minion.
15
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
27 The job processes spawned by the Minion::Worker process can be
28 controlled at runtime with the following signals.
29
30 INT, TERM
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
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 ($worker) {
44 ...
45 });
46
47 Emitted in the worker process when it is performing the maximum number
48 of jobs in parallel.
49
50 $worker->on(busy => sub ($worker) {
51 my $max = $worker->status->{jobs};
52 say "Performing $max jobs.";
53 });
54
55 dequeue
56 $worker->on(dequeue => sub ($worker, $job) {
57 ...
58 });
59
60 Emitted in the worker process after a job has been dequeued.
61
62 $worker->on(dequeue => sub ($worker, $job) {
63 my $id = $job->id;
64 say "Job $id has been dequeued.";
65 });
66
67 wait
68 $worker->on(wait => sub ($worker) {
69 ...
70 });
71
72 Emitted in the worker process before it tries to dequeue a job.
73
74 $worker->on(wait => sub ($worker) {
75 my $max = $worker->status->{dequeue_timeout};
76 say "Waiting up to $max seconds for a new job.";
77 });
78
80 Minion::Worker implements the following attributes.
81
82 commands
83 my $commands = $worker->commands;
84 $worker = $worker->commands({jobs => sub {...}});
85
86 Registered worker remote control commands.
87
88 id
89 my $id = $worker->id;
90 $worker = $worker->id($id);
91
92 Worker id.
93
94 minion
95 my $minion = $worker->minion;
96 $worker = $worker->minion(Minion->new);
97
98 Minion object this worker belongs to.
99
100 status
101 my $status = $worker->status;
102 $worker = $worker->status({queues => ['default', 'important']);
103
104 Status information to configure workers started with "run" and to share
105 every time "register" is called.
106
108 Minion::Worker inherits all methods from Mojo::EventEmitter and
109 implements the following new ones.
110
111 add_command
112 $worker = $worker->add_command(jobs => sub {...});
113
114 Register a worker remote control command.
115
116 $worker->add_command(foo => sub ($worker, @args) {
117 ...
118 });
119
120 dequeue
121 my $job = $worker->dequeue(0.5);
122 my $job = $worker->dequeue(0.5 => {queues => ['important']});
123
124 Wait a given amount of time in seconds for a job, dequeue Minion::Job
125 object and transition from "inactive" to "active" state, or return
126 "undef" if queues were empty.
127
128 These options are currently available:
129
130 id
131 id => '10023'
132
133 Dequeue a specific job.
134
135 min_priority
136 min_priority => 3
137
138 Do not dequeue jobs with a lower priority.
139
140 queues
141 queues => ['important']
142
143 One or more queues to dequeue jobs from, defaults to "default".
144
145 info
146 my $info = $worker->info;
147
148 Get worker information.
149
150 # Check worker host
151 my $host = $worker->info->{host};
152
153 These fields are currently available:
154
155 host
156 host => 'localhost'
157
158 Worker host.
159
160 jobs
161 jobs => ['10023', '10024', '10025', '10029']
162
163 Ids of jobs the worker is currently processing.
164
165 notified
166 notified => 784111777
167
168 Epoch time worker sent the last heartbeat.
169
170 pid
171 pid => 12345
172
173 Process id of worker.
174
175 started
176 started => 784111777
177
178 Epoch time worker was started.
179
180 status
181 status => {queues => ['default', 'important']}
182
183 Hash reference with whatever status information the worker would like
184 to share.
185
186 new
187 my $worker = Minion::Worker->new;
188 my $worker = Minion::Worker->new(status => {foo => 'bar'});
189 my $worker = Minion::Worker->new({status => {foo => 'bar'}});
190
191 Construct a new Minion::Worker object and subscribe to "busy" event
192 with default handler that sleeps for one second.
193
194 process_commands
195 $worker = $worker->process_commands;
196
197 Process worker remote control commands.
198
199 register
200 $worker = $worker->register;
201
202 Register worker or send heartbeat to show that this worker is still
203 alive.
204
205 run
206 $worker->run;
207
208 Run worker and wait for "WORKER SIGNALS".
209
210 # Start a worker for a special named queue
211 my $worker = $minion->worker;
212 $worker->status->{queues} = ['important'];
213 $worker->run;
214
215 These "status" options are currently available:
216
217 command_interval
218 command_interval => 20
219
220 Worker remote control command interval, defaults to 10.
221
222 dequeue_timeout
223 dequeue_timeout => 5
224
225 Maximum amount time in seconds to wait for a job, defaults to 5.
226
227 heartbeat_interval
228 heartbeat_interval => 60
229
230 Heartbeat interval, defaults to 300.
231
232 jobs
233 jobs => 12
234
235 Maximum number of jobs to perform parallel in forked worker processes
236 (not including spare processes), defaults to 4.
237
238 queues
239 queues => ['test']
240
241 One or more queues to get jobs from, defaults to "default".
242
243 repair_interval
244 repair_interval => 3600
245
246 Repair interval, up to half of this value can be subtracted randomly
247 to make sure not all workers repair at the same time, defaults to
248 21600 (6 hours).
249
250 spare
251 spare => 2
252
253 Number of spare worker processes to reserve for high priority jobs,
254 defaults to 1.
255
256 spare_min_priority
257 spare_min_priority => 7
258
259 Minimum priority of jobs to use spare worker processes for, defaults
260 to 1.
261
262 These remote control "commands" are currently available:
263
264 jobs
265 $minion->broadcast('jobs', [10]);
266 $minion->broadcast('jobs', [10], [$worker_id]);
267
268 Instruct one or more workers to change the number of jobs to perform
269 concurrently. Setting this value to 0 will effectively pause the
270 worker. That means all current jobs will be finished, but no new ones
271 accepted, until the number is increased again.
272
273 kill
274 $minion->broadcast('kill', ['INT', 10025]);
275 $minion->broadcast('kill', ['INT', 10025], [$worker_id]);
276
277 Instruct one or more workers to send a signal to a job that is
278 currently being performed. This command will be ignored by workers
279 that do not have a job matching the id. That means it is safe to
280 broadcast this command to all workers.
281
282 stop
283 $minion->broadcast('stop', [10025]);
284 $minion->broadcast('stop', [10025], [$worker_id]);
285
286 Instruct one or more workers to stop a job that is currently being
287 performed immediately. This command will be ignored by workers that
288 do not have a job matching the id. That means it is safe to broadcast
289 this command to all workers.
290
291 unregister
292 $worker = $worker->unregister;
293
294 Unregister worker.
295
297 Minion, Minion::Guide, <https://minion.pm>, Mojolicious::Guides,
298 <https://mojolicious.org>.
299
300
301
302perl v5.36.0 2022-07-22 Minion::Worker(3)