File Coverage

blib/lib/Minion/Command/minion/worker.pm
Criterion Covered Total %
statement 6 21 28.5
branch 0 4 0.0
condition n/a
subroutine 2 6 33.3
pod 1 1 100.0
total 9 32 28.1


line stmt bran cond sub pod time code
1             package Minion::Command::minion::worker;
2 1     1   17392 use Mojo::Base 'Mojolicious::Command';
  1         3  
  1         7  
3              
4 1     1   247 use Mojo::Util qw(getopt);
  1         2  
  1         512  
5              
6             has description => 'Start Minion worker';
7             has usage => sub { shift->extract_usage };
8              
9             sub run {
10 0     0 1   my ($self, @args) = @_;
11              
12 0           my $worker = $self->app->minion->worker;
13 0           my $status = $worker->status;
14             getopt \@args,
15             'C|command-interval=i' => \$status->{command_interval},
16             'D|dequeue-timeout=i' => \$status->{dequeue_timeout},
17             'dispatch-interval=i' => \$status->{dispatch_interval},
18             'I|heartbeat-interval=i' => \$status->{heartbeat_interval},
19             'j|jobs=i' => \$status->{jobs},
20 0 0   0     'L|limit=s' => sub { $_[1] =~ /^(\S+)=(\d+)$/ ? ($status->{limits}{$1} = $2) : warn "Invalid limit option: $_[1]" },
21             'q|queue=s' => \my @queues,
22             'R|repair-interval=i' => \$status->{repair_interval},
23             's|spare=i' => \$status->{spare},
24 0           'S|spare-min-priority=i' => \$status->{spare_min_priority};
25 0 0         $status->{queues} = \@queues if @queues;
26              
27 0           my $log = $self->app->log;
28 0           $log->info("Worker $$ started");
29 0     0     $worker->on(dequeue => sub { pop->once(spawn => \&_spawn) });
  0            
30 0           $worker->run;
31 0           $log->info("Worker $$ stopped");
32             }
33              
34             sub _spawn {
35 0     0     my ($job, $pid) = @_;
36 0           my ($id, $task) = ($job->id, $job->task);
37 0           $job->app->log->debug(qq{Process $pid is performing job "$id" with task "$task"});
38             }
39              
40             1;
41              
42             =encoding utf8
43              
44             =head1 NAME
45              
46             Minion::Command::minion::worker - Minion worker command
47              
48             =head1 SYNOPSIS
49              
50             Usage: APPLICATION minion worker [OPTIONS]
51              
52             ./myapp.pl minion worker
53             ./myapp.pl minion worker -m production -I 15 -C 5 -R 3600 -j 10
54             ./myapp.pl minion worker -q important -q default
55             ./myapp.pl minion worker -L foo=2 -L bar=5
56              
57             Options:
58             -C, --command-interval Worker remote control command interval,
59             defaults to 10
60             -D, --dequeue-timeout Maximum amount of time to wait for
61             jobs, defaults to 5
62             --dispatch-interval Interval for dispatching due
63             schedules, defaults to 30 (set to 0
64             to disable on this worker)
65             -h, --help Show this summary of available options
66             --home Path to home directory of your
67             application, defaults to the value of
68             MOJO_HOME or auto-detection
69             -I, --heartbeat-interval Heartbeat interval, defaults to 300
70             -j, --jobs Maximum number of jobs to perform
71             parallel in forked worker processes
72             (not including spare processes),
73             defaults to 4
74             -L, --limit = Limit number of jobs for a specific
75             task
76             -m, --mode Operating mode for your application,
77             defaults to the value of
78             MOJO_MODE/PLACK_ENV or "development"
79             -q, --queue One or more queues to get jobs from,
80             defaults to "default"
81             -R, --repair-interval Repair interval, up to half of this
82             value can be subtracted randomly to
83             make sure not all workers repair at the
84             same time, defaults to 21600 (6 hours)
85             -s, --spare Number of spare worker processes to
86             reserve for high priority jobs,
87             defaults to 1
88             -S, --spare-min-priority Minimum priority of jobs to use spare
89             worker processes for, defaults to 1
90              
91             =head1 DESCRIPTION
92              
93             L starts a L worker. You can have as many workers as you like.
94              
95             =head1 WORKER SIGNALS
96              
97             The L process can be controlled at runtime with the following signals.
98              
99             =head2 INT, TERM
100              
101             Stop gracefully after finishing the current jobs.
102              
103             =head2 QUIT
104              
105             Stop immediately without finishing the current jobs.
106              
107             =head1 JOB SIGNALS
108              
109             The job processes spawned by the L process can be controlled at runtime with the
110             following signals.
111              
112             =head2 INT, TERM
113              
114             This signal starts out with the operating system default and allows for jobs to install a custom signal handler to stop
115             gracefully.
116              
117             =head2 USR1, USR2
118              
119             These signals start out being ignored and allow for jobs to install custom signal handlers.
120              
121             =head1 REMOTE CONTROL COMMANDS
122              
123             The L process can be controlled at runtime through L,
124             from anywhere in the network, by broadcasting the following remote control commands.
125              
126             =head2 jobs
127              
128             $ ./myapp.pl minion job -b jobs -a '[10]'
129             $ ./myapp.pl minion job -b jobs -a '[10]' 23
130              
131             Instruct one or more workers to change the number of jobs to perform concurrently. Setting this value to C<0> will
132             effectively pause the worker. That means all current jobs will be finished, but no new ones accepted, until the number
133             is increased again. Note that L might need to be adjusted if high priority jobs should be paused as well.
134              
135             =head2 kill
136              
137             $ ./myapp.pl minion job -b kill -a '["INT", 10025]'
138             $ ./myapp.pl minion job -b kill -a '["INT", 10025]' 23
139              
140             Instruct one or more workers to send a signal to a job that is currently being performed. This command will be ignored
141             by workers that do not have a job matching the id. That means it is safe to broadcast this command to all workers.
142              
143             =head2 spare
144              
145             $ ./myapp.pl minion job -b spare -a '[1]'
146             $ ./myapp.pl minion job -b spare -a '[1]' 23
147              
148             Instruct one or more workers to change the number of spare worker processes to reserve for high priority jobs. Setting
149             this value to C<0> will effectively disable the feature, this is required to pause the worker. That means all current
150             high priority jobs will be finished, but no new ones accepted, until the number is increased again.
151              
152             =head2 stop
153              
154             $ ./myapp.pl minion job -b stop -a '[10025]'
155             $ ./myapp.pl minion job -b stop -a '[10025]' 23
156              
157             Instruct one or more workers to stop a job that is currently being performed immediately. This command will be ignored
158             by workers that do not have a job matching the id. That means it is safe to broadcast this command to all workers.
159              
160             =head1 ATTRIBUTES
161              
162             L inherits all attributes from L and implements the following
163             new ones.
164              
165             =head2 description
166              
167             my $description = $worker->description;
168             $worker = $worker->description('Foo');
169              
170             Short description of this command, used for the command list.
171              
172             =head2 usage
173              
174             my $usage = $worker->usage;
175             $worker = $worker->usage('Foo');
176              
177             Usage information for this command, used for the help screen.
178              
179             =head1 METHODS
180              
181             L inherits all methods from L and implements the following new
182             ones.
183              
184             =head2 run
185              
186             $worker->run(@ARGV);
187              
188             Run this command.
189              
190             =head1 SEE ALSO
191              
192             L, L, L, L, L.
193              
194             =cut