File Coverage

blib/lib/Mojolicious/Plugin/Minion/Starter.pm
Criterion Covered Total %
statement 28 55 50.9
branch 5 26 19.2
condition 2 4 50.0
subroutine 5 7 71.4
pod 1 4 25.0
total 41 96 42.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Minion::Starter;
2              
3             # ABSTRACT: start/stop minion workers with the Mojolicious server
4              
5 2     2   87395 use Mojo::Base 'Mojolicious::Plugin', -signatures;
  2         159885  
  2         14  
6              
7             my $app;
8              
9             has app => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') };
10              
11             has config => sub { {} };
12              
13             has workers => sub { [] };
14              
15             sub register {
16 1     1 1 33 my $self = shift;
17 1         2 my $app = shift;
18 1         3 my $config = shift;
19              
20 1         3 $self->app($app);
21 1         9 $self->config($config);
22              
23 1         10 $app->log->info('Started ' . __PACKAGE__);
24 1         190 $app->hook(before_server_start => $self->before_server_start_hook($config));
25             }
26              
27             sub before_server_start_hook{
28 1     1 0 3 my $self = shift;
29 1   50     4 my $spawn = (shift() || {})->{spawn};
30              
31 1 50 50     3 $spawn //= 1; $spawn = $spawn <= 0 ? 1 : $spawn;
  1         3  
32              
33             sub {
34 2     2   2006735 my ($server, $app) = @_;
35              
36 2 50       11 if ($self->config->{debug}) {
37 2         29 $self->app->log->info(sprintf "Server type is %s, process %d", ref $server, $$);
38 2         86 $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 50       55 if (ref $server eq 'Mojo::Server::Prefork') {
45             $server->on(spawn => sub {
46 0         0 my ($server, $pid) = @_;
47 0 0       0 $self->spawn_worker if (scalar @{$self->workers} < $spawn);
  0         0  
48 0         0 });
49 0         0 return;
50             }
51 2 50       7 if (ref $server eq 'Mojo::Server::Daemon') {
52 2         11 $self->spawn_worker for (0..($spawn - 1));
53 2         56 return;
54             }
55 0         0 $self->server_ok($server, $self->config->{debug});
56             }
57 1         9 }
58              
59             sub server_ok {
60 0     0 0 0 my $self = shift;
61 0 0       0 my $server = ref $_[0] ? ref shift : shift;
62              
63 0         0 my $verbose = shift;
64              
65 0 0       0 if ($server eq 'Mojo::Server::Daemon') {
    0          
66 0         0 $self->app->log->info(sprintf "Ok: %s support server type %s", __PACKAGE__, $server);
67 0         0 return 1;
68             } elsif ($server eq 'Mojo::Server::Prefork') {
69 0         0 $self->app->log->info(sprintf "Warning: %s does not support server type %s", __PACKAGE__, $server);
70 0         0 return;
71             } else {
72 0         0 $self->app->log->info(sprintf "%s does not support server type %s", __PACKAGE__, $server);
73 0         0 return;
74             }
75             }
76              
77             sub spawn_worker {
78 4     4 0 9 my $self = shift;
79              
80 4 50       4787 if (my $pid = fork) {
81 4         42 push @{$self->workers}, $pid;
  4         445  
82             # push @workers, $pid;
83 4         161 return;
84             } else {
85 0 0         if ($self->config->{debug}) {
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             sub DESTROY {
95 0     0     my $self = shift;
96              
97 0 0         for (grep { (kill 0 => $_) && ($$ != $_ ) } @{$self->workers}) {
  0            
  0            
98 0 0         if (kill HUP => $_) {
99 0           $self->app->log->info(sprintf 'Stopped minion worker %d', $_);
100             } else {
101 0 0         $self->app->log->info(sprintf 'Error on stopping minion worker %d: %s', $_, $@) if $self->config->{debug};
102             }
103             }
104             }
105              
106             1;