File Coverage

blib/lib/Beam/Minion.pm
Criterion Covered Total %
statement 15 20 75.0
branch n/a
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 22 28 78.5


line stmt bran cond sub pod time code
1             package Beam::Minion;
2             our $VERSION = '0.019';
3             # ABSTRACT: A distributed task runner for Beam::Wire containers
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod # Command-line interface
8             #pod export BEAM_MINION=sqlite://test.db # Configure a DSN directly
9             #pod export BEAM_MINION=file:///etc/beam/minion.yml # Configuration file
10             #pod beam minion worker
11             #pod beam minion run [...]
12             #pod beam minion help
13             #pod
14             #pod # Perl interface
15             #pod Beam::Minion->init(%config)
16             #pod Beam::Minion->enqueue( $container, $service, \@args, \%opt );
17             #pod
18             #pod =head1 DESCRIPTION
19             #pod
20             #pod L is a distributed task runner. One or more workers are
21             #pod created to run tasks, and then each task is sent to a worker to be run.
22             #pod Tasks are configured as L objects by L
23             #pod container files.
24             #pod
25             #pod =head1 GETTING STARTED
26             #pod
27             #pod =head2 Configure Minion
28             #pod
29             #pod To start running your L jobs, you must first start
30             #pod a L worker with the L
31             #pod worker.command|Beam::Minion::Command::worker>. Minion requires
32             #pod a database to coordinate workers, and communicates with this database
33             #pod using a L.
34             #pod
35             #pod The supported Minion backends are:
36             #pod
37             #pod =over
38             #pod
39             #pod =item *
40             #pod
41             #pod L - C<< sqlite: >>
42             #pod
43             #pod =item *
44             #pod
45             #pod L - C<< postgresql://:@/ >>
46             #pod
47             #pod =item *
48             #pod
49             #pod L - C<< mysql://:@/ >>
50             #pod
51             #pod =item *
52             #pod
53             #pod L - C<< mongodb://: >>
54             #pod
55             #pod =back
56             #pod
57             #pod Minion will automatically deploy the
58             #pod database tables it needs, so be sure to allow the right permissions (if
59             #pod the database has such things).
60             #pod
61             #pod In order to communicate with Minion workers on other machines, it will
62             #pod be necessary to use a database accessible from the network (so, not
63             #pod SQLite).
64             #pod
65             #pod =head3 Environment variable
66             #pod
67             #pod Once you've picked a database backend, configure the C
68             #pod environment variable with the URL. This could leave the password in the environment variable
69             #pod which could leak out.
70             #pod
71             #pod =head3 Configuration File
72             #pod
73             #pod To read configuration from a file, set the C environment variable
74             #pod to a URL starting with C and then a path to a YAML file containing a
75             #pod hash of the configuration passed to L.
76             #pod
77             #pod =head2 Start a Worker
78             #pod
79             #pod Once the C environment variable is set, you can start
80             #pod a worker with C<< beam minion worker >>. Each worker can run jobs from
81             #pod all the containers it can find from the C environment
82             #pod variable. Each worker will run up to 4 jobs concurrently.
83             #pod
84             #pod =head2 Spawn a Job
85             #pod
86             #pod Jobs are spawned with C<< beam minion run >>.
87             #pod The C must be an object that consumes the L
88             #pod role. C should be a path to a container file and can be
89             #pod an absolute path, a path relative to the current directory, or a
90             #pod path relative to one of the paths in the C environment
91             #pod variable (separated by C<:>).
92             #pod
93             #pod You can queue up jobs before you have workers running. As soon as
94             #pod a worker is available, it will start running jobs from the queue.
95             #pod
96             #pod =head1 SEE ALSO
97             #pod
98             #pod L, L, L
99             #pod
100             #pod =cut
101              
102 3     3   131955 use strict;
  3         6  
  3         100  
103 3     3   9 use warnings;
  3         4  
  3         152  
104 3     3   606 use Mojo::Base -strict, -signatures;
  3         10176  
  3         32  
105 3     3   8682 use Beam::Minion::Util qw( minion minion_init_args );
  3         10  
  3         647  
106              
107             #pod =sub init
108             #pod
109             #pod Beam::Minion->init(%config)
110             #pod
111             #pod Set the configuration for Beam::Minion. This overrides the C environment
112             #pod variable to allow setting configuration from any kind of source. The configuration should
113             #pod be a hash with a single key for the L backend to use, and a value to be
114             #pod passed to that backend's constructor (same as the arguments to L).
115             #pod
116             #pod =cut
117              
118 0     0 1 0 sub init ($class, %config) {
  0         0  
  0         0  
  0         0  
119 0         0 minion_init_args(%config);
120             }
121              
122             #pod =sub enqueue
123             #pod
124             #pod Beam::Minion->enqueue( $container_name, $task_name, \@args, \%opt );
125             #pod
126             #pod Enqueue the task named C<$task_name> from the container named C<$container_name>.
127             #pod The C environment variable must be set.
128             #pod
129             #pod C<\%opt> is a hash reference with the following keys:
130             #pod
131             #pod =over
132             #pod
133             #pod =item attempts
134             #pod
135             #pod Number of times to retry this job if it fails. Defaults to C<1>.
136             #pod
137             #pod =item delay
138             #pod
139             #pod Time (in seconds) to delay this job (from now). Defaults to C<0>.
140             #pod
141             #pod =item priority
142             #pod
143             #pod The job priority. Higher priority jobs get performed first. Defaults to C<0>.
144             #pod
145             #pod =back
146             #pod
147             #pod (These are the same options allowed in L
148             #pod method|http://mojolicious.org/perldoc/Minion#enqueue1>)
149             #pod
150             #pod =cut
151              
152             sub enqueue {
153 3     3 1 11 my ( $class, $container, $task, $args, $opt ) = @_;
154 3         15 my $minion = minion();
155 2         86510 $minion->enqueue( "$container:$task", $args, $opt );
156             }
157              
158             1;
159              
160             __END__