.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Minion 3pm" .TH Minion 3pm "2017-01-04" "perl v5.24.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Minion \- Job queue .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Minion; \& \& # Connect to backend \& my $minion = Minion\->new(Pg => \*(Aqpostgresql://postgres@/test\*(Aq); \& \& # Add tasks \& $minion\->add_task(something_slow => sub { \& my ($job, @args) = @_; \& sleep 5; \& say \*(AqThis is a background worker process.\*(Aq; \& }); \& \& # Enqueue jobs \& $minion\->enqueue(something_slow => [\*(Aqfoo\*(Aq, \*(Aqbar\*(Aq]); \& $minion\->enqueue(something_slow => [1, 2, 3] => {priority => 5}); \& \& # Perform jobs for testing \& $minion\->enqueue(something_slow => [\*(Aqfoo\*(Aq, \*(Aqbar\*(Aq]); \& $minion\->perform_jobs; \& \& # Build more sophisticated workers \& my $worker = $minion\->repair\->worker; \& while (int rand 2) { \& if (my $job = $worker\->register\->dequeue(5)) { $job\->perform } \& } \& $worker\->unregister; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Minion is a job queue for the Mojolicious real-time web framework, with support for multiple named queues, priorities, delayed jobs, job dependencies, job results, retries with backoff, statistics, distributed workers, parallel processing, autoscaling, remote control, resource leak protection and multiple backends (such as PostgreSQL ). .PP Job queues allow you to process time and/or computationally intensive tasks in background processes, outside of the request/response lifecycle. Among those tasks you'll commonly find image resizing, spam filtering, \s-1HTTP\s0 downloads, building tarballs, warming caches and basically everything else you can imagine that's not super fast. .PP .Vb 1 \& use Mojolicious::Lite; \& \& plugin Minion => {Pg => \*(Aqpostgresql://sri:s3cret@localhost/test\*(Aq}; \& \& # Slow task \& app\->minion\->add_task(poke_mojo => sub { \& my $job = shift; \& $job\->app\->ua\->get(\*(Aqmojolicious.org\*(Aq); \& $job\->app\->log\->debug(\*(AqWe have poked mojolicious.org for a visitor\*(Aq); \& }); \& \& # Perform job in a background worker process \& get \*(Aq/\*(Aq => sub { \& my $c = shift; \& $c\->minion\->enqueue(\*(Aqpoke_mojo\*(Aq); \& $c\->render(text => \*(AqWe will poke mojolicious.org for you soon.\*(Aq); \& }; \& \& app\->start; .Ve .PP Background worker processes are usually started with the command Minion::Command::minion::worker, which becomes automatically available when an application loads the plugin Mojolicious::Plugin::Minion. .PP .Vb 1 \& $ ./myapp.pl minion worker .Ve .PP Jobs can be managed right from the command line with Minion::Command::minion::job. .PP .Vb 1 \& $ ./myapp.pl minion job .Ve .PP To manage background worker processes with systemd, you can use a unit configuration file like this. .PP .Vb 3 \& [Unit] \& Description=My Mojolicious application workers \& After=postgresql.service \& \& [Service] \& Type=simple \& ExecStart=/home/sri/myapp/myapp.pl minion worker \-m production \& KillMode=process \& \& [Install] \& WantedBy=multi\-user.target .Ve .PP Every job can fail or succeed, but not get lost, the system is eventually consistent and will preserve job results for as long as you like, depending on \&\*(L"remove_after\*(R". While individual workers can fail in the middle of processing a job, the system will detect this and ensure that no job is left in an uncertain state, depending on \*(L"missing_after\*(R". .SH "GROWING" .IX Header "GROWING" And as your application grows, you can move tasks into application specific plugins. .PP .Vb 2 \& package MyApp::Task::PokeMojo; \& use Mojo::Base \*(AqMojolicious::Plugin\*(Aq; \& \& sub register { \& my ($self, $app) = @_; \& $app\->minion\->add_task(poke_mojo => sub { \& my $job = shift; \& $job\->app\->ua\->get(\*(Aqmojolicious.org\*(Aq); \& $job\->app\->log\->debug(\*(AqWe have poked mojolicious.org for a visitor\*(Aq); \& }); \& } \& \& 1; .Ve .PP Which are loaded like any other plugin from your application. .PP .Vb 2 \& # Mojolicious \& $app\->plugin(\*(AqMyApp::Task::PokeMojo\*(Aq); \& \& # Mojolicious::Lite \& plugin \*(AqMyApp::Task::PokeMojo\*(Aq; .Ve .SH "EVENTS" .IX Header "EVENTS" Minion inherits all events from Mojo::EventEmitter and can emit the following new ones. .SS "enqueue" .IX Subsection "enqueue" .Vb 4 \& $minion\->on(enqueue => sub { \& my ($minion, $id) = @_; \& ... \& }); .Ve .PP Emitted after a job has been enqueued, in the process that enqueued it. .PP .Vb 4 \& $minion\->on(enqueue => sub { \& my ($minion, $id) = @_; \& say "Job $id has been enqueued."; \& }); .Ve .SS "worker" .IX Subsection "worker" .Vb 4 \& $minion\->on(worker => sub { \& my ($minion, $worker) = @_; \& ... \& }); .Ve .PP Emitted in the worker process after it has been created. .PP .Vb 5 \& $minion\->on(worker => sub { \& my ($minion, $worker) = @_; \& my $id = $worker\->id; \& say "Worker $$:$id started."; \& }); .Ve .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Minion implements the following attributes. .SS "app" .IX Subsection "app" .Vb 2 \& my $app = $minion\->app; \& $minion = $minion\->app(MyApp\->new); .Ve .PP Application for job queue, defaults to a Mojo::HelloWorld object. .SS "backend" .IX Subsection "backend" .Vb 2 \& my $backend = $minion\->backend; \& $minion = $minion\->backend(Minion::Backend::Pg\->new); .Ve .PP Backend, usually a Minion::Backend::Pg object. .SS "backoff" .IX Subsection "backoff" .Vb 2 \& my $cb = $minion\->backoff; \& $minion = $minion\->backoff(sub {...}); .Ve .PP A callback used to calculate the delay for automatically retried jobs, defaults to \f(CW\*(C`(retries ** 4) + 15\*(C'\fR (15, 16, 31, 96, 271, 640...), which means that roughly \f(CW25\fR attempts can be made in \f(CW21\fR days. .PP .Vb 4 \& $minion\->backoff(sub { \& my $retries = shift; \& return ($retries ** 4) + 15 + int(rand 30); \& }); .Ve .SS "missing_after" .IX Subsection "missing_after" .Vb 2 \& my $after = $minion\->missing_after; \& $minion = $minion\->missing_after(172800); .Ve .PP Amount of time in seconds after which workers without a heartbeat will be considered missing and removed from the registry by \*(L"repair\*(R", defaults to \&\f(CW1800\fR (30 minutes). .SS "remove_after" .IX Subsection "remove_after" .Vb 2 \& my $after = $minion\->remove_after; \& $minion = $minion\->remove_after(86400); .Ve .PP Amount of time in seconds after which jobs that have reached the state \&\f(CW\*(C`finished\*(C'\fR and have no unresolved dependencies will be removed automatically by \&\*(L"repair\*(R", defaults to \f(CW172800\fR (2 days). .SS "tasks" .IX Subsection "tasks" .Vb 2 \& my $tasks = $minion\->tasks; \& $minion = $minion\->tasks({foo => sub {...}}); .Ve .PP Registered tasks. .SH "METHODS" .IX Header "METHODS" Minion inherits all methods from Mojo::EventEmitter and implements the following new ones. .SS "add_task" .IX Subsection "add_task" .Vb 1 \& $minion = $minion\->add_task(foo => sub {...}); .Ve .PP Register a task. .PP .Vb 7 \& # Job with result \& $minion\->add_task(add => sub { \& my ($job, $first, $second) = @_; \& $job\->finish($first + $second); \& }); \& my $id = $minion\->enqueue(add => [1, 1]); \& my $result = $minion\->job($id)\->info\->{result}; .Ve .SS "enqueue" .IX Subsection "enqueue" .Vb 3 \& my $id = $minion\->enqueue(\*(Aqfoo\*(Aq); \& my $id = $minion\->enqueue(foo => [@args]); \& my $id = $minion\->enqueue(foo => [@args] => {priority => 1}); .Ve .PP Enqueue a new job with \f(CW\*(C`inactive\*(C'\fR state. Arguments get serialized by the \&\*(L"backend\*(R" (often with Mojo::JSON), so you shouldn't send objects and be careful with binary data, nested data structures with hash and array references are fine though. .PP These options are currently available: .IP "attempts" 2 .IX Item "attempts" .Vb 1 \& attempts => 25 .Ve .Sp Number of times performing this job will be attempted, with a delay based on \&\*(L"backoff\*(R" after the first attempt, defaults to \f(CW1\fR. .IP "delay" 2 .IX Item "delay" .Vb 1 \& delay => 10 .Ve .Sp Delay job for this many seconds (from now). .IP "parents" 2 .IX Item "parents" .Vb 1 \& parents => [$id1, $id2, $id3] .Ve .Sp One or more existing jobs this job depends on, and that need to have transitioned to the state \f(CW\*(C`finished\*(C'\fR before it can be processed. .IP "priority" 2 .IX Item "priority" .Vb 1 \& priority => 5 .Ve .Sp Job priority, defaults to \f(CW0\fR. Jobs with a higher priority get performed first. .IP "queue" 2 .IX Item "queue" .Vb 1 \& queue => \*(Aqimportant\*(Aq .Ve .Sp Queue to put job in, defaults to \f(CW\*(C`default\*(C'\fR. .SS "job" .IX Subsection "job" .Vb 1 \& my $job = $minion\->job($id); .Ve .PP Get Minion::Job object without making any changes to the actual job or return \f(CW\*(C`undef\*(C'\fR if job does not exist. .PP .Vb 2 \& # Check job state \& my $state = $minion\->job($id)\->info\->{state}; \& \& # Get job result \& my $result = $minion\->job($id)\->info\->{result}; .Ve .SS "new" .IX Subsection "new" .Vb 1 \& my $minion = Minion\->new(Pg => \*(Aqpostgresql://postgres@/test\*(Aq); .Ve .PP Construct a new Minion object. .SS "perform_jobs" .IX Subsection "perform_jobs" .Vb 2 \& $minion\->perform_jobs; \& $minion\->perform_jobs({queues => [\*(Aqimportant\*(Aq]}); .Ve .PP Perform all jobs with a temporary worker, very useful for testing. .PP .Vb 4 \& # Longer version \& my $worker = $minion\->worker; \& while (my $job = $worker\->register\->dequeue(0)) { $job\->perform } \& $worker\->unregister; .Ve .PP These options are currently available: .IP "queues" 2 .IX Item "queues" .Vb 1 \& queues => [\*(Aqimportant\*(Aq] .Ve .Sp One or more queues to dequeue jobs from, defaults to \f(CW\*(C`default\*(C'\fR. .SS "repair" .IX Subsection "repair" .Vb 1 \& $minion = $minion\->repair; .Ve .PP Repair worker registry and job queue if necessary. .SS "reset" .IX Subsection "reset" .Vb 1 \& $minion = $minion\->reset; .Ve .PP Reset job queue. .SS "stats" .IX Subsection "stats" .Vb 1 \& my $stats = $minion\->stats; .Ve .PP Get statistics for jobs and workers. .PP .Vb 2 \& # Check idle workers \& my $idle = $minion\->stats\->{inactive_workers}; .Ve .PP These fields are currently available: .IP "active_jobs" 2 .IX Item "active_jobs" .Vb 1 \& active_jobs => 100 .Ve .Sp Number of jobs in \f(CW\*(C`active\*(C'\fR state. .IP "active_workers" 2 .IX Item "active_workers" .Vb 1 \& active_workers => 100 .Ve .Sp Number of workers that are currently processing a job. .IP "delayed_jobs" 2 .IX Item "delayed_jobs" .Vb 1 \& delayed_jobs => 100 .Ve .Sp Number of jobs in \f(CW\*(C`inactive\*(C'\fR state that are scheduled to run at specific time in the future or have unresolved dependencies. Note that this field is \&\s-1EXPERIMENTAL\s0 and might change without warning! .IP "enqueued_jobs" 2 .IX Item "enqueued_jobs" .Vb 1 \& enqueued_jobs => 100000 .Ve .Sp Rough estimate of how many jobs have ever been enqueued. Note that this field is \&\s-1EXPERIMENTAL\s0 and might change without warning! .IP "failed_jobs" 2 .IX Item "failed_jobs" .Vb 1 \& failed_jobs => 100 .Ve .Sp Number of jobs in \f(CW\*(C`failed\*(C'\fR state. .IP "finished_jobs" 2 .IX Item "finished_jobs" .Vb 1 \& finished_jobs => 100 .Ve .Sp Number of jobs in \f(CW\*(C`finished\*(C'\fR state. .IP "inactive_jobs" 2 .IX Item "inactive_jobs" .Vb 1 \& inactive_jobs => 100 .Ve .Sp Number of jobs in \f(CW\*(C`inactive\*(C'\fR state. .IP "inactive_workers" 2 .IX Item "inactive_workers" .Vb 1 \& inactive_workers => 100 .Ve .Sp Number of workers that are currently not processing a job. .SS "worker" .IX Subsection "worker" .Vb 1 \& my $worker = $minion\->worker; .Ve .PP Build Minion::Worker object. .SH "REFERENCE" .IX Header "REFERENCE" This is the class hierarchy of the Minion distribution. .IP "\(bu" 2 Minion .IP "\(bu" 2 Minion::Backend .RS 2 .IP "\(bu" 2 Minion::Backend::Pg .RE .RS 2 .RE .IP "\(bu" 2 Minion::Command::minion .IP "\(bu" 2 Minion::Command::minion::job .IP "\(bu" 2 Minion::Command::minion::worker .IP "\(bu" 2 Minion::Job .IP "\(bu" 2 Minion::Worker .IP "\(bu" 2 Mojolicious::Plugin::Minion .SH "AUTHOR" .IX Header "AUTHOR" Sebastian Riedel, \f(CW\*(C`sri@cpan.org\*(C'\fR. .SH "CREDITS" .IX Header "CREDITS" In alphabetical order: .Sp .RS 2 Andrey Khozov .Sp Brian Medley .Sp Joel Berger .Sp Paul Williams .RE .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2014\-2017, Sebastian Riedel and others. .PP This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. .SH "SEE ALSO" .IX Header "SEE ALSO" , Mojolicious::Guides, .