blib/lib/Mojolicious/Plugin/Minion/Workers.pm

filter: Statementstmt 10.7% 7 / 65 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 9.0% 1 / 11 Podpod 28.5% 2 / 7 totaltotal 5.5% 10 / 181 CC 32 SCAR 47.9
CC
32
Cov
5%
register
272.0
prefork
56.0
manage
30.0
CRAP 120.6
↑ Summary
1 package Mojolicious::Plugin::Minion::Workers;
2 1 ▶use Mojo::Base 'Mojolicious::Plugin::Minion';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '0.9095';# as to Minion version/10+<child minor>
5  
6 1 has minion => undef, weak=>1;
7 1 has qw(conf);
8  
9 0 ▶sub register {
Subroutine register: not called CC=16 SCAR=56.1
Pod documented
10 0 my ($self, $app, $conf) = @_;
11  
12 0 my $workers = delete $conf->{workers};
13 0 my $manage = delete $conf->{manage};
14 0 ▶ my $tasks = delete $conf->{tasks} || {};
Condition0%0/2
delete $conf->{'tasks'} || {}
l!l
00
MC/DC0%0/1
delete $conf->{'tasks'} || {}
delete $conf->{'tasks'}
Truth table0%0/2
A
A= delete $conf->{'tasks'}
Aresult
11
00
15
16 0 my $helper = $app->renderer->get_helper('minion');
17
18 0 my @backend = keys %$conf;
19 require Carp
20 0 ▶ and Carp::croak("Too many config args for Mojolicious::Plugin::Minion")
Branch: 1 branch
BranchTrueFalseTotal
if @backend > 1 and not $helper 0 0 0
Condition0%0/8
@backend > 1 && !$helper && (require Carp && Carp::croak('Too many config args for Mojolicious::Plugin::Minion'))
!ll&&!rl&&r
000
@backend > 1 and not $helper
!ll&&!rl&&r
000
require Carp and Carp::croak("Too many config args for Mojolicious::Plugin::Minion")
!ll
00
MC/DC0%0/3
@backend > 1 && !$helper && (require Carp && Carp::croak('Too many config args for Mojolicious::Plugin::Minion'))
@backend > 1 not $helper require Carp
Truth table0%0/4
A and not(B) && C
A= @backend > 1B= not $helperC= require Carp
ABCresult
0XX0
10X0
1100
1111
21 if @backend > 1 && !$helper;
22  
23 $conf->{ $backend[0] } = $conf->{ $backend[0] }->($app)
24 0 ▶ if $backend[0] && ref($conf->{ $backend[0] }) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $backend[0] and ref $$conf{$backend[0]} eq "CODE" 0 0 0
Condition0%0/3
$backend[0] and ref $$conf{$backend[0]} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$backend[0] and ref $$conf{$backend[0]} eq "CODE"
$backend[0] ref $$conf{$backend[0]} eq "CODE"
Truth table0%0/3
A and B
A= $backend[0]B= ref $$conf{$backend[0]} eq "CODE"
ABresult
0X0
100
111
25  
26 0 ▶ $self->SUPER::register($app, $conf)
Branch: 1 branch
BranchTrueFalseTotal
if $backend[0] and not $helper 0 0 0
Condition0%0/3
$backend[0] and not $helper
!ll&&!rl&&r
000
MC/DC0%0/2
$backend[0] and not $helper
$backend[0] not $helper
Truth table0%0/3
A and not(B)
A= $backend[0]B= not $helper
ABresult
0X0
100
111
27 if $backend[0] && !$helper;
28  
29 0 $self->minion($app->minion);
30 $self->conf({
31 %$conf,
32 workers => $workers,
33 is_manage => !$ARGV[0]
34 || $ARGV[0] eq 'daemon'
35 || $ARGV[0] eq 'prefork',
36 is_prefork => $ENV{HYPNOTOAD_APP}
37 0 ▶ || ($ARGV[0] && $ARGV[0] eq 'prefork'),
Condition0%0/12
!$ARGV[0] || $ARGV[0] eq 'daemon' || $ARGV[0] eq 'prefork'
l!l&&r!l&&!r
000
not $ARGV[0] or $ARGV[0] eq "daemon"
l!l&&r!l&&!r
000
$ENV{'HYPNOTOAD_APP'} || $ARGV[0] && $ARGV[0] eq 'prefork'
l!l&&r!l&&!r
000
$ARGV[0] and $ARGV[0] eq "prefork"
!ll&&!rl&&r
000
MC/DC0%0/3
!$ARGV[0] || $ARGV[0] eq 'daemon' || $ARGV[0] eq 'prefork'
not $ARGV[0] $ARGV[0] eq "daemon" $ARGV[0] eq 'prefork'
MC/DC0%0/3
$ENV{'HYPNOTOAD_APP'} || $ARGV[0] && $ARGV[0] eq 'prefork'
$ENV{'HYPNOTOAD_APP'} $ARGV[0] $ARGV[0] eq "prefork"
Truth table0%0/4
not(A) or B || C
A= not $ARGV[0]B= $ARGV[0] eq "daemon"C= $ARGV[0] eq 'prefork'
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A || B and C
A= $ENV{'HYPNOTOAD_APP'}B= $ARGV[0]C= $ARGV[0] eq "prefork"
ABCresult
1XX1
0111
00X0
0100
38 });
39  
40 0 ▶ $app->minion->attr('workers'=> sub { $self }, weak=>1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
41
42 0 ▶ while (my ($name, $sub) = each %$tasks) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tasks) 0 0 0
43 0 $app->log->debug(sprintf("Applied Minion task [%s] in [%s] from config", $name, $app->minion->add_task($name => $sub)));
44 }
45
46 $self->manage()
47 0 ▶ and $self->conf->{is_manage} = 0
Branch: 1 branch
BranchTrueFalseTotal
if $manage 0 0 0
Condition0%0/5
$manage && ($self->manage && ($self->conf->{'is_manage'} = 0))
!ll&&!rl&&r
000
$self->manage and $self->conf->{"is_manage"} = 0
!ll
00
MC/DC0%0/2
$manage && ($self->manage && ($self->conf->{'is_manage'} = 0))
$manage $self->manage
Truth table0%0/3
A && B
A= $manageB= $self->manage
ABresult
0X0
100
111
48 if $manage;
49  
50 0 return $self;
51 }
52  
53 0 ▶sub manage {
Subroutine manage: not called CC=5 SCAR=34.0
Pod documented
54 0 my ($self, $workers) = @_;
55 0 my $conf = $self->conf;
56 return
57 0 ▶ unless $conf->{is_manage};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"is_manage"} 0 0 0
58  
59 $workers ||= $conf->{workers}
60 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$workers ||= $conf->{"workers"} or return 0 0 0
Condition0%0/3
$workers ||= $conf->{"workers"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$workers ||= $conf->{"workers"}
$workers $conf->{"workers"}
Truth table0%0/3
A ||= B
A= $workersB= $conf->{"workers"}
ABresult
1X1
011
000
61  
62 0 my $minion = $self->minion;
63  
64 0 ▶ if ($conf->{is_prefork}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{'is_prefork'}) { } 0 0 0
65 0 $self->prefork;
66 } else {
67 0 $self->subprocess;
68 }
69 0 return $self;
70 }
71  
72 # Cases: hypnotoad script/app.pl | perl script/app.pl prefork
73 0 ▶sub prefork {
Subroutine prefork: not called CC=7 SCAR=40.3
Pod undocumented
74 0 my ($self) = @_;
75
76 # case hot deploy and kill -USR2
77 return
78 #~ if $hypnotoad_pid && !$ENV{HYPNOTOAD_STOP};
79 0 ▶ if $ENV{HYPNOTOAD_PID} && !$ENV{HYPNOTOAD_STOP};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HYPNOTOAD_PID"} and not $ENV{"HYPNOTOAD_STOP"} 0 0 0
Condition0%0/3
$ENV{"HYPNOTOAD_PID"} and not $ENV{"HYPNOTOAD_STOP"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"HYPNOTOAD_PID"} and not $ENV{"HYPNOTOAD_STOP"}
$ENV{"HYPNOTOAD_PID"} not $ENV{"HYPNOTOAD_STOP"}
Truth table0%0/3
A and not(B)
A= $ENV{"HYPNOTOAD_PID"}B= not $ENV{"HYPNOTOAD_STOP"}
ABresult
0X0
100
111
80  
81 0 $self->kill_workers;
82
83 0 ▶ return if $ENV{HYPNOTOAD_STOP};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HYPNOTOAD_STOP"} 0 0 0
84  
85 0 my $workers = $self->conf->{workers};
86 0 ▶ while ($workers--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($workers--) 0 0 0
87 0 ▶ defined(my $pid = fork())
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = fork) or die "Can't fork: $!" 0 0 0
88 || die "Can't fork: $!";
89 0 ▶ next if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 0 0 0
90 0 detach();
91 0 $self->worker_run;
92 0 CORE::exit(0);
93 }
94 }
95  
96 # Cases: morbo script/app.pl | perl script/app.pl daemon
97 0 ▶sub subprocess {
Subroutine subprocess: not called CC=1 SCAR=6.9
Pod undocumented
98 0 my ($self) = @_;
99  
100 0 $self->kill_workers;
101  
102 # subprocess allow run/restart worker later inside app worker
103 0 my $subprocess = Mojo::IOLoop::Subprocess->new();
104 $subprocess->run(
105 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 my $subprocess = shift;
107 0 $self->worker_run;
108 0 return $$;
109 },
110 0 ▶ sub {1}
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 );
112 # Dont $subprocess->ioloop->start here!
113 }
114  
115 0 ▶sub worker_run {
Subroutine worker_run: not called CC=1 SCAR=6.9
Pod undocumented
116 0 my ($self) = @_;
117 0 my $minion = $self->minion;
118 0 $ENV{MINION_PID} = $$;
119 0 $0 = "$0 minion worker";
120 0 $minion->app->log->info("Minion worker (pid $$) was starting");
121 0 $minion->worker->run;
122 }
123  
124  
125  
126 0 ▶sub kill_workers {
Subroutine kill_workers: not called CC=4 SCAR=30.0
Pod undocumented
127 0 my ($self, $workers) = @_;
128 0 my $minion = $self->minion;
129 0 ▶ $workers ||= $minion->backend->list_workers(0,1000)->{workers};
Condition0%0/3
$workers ||= $minion->backend->list_workers(0, 1000)->{"workers"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$workers ||= $minion->backend->list_workers(0, 1000)->{"workers"}
$workers $minion->backend->list_workers(0, 1000)->{"workers"}
Truth table0%0/3
A ||= B
A= $workersB= $minion->backend->list_workers(0, 1000)->{"workers"}
ABresult
1X1
011
000
130  
131 kill 'QUIT', $_->{pid}
132 and $minion->app->log->info("Minion worker (pid $_->{pid}) was stopped")
133 0 ▶ for @$workers;
Branch: 1 branch
BranchTrueFalseTotal
kill "QUIT", $_->{"pid"} and $minion->app->log->info(qq[Minion worker (pid $_->{"pid"}) was stopped]) 0 0 0
134 }
135  
136 0 ▶ sub detach {
Subroutine detach: not called CC=4 SCAR=30.0
Pod undocumented
137 #~ chdir("/") || die "can't chdir to /: $!";
138 #~ defined(my $pid = fork()) || die "can't fork: $!";
139 #~ return 0
140 #~ if $pid; # parent
141
142 #~ require POSIX;
143 #~ (POSIX::setsid() != -1) || die "Can't start a new session: $!";
144 0 ▶ open(STDIN, "< /dev/null") || die "Can't read /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "< /dev/null" or die "Can't read /dev/null: $!" 0 0 0
145 0 ▶ open(STDOUT, "> /dev/null") || die "Can't write to /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, "> /dev/null" or die "Can't write to /dev/null: $!" 0 0 0
146 0 ▶ open(STDERR, ">&STDOUT") || die "Can't dup stdout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "Can't dup stdout: $!" 0 0 0
147  
148 #~ return $$;
149 }
150  
151 1 1;
152  
153 __END__
↑ Summary