Minion - Job queue
use Minion; # Connect to backend my $minion = Minion->new(File => '/home/sri/minion.db'); my $minion = Minion->new(Pg => 'postgresql://postgres@/test'); # Add tasks $minion->add_task(something_slow => sub { my ($job, @args) = @_; sleep 5; say 'This is a background worker process.'; }); # Enqueue jobs $minion->enqueue(something_slow => ['foo', 'bar']); $minion->enqueue(something_slow => [1, 2, 3] => {priority => 5}); # Perform jobs for testing $minion->enqueue(something_slow => ['foo', 'bar']); $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;
Minion is a job queue for the Mojolicious real-time web framework with support for multiple backends, such as DBM::Deep and PostgreSQL.
A job queue allows 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, HTTP downloads, building tarballs, warming caches and basically everything else you can imagine that's not super fast.
use Mojolicious::Lite; plugin Minion => {Pg => 'postgresql://sri:s3cret@localhost/test'}; # Slow task app->minion->add_task(poke_mojo => sub { my $job = shift; $job->app->ua->get('mojolicio.us'); $job->app->log->debug('We have poked mojolicio.us for a visitor'); }); # Perform job in a background worker process get '/' => sub { my $c = shift; $c->minion->enqueue('poke_mojo'); $c->render(text => 'We will poke mojolicio.us for you soon.'); }; app->start;
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.
$ ./myapp.pl minion worker
Jobs can be managed right from the command line with Minion::Command::minion::job.
$ ./myapp.pl minion job
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 "remove_after". 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 "missing_after".
And as your application grows, you can move tasks into application specific plugins.
package MyApp::Task::PokeMojo; use Mojo::Base 'Mojolicious::Plugin'; sub register { my ($self, $app) = @_; $app->minion->add_task(poke_mojo => sub { my $job = shift; $job->app->ua->get('mojolicio.us'); $job->app->log->debug('We have poked mojolicio.us for a visitor'); }); } 1;
Which are loaded like any other plugin from your application.
# Mojolicious $app->plugin('MyApp::Task::PokeMojo'); # Mojolicious::Lite plugin 'MyApp::Task::PokeMojo';
Minion inherits all events from Mojo::EventEmitter and can emit the following new ones.
$minion->on(worker => sub { my ($minion, $worker) = @_; ... });
Emitted in the worker process after it has been created.
$minion->on(worker => sub { my ($minion, $worker) = @_; my $id = $worker->id; say "Worker $$:$id started."; });
Minion implements the following attributes.
my $app = $minion->app; $minion = $minion->app(MyApp->new);
Application for job queue, defaults to a Mojo::HelloWorld object.
my $backend = $minion->backend; $minion = $minion->backend(Minion::Backend::File->new);
Backend, usually a Minion::Backend::File or Minion::Backend::Pg object.
my $after = $minion->missing_after; $minion = $minion->missing_after(172800);
Amount of time in seconds after which workers without a heartbeat will be considered missing and removed from the registry by "repair", defaults to 86400 (1 day).
86400
my $after = $minion->remove_after; $minion = $minion->remove_after(86400);
Amount of time in seconds after which jobs that have reached the state finished will be removed automatically by "repair", defaults to 864000 (10 days).
finished
864000
my $tasks = $minion->tasks; $minion = $minion->tasks({foo => sub {...}});
Registered tasks.
Minion inherits all methods from Mojo::EventEmitter and implements the following new ones.
$minion = $minion->add_task(foo => sub {...});
Register a new task.
my $id = $minion->enqueue('foo'); my $id = $minion->enqueue(foo => [@args]); my $id = $minion->enqueue(foo => [@args] => {priority => 1});
Enqueue a new job with inactive state. Arguments get serialized by the "backend" (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.
inactive
These options are currently available:
delay => 10
Delay job for this many seconds (from now).
priority => 5
Job priority, defaults to 0.
0
queue => 'important'
Queue to put job in, defaults to default.
default
my $job = $minion->job($id);
Get Minion::Job object without making any changes to the actual job or return undef if job does not exist.
undef
# Check job state my $state = $minion->job($id)->info->{state}; # Get job result my $result = $minion->job($id)->info->{result};
my $minion = Minion->new(File => '/home/sri/minion.db');
Construct a new Minion object.
$minion->perform_jobs;
Perform all jobs, very useful for testing.
$minion = $minion->repair;
Repair worker registry and job queue if necessary.
$minion = $minion->reset;
Reset job queue.
my $stats = $minion->stats;
Get statistics for jobs and workers.
my $worker = $minion->worker;
Build Minion::Worker object.
This is the class hierarchy of the Minion distribution.
Minion
Minion::Backend
Minion::Backend::File
Minion::Backend::Pg
Minion::Command::minion
Minion::Command::minion::job
Minion::Command::minion::worker
Minion::Job
Minion::Worker
Mojolicious::Plugin::Minion
Sebastian Riedel, sri@cpan.org.
sri@cpan.org
In alphabetical order:
Andrey Khozov
Brian Medley
Paul Williams
Copyright (C) 2014-2015, Sebastian Riedel.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
https://github.com/kraih/minion, Mojolicious::Guides, http://mojolicio.us.
To install Minion, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Minion
CPAN shell
perl -MCPAN -e shell install Minion
For more information on module installation, please visit the detailed CPAN module installation guide.