The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Minion - Job queue

SYNOPSIS

  use Minion;

  # Connect to backend
  my $minion = Minion->new(Mango => 'mongodb://localhost:27017');

  # 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]);

  # Perform jobs automatically for testing
  $minion->auto_perform(1);
  $minion->enqueue(something_slow => ['foo', 'bar']);

  # Build more sophisticated workers
  my $worker = $minion->repair->worker->register;
  if (my $job = $worker->dequeue) { $job->perform }
  $worker->unregister;

DESCRIPTION

Minion is a job queue for the Mojolicious real-time web framework.

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

Note that this whole distribution is EXPERIMENTAL and will change without warning!

Most of the API is not changing much anymore, but you should wait for a stable 1.0 release before using any of the modules in this distribution in a production environment.

EVENTS

Minion inherits all events from Mojo::EventEmitter and can emit the following new ones.

worker

  $minion->on(worker => sub {
    my ($minion, $worker) = @_;
    ...
  });

Emitted when a new worker is created.

  $minion->on(worker => sub {
    my ($minion, $worker) = @_;
    my $id = $worker->id;
    say "Worker $$:$id started.";
  });

ATTRIBUTES

Minion implements the following attributes.

app

  my $app = $minion->app;
  $minion = $minion->app(MyApp->new);

Application for job queue, defaults to a Mojo::HelloWorld object.

auto_perform

  my $bool = $minion->auto_perform;
  $minion  = $minion->auto_perform($bool);

Perform jobs automatically when a new one has been enqueued with "enqueue", very useful for testing.

backend

  my $backend = $minion->backend;
  $minion     = $minion->backend(Minion::Backend::Mango->new);

Backend.

tasks

  my $tasks = $minion->tasks;
  $minion   = $minion->tasks({foo => sub {...}});

Registered tasks.

METHODS

Minion inherits all methods from Mojo::EventEmitter and implements the following new ones.

add_task

  $minion = $minion->add_task(foo => sub {...});

Register a new task.

enqueue

  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. You can also append a callback to perform operation non-blocking.

  $minion->enqueue(foo => sub {
    my ($minion, $err, $id) = @_;
    ...
  });
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

These options are currently available:

delayed
  delayed => (time + 1) * 1000

Delay job until after this point in time.

priority
  priority => 5

Job priority, defaults to 0.

job

  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.

new

  my $minion = Minion->new(Mango => 'mongodb://127.0.0.1:27017');

Construct a new Minion object.

repair

  $minion = $minion->repair;

Repair worker registry and job queue, all workers on this host should be owned by the same user.

reset

  $minion = $minion->reset;

Reset job queue.

stats

  my $stats = $minion->stats;

Get statistics for jobs and workers.

worker

  my $worker = $minion->worker;

Build Minion::Worker object.

AUTHOR

Sebastian Riedel, sri@cpan.org.

COPYRIGHT AND LICENSE

Copyright (C) 2014, 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.

SEE ALSO

https://github.com/kraih/minion, Mojolicious::Guides, http://mojolicio.us.