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