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