| 1 | package Minion::Command::minion::worker; | ||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Command'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Mojo::Util qw(getopt); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | has description => 'Start Minion worker'; | |||||||||
| 7 | 1 | ▶ | has usage => sub { shift->extract_usage }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 10 | 0 | 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 | ▶ | 'L|limit=s' => sub { $_[1] =~ /^(\S+)=(\d+)$/ ? ($status->{limits}{$1} = $2) : warn "Invalid limit option: $_[1]" }, | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 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 | ▶ | $status->{queues} = \@queues if @queues; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 0 | my $log = $self->app->log; | |||||||||
| 28 | 0 | $log->info("Worker $$ started"); | |||||||||
| 29 | 0 | ▶ | $worker->on(dequeue => sub { pop->once(spawn => \&_spawn) }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 30 | 0 | $worker->run; | |||||||||
| 31 | 0 | $log->info("Worker $$ stopped"); | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | sub _spawn { | ||||||||
|
Subroutine
_spawn: not called CC=1 SCAR=6.9
| |||||||||||
| 35 | 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 | 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 <seconds> Worker remote control command interval, | ||||||||||
| 59 | defaults to 10 | ||||||||||
| 60 | -D, --dequeue-timeout <seconds> Maximum amount of time to wait for | ||||||||||
| 61 | jobs, defaults to 5 | ||||||||||
| 62 | --dispatch-interval <seconds> 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> Path to home directory of your | ||||||||||
| 67 | application, defaults to the value of | ||||||||||
| 68 | MOJO_HOME or auto-detection | ||||||||||
| 69 | -I, --heartbeat-interval <seconds> Heartbeat interval, defaults to 300 | ||||||||||
| 70 | -j, --jobs <number> Maximum number of jobs to perform | ||||||||||
| 71 | parallel in forked worker processes | ||||||||||
| 72 | (not including spare processes), | ||||||||||
| 73 | defaults to 4 | ||||||||||
| 74 | -L, --limit <name>=<number> Limit number of jobs for a specific | ||||||||||
| 75 | task | ||||||||||
| 76 | -m, --mode <name> Operating mode for your application, | ||||||||||
| 77 | defaults to the value of | ||||||||||
| 78 | MOJO_MODE/PLACK_ENV or "development" | ||||||||||
| 79 | -q, --queue <name> One or more queues to get jobs from, | ||||||||||
| 80 | defaults to "default" | ||||||||||
| 81 | -R, --repair-interval <seconds> 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> Number of spare worker processes to | ||||||||||
| 86 | reserve for high priority jobs, | ||||||||||
| 87 | defaults to 1 | ||||||||||
| 88 | -S, --spare-min-priority <number> Minimum priority of jobs to use spare | ||||||||||
| 89 | worker processes for, defaults to 1 | ||||||||||
| 90 | |||||||||||
| 91 | =head1 DESCRIPTION | ||||||||||
| 92 | |||||||||||
| 93 | L<Minion::Command::minion::worker> starts a L<Minion> worker. You can have as many workers as you like. | ||||||||||
| 94 | |||||||||||
| 95 | =head1 WORKER SIGNALS | ||||||||||
| 96 | |||||||||||
| 97 | The L<Minion::Command::minion::worker> 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<Minion::Command::minion::worker> 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<Minion::Command::minion::worker> process can be controlled at runtime through L<Minion::Command::minion::job>, | ||||||||||
| 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</"spare"> 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<Minion::Command::minion::worker> inherits all attributes from L<Mojolicious::Command> 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<Minion::Command::minion::worker> inherits all methods from L<Mojolicious::Command> 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<Minion>, L<Minion::Guide>, L<https://minion.pm>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||
| 193 | |||||||||||
| 194 | =cut | ||||||||||