| 1 | package Mojolicious::Plugin::Minion::Starter; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: start/stop minion workers with the Mojolicious server | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin', -signatures; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 2 | my $app; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 0 | ▶ | has app => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 0 | ▶ | has config => sub { {} }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | has workers => sub { [] }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | ▶ | sub register { | ||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 16 | 1 | my $self = shift; | |||||||||||||||||||
| 17 | 1 | my $app = shift; | |||||||||||||||||||
| 18 | 1 | my $config = shift; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | $self->app($app); | |||||||||||||||||||
| 21 | 1 | $self->config($config); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 1 | $app->log->info('Started ' . __PACKAGE__); | |||||||||||||||||||
| 24 | 1 | $app->hook(before_server_start => $self->before_server_start_hook($config)); | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 1 | ▶ | sub before_server_start_hook{ | ||||||||||||||||||
|
Subroutine
before_server_start_hook: called CC=4 SCAR=16.5
Pod
undocumented
| |||||||||||||||||||||
| 28 | 1 | my $self = shift; | |||||||||||||||||||
| 29 | 1 | ▶ | my $spawn = (shift() || {})->{spawn}; | ||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 1 | ▶ | $spawn //= 1; $spawn = $spawn <= 0 ? 1 : $spawn; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$spawn //= 1
MC/DC0%0/1
$spawn //= 1
$spawn
Truth table50%1/2
A
A= $spawn
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 2 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=20.9
| |||||||||||||||||||||
| 34 | 2 | my ($server, $app) = @_; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 2 | ▶ | if ($self->config->{debug}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 2 | $self->app->log->info(sprintf "Server type is %s, process %d", ref $server, $$); | |||||||||||||||||||
| 38 | 2 | $self->app->log->info(sprintf "Pid of parent of server process is %d", getppid()); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | # Mojo::Server::PSGI + plackup: parent is shell, server is plackup | ||||||||||||||||||||
| 41 | # Mojo::Server::PSGI + starman: parent is starman | ||||||||||||||||||||
| 42 | # Mojo::Server::Daemon morbo: parent is not shell | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 2 | ▶ | if (ref $server eq 'Mojo::Server::Prefork') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 0 | ▶ | $server->on(spawn => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 46 | 0 | my ($server, $pid) = @_; | |||||||||||||||||||
| 47 | 0 | ▶ | $self->spawn_worker if (scalar @{$self->workers} < $spawn); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | }); | |||||||||||||||||||
| 49 | 0 | return; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | 2 | ▶ | if (ref $server eq 'Mojo::Server::Daemon') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 2 | $self->spawn_worker for (0..($spawn - 1)); | |||||||||||||||||||
| 53 | 2 | return; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | 0 | $self->server_ok($server, $self->config->{debug}); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | 1 | } | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | ▶ | sub server_ok { | ||||||||||||||||||
|
Subroutine
server_ok: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 60 | 0 | my $self = shift; | |||||||||||||||||||
| 61 | 0 | ▶ | my $server = ref $_[0] ? ref shift : shift; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | my $verbose = shift; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | if ($server eq 'Mojo::Server::Daemon') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 66 | 0 | $self->app->log->info(sprintf "Ok: %s support server type %s", __PACKAGE__, $server); | |||||||||||||||||||
| 67 | 0 | return 1; | |||||||||||||||||||
| 68 | } elsif ($server eq 'Mojo::Server::Prefork') { | ||||||||||||||||||||
| 69 | 0 | $self->app->log->info(sprintf "Warning: %s does not support server type %s", __PACKAGE__, $server); | |||||||||||||||||||
| 70 | 0 | return; | |||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||
| 72 | 0 | $self->app->log->info(sprintf "%s does not support server type %s", __PACKAGE__, $server); | |||||||||||||||||||
| 73 | 0 | return; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 4 | ▶ | sub spawn_worker { | ||||||||||||||||||
|
Subroutine
spawn_worker: called CC=3 SCAR=14.8
Pod
undocumented
| |||||||||||||||||||||
| 78 | 4 | my $self = shift; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 4 | ▶ | if (my $pid = fork) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 4 | push @{$self->workers}, $pid; | |||||||||||||||||||
| 82 | # push @workers, $pid; | ||||||||||||||||||||
| 83 | 4 | return; | |||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||
| 85 | 0 | ▶ | if ($self->config->{debug}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | $self->app->log->info(sprintf "Starting minion worker %d with parent %d", $$, getppid()); | |||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||
| 88 | 0 | $self->app->log->info("Starting minion worker $$"); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | 0 | $self->app->minion->worker->run; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | for (grep { (kill 0 => $_) && ($$ != $_ ) } @{$self->workers}) { | ||||||||||||||||||
|
Condition0%0/3
kill(0, $_) && $$ != $_
MC/DC0%0/2
kill(0, $_) && $$ != $_
kill(0, $_)
$$ != $_
Truth table0%0/3
A && B
A= kill(0, $_)B= $$ != $_
| |||||||||||||||||||||
| 98 | 0 | ▶ | if (kill HUP => $_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | $self->app->log->info(sprintf 'Stopped minion worker %d', $_); | |||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||
| 101 | 0 | ▶ | $self->app->log->info(sprintf 'Error on stopping minion worker %d: %s', $_, $@) if $self->config->{debug}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 2 | 1; | |||||||||||||||||||