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