1Minion::Backend::SQLiteU(s3eprm)Contributed Perl DocumenMtiantiioonn::Backend::SQLite(3pm)
2
3
4
6 Minion::Backend::SQLite - SQLite backend for Minion job queue
7
9 use Minion::Backend::SQLite;
10 my $backend = Minion::Backend::SQLite->new('sqlite:test.db');
11
12 # Minion
13 use Minion;
14 my $minion = Minion->new(SQLite => 'sqlite:test.db');
15
16 # Mojolicious (via Mojolicious::Plugin::Minion)
17 $self->plugin(Minion => { SQLite => 'sqlite:test.db' });
18
19 # Mojolicious::Lite (via Mojolicious::Plugin::Minion)
20 plugin Minion => { SQLite => 'sqlite:test.db' };
21
22 # Share the database connection cache
23 helper sqlite => sub { state $sqlite = Mojo::SQLite->new('sqlite:test.db') };
24 plugin Minion => { SQLite => app->sqlite };
25
27 Minion::Backend::SQLite is a backend for Minion based on Mojo::SQLite.
28 All necessary tables will be created automatically with a set of
29 migrations named "minion". If no connection string or ":temp:" is
30 provided, the database will be created in a temporary directory.
31
33 Minion::Backend::SQLite inherits all attributes from Minion::Backend
34 and implements the following new ones.
35
36 dequeue_interval
37 my $seconds = $backend->dequeue_interval;
38 $backend = $backend->dequeue_interval($seconds);
39
40 Interval in seconds between "dequeue" attempts. Defaults to 0.5.
41
42 sqlite
43 my $sqlite = $backend->sqlite;
44 $backend = $backend->sqlite(Mojo::SQLite->new);
45
46 Mojo::SQLite object used to store all data.
47
49 Minion::Backend::SQLite inherits all methods from Minion::Backend and
50 implements the following new ones.
51
52 new
53 my $backend = Minion::Backend::SQLite->new;
54 my $backend = Minion::Backend::SQLite->new(':temp:');
55 my $backend = Minion::Backend::SQLite->new('sqlite:test.db');
56 my $backend = Minion::Backend::SQLite->new->tap(sub { $_->sqlite->from_filename('C:\\foo\\bar.db') });
57 my $backend = Minion::Backend::SQLite->new(Mojo::SQLite->new);
58
59 Construct a new Minion::Backend::SQLite object.
60
61 broadcast
62 my $bool = $backend->broadcast('some_command');
63 my $bool = $backend->broadcast('some_command', [@args]);
64 my $bool = $backend->broadcast('some_command', [@args], [$id1, $id2, $id3]);
65
66 Broadcast remote control command to one or more workers.
67
68 dequeue
69 my $job_info = $backend->dequeue($worker_id, 0.5);
70 my $job_info = $backend->dequeue($worker_id, 0.5, {queues => ['important']});
71
72 Wait a given amount of time in seconds for a job, dequeue it and
73 transition from "inactive" to "active" state, or return "undef" if
74 queues were empty. Jobs will be checked for in intervals defined by
75 "dequeue_interval" until the timeout is reached.
76
77 These options are currently available:
78
79 id
80 id => '10023'
81
82 Dequeue a specific job.
83
84 min_priority
85 min_priority => 3
86
87 Do not dequeue jobs with a lower priority.
88
89 queues
90 queues => ['important']
91
92 One or more queues to dequeue jobs from, defaults to "default".
93
94 These fields are currently available:
95
96 args
97 args => ['foo', 'bar']
98
99 Job arguments.
100
101 id
102 id => '10023'
103
104 Job ID.
105
106 retries
107 retries => 3
108
109 Number of times job has been retried.
110
111 task
112 task => 'foo'
113
114 Task name.
115
116 enqueue
117 my $job_id = $backend->enqueue('foo');
118 my $job_id = $backend->enqueue(foo => [@args]);
119 my $job_id = $backend->enqueue(foo => [@args] => {priority => 1});
120
121 Enqueue a new job with "inactive" state.
122
123 These options are currently available:
124
125 attempts
126 attempts => 25
127
128 Number of times performing this job will be attempted, with a delay
129 based on "backoff" in Minion after the first attempt, defaults to 1.
130
131 delay
132 delay => 10
133
134 Delay job for this many seconds (from now).
135
136 expire
137 expire => 300
138
139 Job is valid for this many seconds (from now) before it expires. Note
140 that this option is EXPERIMENTAL and might change without warning!
141
142 lax
143 lax => 1
144
145 Existing jobs this job depends on may also have transitioned to the
146 "failed" state to allow for it to be processed, defaults to "false".
147 Note that this option is EXPERIMENTAL and might change without
148 warning!
149
150 notes
151 notes => {foo => 'bar', baz => [1, 2, 3]}
152
153 Hash reference with arbitrary metadata for this job.
154
155 parents
156 parents => [$id1, $id2, $id3]
157
158 One or more existing jobs this job depends on, and that need to have
159 transitioned to the state "finished" before it can be processed.
160
161 priority
162 priority => 5
163
164 Job priority, defaults to 0. Jobs with a higher priority get
165 performed first.
166
167 queue
168 queue => 'important'
169
170 Queue to put job in, defaults to "default".
171
172 fail_job
173 my $bool = $backend->fail_job($job_id, $retries);
174 my $bool = $backend->fail_job($job_id, $retries, 'Something went wrong!');
175 my $bool = $backend->fail_job(
176 $job_id, $retries, {msg => 'Something went wrong!'});
177
178 Transition from "active" to "failed" state with or without a result,
179 and if there are attempts remaining, transition back to "inactive" with
180 an exponentially increasing delay based on "backoff" in Minion.
181
182 finish_job
183 my $bool = $backend->finish_job($job_id, $retries);
184 my $bool = $backend->finish_job($job_id, $retries, 'All went well!');
185 my $bool = $backend->finish_job($job_id, $retries, {msg => 'All went well!'});
186
187 Transition from "active" to "finished" state with or without a result.
188
189 history
190 my $history = $backend->history;
191
192 Get history information for job queue.
193
194 These fields are currently available:
195
196 daily
197 daily => [{epoch => 12345, finished_jobs => 95, failed_jobs => 2}, ...]
198
199 Hourly counts for processed jobs from the past day.
200
201 list_jobs
202 my $results = $backend->list_jobs($offset, $limit);
203 my $results = $backend->list_jobs($offset, $limit, {states => ['inactive']});
204
205 Returns the information about jobs in batches.
206
207 # Get the total number of results (without limit)
208 my $num = $backend->list_jobs(0, 100, {queues => ['important']})->{total};
209
210 # Check job state
211 my $results = $backend->list_jobs(0, 1, {ids => [$job_id]});
212 my $state = $results->{jobs}[0]{state};
213
214 # Get job result
215 my $results = $backend->list_jobs(0, 1, {ids => [$job_id]});
216 my $result = $results->{jobs}[0]{result};
217
218 These options are currently available:
219
220 before
221 before => 23
222
223 List only jobs before this id.
224
225 ids
226 ids => ['23', '24']
227
228 List only jobs with these ids.
229
230 queues
231 queues => ['important', 'unimportant']
232
233 List only jobs in these queues.
234
235 states
236 states => ['inactive', 'active']
237
238 List only jobs in these states.
239
240 tasks
241 tasks => ['foo', 'bar']
242
243 List only jobs for these tasks.
244
245 These fields are currently available:
246
247 args
248 args => ['foo', 'bar']
249
250 Job arguments.
251
252 attempts
253 attempts => 25
254
255 Number of times performing this job will be attempted.
256
257 children
258 children => ['10026', '10027', '10028']
259
260 Jobs depending on this job.
261
262 created
263 created => 784111777
264
265 Epoch time job was created.
266
267 delayed
268 delayed => 784111777
269
270 Epoch time job was delayed to.
271
272 expires
273 expires => 784111777
274
275 Epoch time job is valid until before it expires.
276
277 finished
278 finished => 784111777
279
280 Epoch time job was finished.
281
282 id
283 id => 10025
284
285 Job id.
286
287 lax
288 lax => 0
289
290 Existing jobs this job depends on may also have failed to allow for
291 it to be processed.
292
293 notes
294 notes => {foo => 'bar', baz => [1, 2, 3]}
295
296 Hash reference with arbitrary metadata for this job.
297
298 parents
299 parents => ['10023', '10024', '10025']
300
301 Jobs this job depends on.
302
303 priority
304 priority => 3
305
306 Job priority.
307
308 queue
309 queue => 'important'
310
311 Queue name.
312
313 result
314 result => 'All went well!'
315
316 Job result.
317
318 retried
319 retried => 784111777
320
321 Epoch time job has been retried.
322
323 retries
324 retries => 3
325
326 Number of times job has been retried.
327
328 started
329 started => 784111777
330
331 Epoch time job was started.
332
333 state
334 state => 'inactive'
335
336 Current job state, usually "active", "failed", "finished" or
337 "inactive".
338
339 task
340 task => 'foo'
341
342 Task name.
343
344 time
345 time => 78411177
346
347 Current time.
348
349 worker
350 worker => '154'
351
352 Id of worker that is processing the job.
353
354 list_locks
355 my $results = $backend->list_locks($offset, $limit);
356 my $results = $backend->list_locks($offset, $limit, {names => ['foo']});
357
358 Returns information about locks in batches.
359
360 # Get the total number of results (without limit)
361 my $num = $backend->list_locks(0, 100, {names => ['bar']})->{total};
362
363 # Check expiration time
364 my $results = $backend->list_locks(0, 1, {names => ['foo']});
365 my $expires = $results->{locks}[0]{expires};
366
367 These options are currently available:
368
369 names
370 names => ['foo', 'bar']
371
372 List only locks with these names.
373
374 These fields are currently available:
375
376 expires
377 expires => 784111777
378
379 Epoch time this lock will expire.
380
381 name
382 name => 'foo'
383
384 Lock name.
385
386 list_workers
387 my $results = $backend->list_workers($offset, $limit);
388 my $results = $backend->list_workers($offset, $limit, {ids => [23]});
389
390 Returns information about workers in batches.
391
392 # Get the total number of results (without limit)
393 my $num = $backend->list_workers(0, 100)->{total};
394
395 # Check worker host
396 my $results = $backend->list_workers(0, 1, {ids => [$worker_id]});
397 my $host = $results->{workers}[0]{host};
398
399 These options are currently available:
400
401 before
402 before => 23
403
404 List only workers before this id.
405
406 ids
407 ids => ['23', '24']
408
409 List only workers with these ids.
410
411 These fields are currently available:
412
413 id
414 id => 22
415
416 Worker id.
417
418 host
419 host => 'localhost'
420
421 Worker host.
422
423 jobs
424 jobs => ['10023', '10024', '10025', '10029']
425
426 Ids of jobs the worker is currently processing.
427
428 notified
429 notified => 784111777
430
431 Epoch time worker sent the last heartbeat.
432
433 pid
434 pid => 12345
435
436 Process id of worker.
437
438 started
439 started => 784111777
440
441 Epoch time worker was started.
442
443 status
444 status => {queues => ['default', 'important']}
445
446 Hash reference with whatever status information the worker would like
447 to share.
448
449 lock
450 my $bool = $backend->lock('foo', 3600);
451 my $bool = $backend->lock('foo', 3600, {limit => 20});
452
453 Try to acquire a named lock that will expire automatically after the
454 given amount of time in seconds. An expiration time of 0 can be used to
455 check if a named lock already exists without creating one.
456
457 These options are currently available:
458
459 limit
460 limit => 20
461
462 Number of shared locks with the same name that can be active at the
463 same time, defaults to 1.
464
465 note
466 my $bool = $backend->note($job_id, {mojo => 'rocks', minion => 'too'});
467
468 Change one or more metadata fields for a job. Setting a value to
469 "undef" will remove the field. It is currently an error to attempt to
470 set a metadata field with a name containing the characters ".", "[", or
471 "]".
472
473 receive
474 my $commands = $backend->receive($worker_id);
475
476 Receive remote control commands for worker.
477
478 register_worker
479 my $worker_id = $backend->register_worker;
480 my $worker_id = $backend->register_worker($worker_id);
481 my $worker_id = $backend->register_worker(
482 $worker_id, {status => {queues => ['default', 'important']}});
483
484 Register worker or send heartbeat to show that this worker is still
485 alive.
486
487 These options are currently available:
488
489 status
490 status => {queues => ['default', 'important']}
491
492 Hash reference with whatever status information the worker would like
493 to share.
494
495 remove_job
496 my $bool = $backend->remove_job($job_id);
497
498 Remove "failed", "finished" or "inactive" job from queue.
499
500 repair
501 $backend->repair;
502
503 Repair worker registry and job queue if necessary.
504
505 reset
506 $backend->reset({all => 1});
507
508 Reset job queue.
509
510 These options are currently available:
511
512 all
513 all => 1
514
515 Reset everything.
516
517 locks
518 locks => 1
519
520 Reset only locks.
521
522 retry_job
523 my $bool = $backend->retry_job($job_id, $retries);
524 my $bool = $backend->retry_job($job_id, $retries, {delay => 10});
525
526 Transition job back to "inactive" state, already "inactive" jobs may
527 also be retried to change options.
528
529 These options are currently available:
530
531 attempts
532 attempts => 25
533
534 Number of times performing this job will be attempted.
535
536 delay
537 delay => 10
538
539 Delay job for this many seconds (from now).
540
541 expire
542 expire => 300
543
544 Job is valid for this many seconds (from now) before it expires. Note
545 that this option is EXPERIMENTAL and might change without warning!
546
547 lax
548 lax => 1
549
550 Existing jobs this job depends on may also have transitioned to the
551 "failed" state to allow for it to be processed, defaults to "false".
552 Note that this option is EXPERIMENTAL and might change without
553 warning!
554
555 parents
556 parents => [$id1, $id2, $id3]
557
558 Jobs this job depends on.
559
560 priority
561 priority => 5
562
563 Job priority.
564
565 queue
566 queue => 'important'
567
568 Queue to put job in.
569
570 stats
571 my $stats = $backend->stats;
572
573 Get statistics for the job queue.
574
575 These fields are currently available:
576
577 active_jobs
578 active_jobs => 100
579
580 Number of jobs in "active" state.
581
582 active_locks
583 active_locks => 100
584
585 Number of active named locks.
586
587 active_workers
588 active_workers => 100
589
590 Number of workers that are currently processing a job.
591
592 delayed_jobs
593 delayed_jobs => 100
594
595 Number of jobs in "inactive" state that are scheduled to run at
596 specific time in the future.
597
598 enqueued_jobs
599 enqueued_jobs => 100000
600
601 Rough estimate of how many jobs have ever been enqueued.
602
603 failed_jobs
604 failed_jobs => 100
605
606 Number of jobs in "failed" state.
607
608 finished_jobs
609 finished_jobs => 100
610
611 Number of jobs in "finished" state.
612
613 inactive_jobs
614 inactive_jobs => 100
615
616 Number of jobs in "inactive" state.
617
618 inactive_workers
619 inactive_workers => 100
620
621 Number of workers that are currently not processing a job.
622
623 uptime
624 uptime => undef
625
626 Uptime in seconds. Always undefined for SQLite.
627
628 unlock
629 my $bool = $backend->unlock('foo');
630
631 Release a named lock.
632
633 unregister_worker
634 $backend->unregister_worker($worker_id);
635
636 Unregister worker.
637
639 Report any issues on the public bugtracker.
640
642 Dan Book <dbook@cpan.org>
643
645 This software is Copyright (c) 2015 by Dan Book.
646
647 This is free software, licensed under:
648
649 The Artistic License 2.0 (GPL Compatible)
650
652 Minion, Mojo::SQLite
653
654
655
656perl v5.34.0 2022-01-21 Minion::Backend::SQLite(3pm)