GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  MINION (3)

.ds Aq ’

NAME

Minion - Job queue

CONTENTS

SYNOPSIS



  use Minion;

  # Connect to backend
  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;



DESCRIPTION

Minion is a job queue for the Mojolicious <http://mojolicious.org> real-time web framework, with support for multiple named queues, priorities, delayed jobs, job results, retries with backoff, statistics, distributed workers, parallel processing, autoscaling, resource leak protection and multiple backends (such as PostgreSQL <http://www.postgresql.org>).

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, 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(mojolicious.org);
    $job->app->log->debug(We have poked mojolicious.org 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 mojolicious.org 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.

GROWING

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(mojolicious.org);
      $job->app->log->debug(We have poked mojolicious.org 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;



EVENTS

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

    worker



  $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.";
  });



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.

    backend



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



Backend, usually a Minion::Backend::Pg object.

    backoff



  my $cb  = $minion->backoff;
  $minion = $minion->backoff(sub {...});



A callback used to calculate the delay for automatically retried jobs, defaults to (retries ** 4) + 15 (15, 16, 31, 96, 271, 640...), which means that roughly 25 attempts can be made in 21 days.



  $minion->backoff(sub {
    my $retries = shift;
    return ($retries ** 4) + 15 + int(rand 30);
  });



    missing_after



  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 1800 (30 minutes).

    remove_after



  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 172800 (2 days).

    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 task.



  # 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};



    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. 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.

These options are currently available:
attempts


  attempts => 25



Number of times performing this job will be attempted, with a delay based on backoff after the first attempt, defaults to 1.

delay


  delay => 10



Delay job for this many seconds (from now).

priority


  priority => 5



Job priority, defaults to 0.

queue


  queue => important



Queue to put job in, defaults to default.

    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.



  # Check job state
  my $state = $minion->job($id)->info->{state};

  # Get job result
  my $result = $minion->job($id)->info->{result};



    new



  my $minion = Minion->new(Pg => postgresql://postgres@/test);



Construct a new Minion object.

    perform_jobs



  $minion->perform_jobs;
  $minion->perform_jobs({queues => [important]});



Perform all jobs, very useful for testing.

These options are currently available:
queues


  queues => [important]



One or more queues to dequeue jobs from, defaults to default.

    repair



  $minion = $minion->repair;



Repair worker registry and job queue if necessary.

    reset



  $minion = $minion->reset;



Reset job queue.

    stats



  my $stats = $minion->stats;



Get statistics for jobs and workers.



  # Check idle workers
  my $idle = $minion->stats->{inactive_workers};



These fields are currently available:
active_jobs


  active_jobs => 100



Number of jobs in active state.

active_workers


  active_workers => 100



Number of workers that are currently processing a job.

failed_jobs


  failed_jobs => 100



Number of jobs in failed state.

finished_jobs


  finished_jobs => 100



Number of jobs in finished state.

inactive_jobs


  inactive_jobs => 100



Number of jobs in inactive state.

inactive_workers


  inactive_workers => 100



Number of workers that are currently not processing a job.

    worker



  my $worker = $minion->worker;



Build Minion::Worker object.

REFERENCE

This is the class hierarchy of the Minion distribution.
o Minion
o Minion::Backend
o Minion::Backend::Pg
o Minion::Command::minion
o Minion::Command::minion::job
o Minion::Command::minion::worker
o Minion::Job
o Minion::Worker
o Mojolicious::Plugin::Minion

AUTHOR

Sebastian Riedel, sri@cpan.org.

CREDITS

In alphabetical order:

Andrey Khozov

Brian Medley

Paul Williams

COPYRIGHT AND LICENSE

Copyright (C) 2014-2016, Sebastian Riedel and others.

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://mojolicious.org>.
Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 MINION (3) 2016-03-20

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.