| 1 | package Proclet; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 31 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 31 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 31 | ▶ | use Parallel::Prefork 0.13; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 31 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 31 | ▶ | use Data::Validator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 31 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 31 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 31 | ▶ | use Log::Minimal env_debug => 'PROCLET_DEBUG'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 31 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 31 | ▶ | use Term::ANSIColor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 31 | ▶ | use File::Which; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 31 | ▶ | use Time::Crontab; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 31 | ▶ | use String::ShellQuote; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | subtype 'ServiceProcs' | ||||||||||||||||||||||||||||
| 18 | => as 'Int' | ||||||||||||||||||||||||||||
| 19 | 54 | ▶ | => where { $_ >= 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | subtype 'ServicePort' | ||||||||||||||||||||||||||||
| 22 | => as 'Int' | ||||||||||||||||||||||||||||
| 23 | 9 | ▶ | => where { $_ > 0 && $_ % 1000 == 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | subtype 'Proclet::Service' | ||||||||||||||||||||||||||||
| 26 | => as 'HashRef' | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | => message { "This argument must be String or ArrayRef or CodeRef" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 28 | coerce 'Proclet::Service' | ||||||||||||||||||||||||||||
| 29 | 18 | ▶ | => from 'ArrayRef' => via { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.3
| |||||||||||||||||||||||||||||
| 30 | 18 | my $command = $_; | |||||||||||||||||||||||||||
| 31 | 24 | ▶ | +{generator => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 32 | 24 | my $notice = shift; | |||||||||||||||||||||||||||
| 33 | 24 | my @command = @{$command}; | |||||||||||||||||||||||||||
| 34 | 24 | my @o_command = @command; | |||||||||||||||||||||||||||
| 35 | 24 | my $bash = which("bash"); | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ( @command == 1 && $bash ) { unshift @command, $bash, "-c" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@command == 1 and $bash
MC/DC0%0/2
@command == 1 and $bash
@command == 1
$bash
Truth table33%1/3
A and B
A= @command == 1B= $bash
| |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 38 | 3 | ▶ | infof "Exec command: ". shell_quote(@o_command)."\n" if $notice; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 3 | exec(@command); | |||||||||||||||||||||||||||
| 40 | 0 | die $! | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 24 | }} | |||||||||||||||||||||||||||
| 43 | 18 | } | |||||||||||||||||||||||||||
| 44 | 18 | ▶ | => from 'Str' => via { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||
| 45 | 18 | my $o_command = $_; | |||||||||||||||||||||||||||
| 46 | 23 | ▶ | +{generator => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 47 | 23 | my ($notice, $port, $tag) = @_; | |||||||||||||||||||||||||||
| 48 | 23 | my $command = $o_command; #copy | |||||||||||||||||||||||||||
| 49 | 23 | ▶ | $command =~ s/\$PORT/$port/g if $port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 23 | my @command = ($command); | |||||||||||||||||||||||||||
| 51 | 23 | my @o_command = @command; | |||||||||||||||||||||||||||
| 52 | 23 | ▶ | if ( my $bash = which("bash") ) { unshift @command, $bash, "-c" } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 5 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | infof "Exec command: ". shell_quote(@o_command)."\n" if $notice; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 5 | exec(@command); | |||||||||||||||||||||||||||
| 56 | 0 | die $! | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 23 | }} | |||||||||||||||||||||||||||
| 59 | 18 | } | |||||||||||||||||||||||||||
| 60 | 21 | ▶ | => from 'CodeRef' => via { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 21 | my $command = $_; | |||||||||||||||||||||||||||
| 62 | 48 | ▶ | +{generator => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 48 | my ($notice, $port, $tag) = @_; | |||||||||||||||||||||||||||
| 64 | 7 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 65 | 7 | ▶ | infof "Start callback: " . $tag . "\n" if $notice; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 7 | $command->($port); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 21 | }}; | |||||||||||||||||||||||||||
| 69 | 31 | }; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | subtype 'Proclet::Scheduler' | ||||||||||||||||||||||||||||
| 73 | => as 'CodeRef' | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | => message { "This argument must be String or CodeRef" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 75 | coerce 'Proclet::Scheduler' | ||||||||||||||||||||||||||||
| 76 | 6 | ▶ | => from 'Str' => via { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 77 | 6 | my $str = $_; | |||||||||||||||||||||||||||
| 78 | 6 | my $crontab = Time::Crontab->new($str); | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | 0 | my $unixtime = shift; | |||||||||||||||||||||||||||
| 81 | 0 | $crontab->match($unixtime); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 6 | }; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 31 | ▶ | no Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 31 | our $VERSION = '0.35'; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | has '_services' => ( | ||||||||||||||||||||||||||||
| 91 | is => 'ro', | ||||||||||||||||||||||||||||
| 92 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 93 | 27 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | 31 | ); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 31 | has 'spawn_interval' => ( | |||||||||||||||||||||||||||
| 97 | is => 'ro', | ||||||||||||||||||||||||||||
| 98 | isa => 'Int', | ||||||||||||||||||||||||||||
| 99 | default => 0, | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 31 | has 'err_respawn_interval' => ( | |||||||||||||||||||||||||||
| 103 | is => 'ro', | ||||||||||||||||||||||||||||
| 104 | isa => 'Int', | ||||||||||||||||||||||||||||
| 105 | default => 1, | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 31 | has 'color' => ( | |||||||||||||||||||||||||||
| 109 | is => 'rw', | ||||||||||||||||||||||||||||
| 110 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 111 | default => 0, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 31 | has 'logger' => ( | |||||||||||||||||||||||||||
| 115 | is => 'ro', | ||||||||||||||||||||||||||||
| 116 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 117 | required => 0, | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 31 | has 'enable_log_worker' => ( | |||||||||||||||||||||||||||
| 121 | is => 'ro', | ||||||||||||||||||||||||||||
| 122 | isa => 'Int', | ||||||||||||||||||||||||||||
| 123 | default => 1, | ||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 31 | has 'exec_notice' => ( | |||||||||||||||||||||||||||
| 127 | is => 'ro', | ||||||||||||||||||||||||||||
| 128 | isa => 'Int', | ||||||||||||||||||||||||||||
| 129 | default => 1, | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # for Procfile port assignment | ||||||||||||||||||||||||||||
| 133 | 31 | has '_base_port' => ( | |||||||||||||||||||||||||||
| 134 | is => 'ro', | ||||||||||||||||||||||||||||
| 135 | isa => 'ServicePort', | ||||||||||||||||||||||||||||
| 136 | required => 0, | ||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 31 | my $rule = Data::Validator->new( | |||||||||||||||||||||||||||
| 140 | code => { isa => 'Proclet::Service', coerce => 1 }, | ||||||||||||||||||||||||||||
| 141 | worker => { isa => 'ServiceProcs', default => 1 }, | ||||||||||||||||||||||||||||
| 142 | tag => { isa => 'Str', optional => 1 }, | ||||||||||||||||||||||||||||
| 143 | every => { isa => 'Proclet::Scheduler', coerce => 1, optional => 1 }, | ||||||||||||||||||||||||||||
| 144 | )->with('Method'); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 31 | our @COLORS = qw/green magenta blue yellow cyan/; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 57 | ▶ | sub service { | ||||||||||||||||||||||||||
|
Subroutine
service: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 57 | my($self, $args) = $rule->validate(@_); | |||||||||||||||||||||||||||
| 150 | 57 | ▶ | $self->{service_num} ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"service_num"} ||= 0
MC/DC100%1/1
$self->{"service_num"} ||= 0
$self->{"service_num"}
Truth table100%2/2
A
A= $self->{"service_num"}
| |||||||||||||||||||||||||||||
| 151 | 57 | $self->{service_num}++; | |||||||||||||||||||||||||||
| 152 | 57 | ▶ | $self->{tags} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"tags"} ||= {}
MC/DC100%1/1
$self->{"tags"} ||= {}
$self->{"tags"}
Truth table100%2/2
A
A= $self->{"tags"}
| |||||||||||||||||||||||||||||
| 153 | 57 | ▶ | my $tag = ( exists $args->{tag} && defined $args->{tag} ) ? $args->{tag} : $self->{service_num}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args->{"tag"} and defined $args->{"tag"}
MC/DC50%1/2
exists $args->{"tag"} and defined $args->{"tag"}
exists $args->{"tag"}
defined $args->{"tag"}
Truth table66%2/3
A and B
A= exists $args->{"tag"}B= defined $args->{"tag"}
| |||||||||||||||||||||||||||||
| 154 | 57 | ▶ | die "tag: $tag is already exists" if exists $self->{tags}->{$tag}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 57 | $self->{tags}->{$tag} = 1; | |||||||||||||||||||||||||||
| 156 | 57 | my $port = 0; | |||||||||||||||||||||||||||
| 157 | 57 | ▶ | if ( $self->{_base_port} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 18 | $port = $self->{_base_port}; | |||||||||||||||||||||||||||
| 159 | 18 | $self->{_base_port} += 100; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 57 | ▶ | my $cron = ( exists $args->{every} && defined $args->{every} ) ? $args->{every} : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args->{"every"} and defined $args->{"every"}
MC/DC50%1/2
exists $args->{"every"} and defined $args->{"every"}
exists $args->{"every"}
defined $args->{"every"}
Truth table66%2/3
A and B
A= exists $args->{"every"}B= defined $args->{"every"}
| |||||||||||||||||||||||||||||
| 162 | 57 | push @{$self->_services}, { | |||||||||||||||||||||||||||
| 163 | code => $args->{code}, | ||||||||||||||||||||||||||||
| 164 | worker => $args->{worker}, | ||||||||||||||||||||||||||||
| 165 | tag => $tag, | ||||||||||||||||||||||||||||
| 166 | start_port => $port, | ||||||||||||||||||||||||||||
| 167 | 57 | color => $COLORS[ $self->{service_num} % @COLORS ], | |||||||||||||||||||||||||||
| 168 | cron => $cron | ||||||||||||||||||||||||||||
| 169 | }; | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 31 | my $LOGGER = '__log__'; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 27 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 27 | my $self = shift; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 27 | my %services; | |||||||||||||||||||||||||||
| 178 | my @services; | ||||||||||||||||||||||||||||
| 179 | 27 | for my $service ( @{$self->_services} ) { | |||||||||||||||||||||||||||
| 180 | 57 | my $worker = $service->{worker}; | |||||||||||||||||||||||||||
| 181 | 57 | ▶ | for ( my $i = 1; $i <= $worker; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 95 | my $sid = $service->{tag} . '.' . $i; | |||||||||||||||||||||||||||
| 183 | 95 | $services{$sid} = { %$service }; | |||||||||||||||||||||||||||
| 184 | 95 | my $port = $services{$sid}{start_port} + $i - 1; | |||||||||||||||||||||||||||
| 185 | 95 | my $code_generator = $services{$sid}{code}{generator}; | |||||||||||||||||||||||||||
| 186 | 95 | my $code = $code_generator->($self->exec_notice, $port, $service->{tag}); | |||||||||||||||||||||||||||
| 187 | 95 | ▶ | if ( $services{$sid}{cron} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 6 | $code = $self->cron_worker($code,$services{$sid}{cron}); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 95 | $services{$sid}{code} = $code; | |||||||||||||||||||||||||||
| 191 | 95 | ▶ | if ( $self->enable_log_worker ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 92 | $services{$sid}->{pipe} = $self->create_pipe; | |||||||||||||||||||||||||||
| 193 | }; | ||||||||||||||||||||||||||||
| 194 | 95 | push @services, $sid; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 27 | ▶ | croak('no services exists') if ! @services; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 27 | ▶ | if ( $self->enable_log_worker ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 24 | $services{$LOGGER} = { | |||||||||||||||||||||||||||
| 201 | code => $self->log_worker(), | ||||||||||||||||||||||||||||
| 202 | }; | ||||||||||||||||||||||||||||
| 203 | 24 | push @services, $LOGGER; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 27 | my $next; | |||||||||||||||||||||||||||
| 207 | my %pid2service; | ||||||||||||||||||||||||||||
| 208 | 27 | my %running; | |||||||||||||||||||||||||||
| 209 | 27 | my $wait_all_children; | |||||||||||||||||||||||||||
| 210 | my $pm = Parallel::Prefork->new({ | ||||||||||||||||||||||||||||
| 211 | spawn_interval => $self->spawn_interval, | ||||||||||||||||||||||||||||
| 212 | err_respawn_interval => $self->err_respawn_interval, | ||||||||||||||||||||||||||||
| 213 | max_workers => scalar @services, | ||||||||||||||||||||||||||||
| 214 | trap_signals => { | ||||||||||||||||||||||||||||
| 215 | TERM => 'TERM', | ||||||||||||||||||||||||||||
| 216 | HUP => 'TERM', | ||||||||||||||||||||||||||||
| 217 | INT => 'INT', | ||||||||||||||||||||||||||||
| 218 | }, | ||||||||||||||||||||||||||||
| 219 | 33 | ▶ | on_child_reap => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 220 | 33 | my ( $pm, $exit_pid, $status ) = @_; | |||||||||||||||||||||||||||
| 221 | 33 | local $Log::Minimal::AUTODUMP = 1; | |||||||||||||||||||||||||||
| 222 | debugf "[Proclet] on child reap: exit_pid => %s status => %s, service => %s", | ||||||||||||||||||||||||||||
| 223 | 33 | ▶ | $exit_pid, $status, exists $pid2service{$exit_pid} ? $pid2service{$exit_pid} : 'undefined'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 33 | ▶ | if ( exists $pid2service{$exit_pid} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 33 | my $sid = $pid2service{$exit_pid}; | |||||||||||||||||||||||||||
| 226 | 33 | delete $running{$sid}; | |||||||||||||||||||||||||||
| 227 | 33 | delete $pid2service{$exit_pid}; | |||||||||||||||||||||||||||
| 228 | 33 | ▶ | if ( $wait_all_children ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 25 | ▶ | if ( scalar keys %running == 1 && exists $running{$LOGGER} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar keys %running == 1 and exists $running{$LOGGER}
MC/DC50%1/2
scalar keys %running == 1 and exists $running{$LOGGER}
scalar keys %running == 1
exists $running{$LOGGER}
Truth table66%2/3
A and B
A= scalar keys %running == 1B= exists $running{$LOGGER}
| |||||||||||||||||||||||||||||
| 230 | 5 | kill 'TERM', $running{$LOGGER}; | |||||||||||||||||||||||||||
| 231 | 5 | ▶ | sleep(1) && kill 'TERM', $running{$LOGGER}; #safe | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | 33 | debugf "[Proclet] on_child_reap: running => %s", \%running; | |||||||||||||||||||||||||||
| 236 | }, | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 88 | ▶ | before_fork => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 239 | 88 | local $Log::Minimal::AUTODUMP = 1; | |||||||||||||||||||||||||||
| 240 | 88 | debugf "[Proclet] before_fork: running => %s", \%running; | |||||||||||||||||||||||||||
| 241 | 88 | my $pm = shift; | |||||||||||||||||||||||||||
| 242 | 88 | ▶ | if ( $self->enable_log_worker && ! exists $running{$LOGGER} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->enable_log_worker and not exists $running{$LOGGER}
MC/DC100%2/2
$self->enable_log_worker and not exists $running{$LOGGER}
$self->enable_log_worker
not exists $running{$LOGGER}
Truth table100%3/3
A and not(B)
A= $self->enable_log_workerB= not exists $running{$LOGGER}
| |||||||||||||||||||||||||||||
| 243 | 24 | $next = $LOGGER; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | else { | ||||||||||||||||||||||||||||
| 246 | 64 | for my $sid ( @services ) { | |||||||||||||||||||||||||||
| 247 | 139 | ▶ | if ( ! exists $running{$sid} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 64 | $next = $sid; | |||||||||||||||||||||||||||
| 249 | 64 | last; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | 88 | debugf "[Proclet] before_fork: next => %s", $next; | |||||||||||||||||||||||||||
| 254 | }, | ||||||||||||||||||||||||||||
| 255 | 67 | ▶ | after_fork => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 256 | 67 | my ($pm, $pid) = @_; | |||||||||||||||||||||||||||
| 257 | 67 | local $Log::Minimal::AUTODUMP = 1; | |||||||||||||||||||||||||||
| 258 | 67 | ▶ | if ( defined $next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 67 | debugf "[Proclet] child start: sid =>%s", $next; | |||||||||||||||||||||||||||
| 260 | 67 | $pid2service{$pid} = $next; | |||||||||||||||||||||||||||
| 261 | 67 | $running{$next} = $pid; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | else { | ||||||||||||||||||||||||||||
| 264 | 0 | debugf "[Proclet] child start but next is undefined"; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | 67 | $next = undef; | |||||||||||||||||||||||||||
| 267 | }, | ||||||||||||||||||||||||||||
| 268 | 27 | }); | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 27 | ▶ | while ($pm->signal_received !~ m!^(?:TERM|INT)$! ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 21 | ▶ | $pm->start( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||
| 272 | 21 | ▶ | if ( defined $next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 21 | my $service = delete $services{$next}; | |||||||||||||||||||||||||||
| 274 | 21 | ▶ | if ( ! $self->enable_log_worker ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 275 | 2 | $service->{code}->(); | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | elsif ( $service->{pipe} ) { | ||||||||||||||||||||||||||||
| 278 | 14 | undef %services; | |||||||||||||||||||||||||||
| 279 | 14 | my $logwh = $service->{pipe}->[1]; | |||||||||||||||||||||||||||
| 280 | 14 | close $service->{pipe}->[0]; | |||||||||||||||||||||||||||
| 281 | 14 | ▶ | open STDOUT, '>&', $logwh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | or die "Died: failed to redirect STDOUT"; | ||||||||||||||||||||||||||||
| 283 | 14 | ▶ | open STDERR, '>&', $logwh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | or die "Died: failed to redirect STDERR"; | ||||||||||||||||||||||||||||
| 285 | 14 | $service->{code}->(); | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | else { | ||||||||||||||||||||||||||||
| 288 | # logworker | ||||||||||||||||||||||||||||
| 289 | 5 | $service->{code}->(\%services); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | else { | ||||||||||||||||||||||||||||
| 293 | 0 | local $Log::Minimal::AUTODUMP = 1; | |||||||||||||||||||||||||||
| 294 | 0 | debugf "[Proclet] child (pid=>%s) start but next is undefined",$$; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | 27 | }); | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | 6 | $wait_all_children = 1; | |||||||||||||||||||||||||||
| 299 | 6 | $pm->wait_all_children(); | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 92 | ▶ | sub create_pipe { | ||||||||||||||||||||||||||
|
Subroutine
create_pipe: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 303 | 92 | my $self = shift; | |||||||||||||||||||||||||||
| 304 | 92 | ▶ | pipe my $logrh, my $logwh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | or die "Died: failed to create pipe:$!"; | ||||||||||||||||||||||||||||
| 306 | 92 | return [$logrh, $logwh]; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 24 | ▶ | sub log_worker { | ||||||||||||||||||||||||||
|
Subroutine
log_worker: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 310 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 311 | 5 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 312 | 5 | local $Log::Minimal::AUTODUMP = 1; | |||||||||||||||||||||||||||
| 313 | 5 | my $services = shift; | |||||||||||||||||||||||||||
| 314 | 5 | my %fileno2sid; | |||||||||||||||||||||||||||
| 315 | 5 | my $s = IO::Select->new(); | |||||||||||||||||||||||||||
| 316 | 5 | debugf "[Proclet] start log worker"; | |||||||||||||||||||||||||||
| 317 | 5 | my $maxlen = 0; | |||||||||||||||||||||||||||
| 318 | 5 | for my $sid ( keys %$services ) { | |||||||||||||||||||||||||||
| 319 | 19 | close $services->{$sid}->{pipe}->[1]; | |||||||||||||||||||||||||||
| 320 | 19 | my $rh = $services->{$sid}->{pipe}->[0]; | |||||||||||||||||||||||||||
| 321 | 19 | $fileno2sid{fileno($rh)} = $sid; | |||||||||||||||||||||||||||
| 322 | 19 | $s->add($rh); | |||||||||||||||||||||||||||
| 323 | 19 | ▶ | $maxlen = length($sid) if length($sid) > $maxlen; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | 5 | ▶ | $maxlen = 10 if $maxlen < 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 5 | my $loop = 0; | |||||||||||||||||||||||||||
| 327 | 10 | ▶ | local $SIG{TERM} = $SIG{INT} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 328 | 10 | $loop++; | |||||||||||||||||||||||||||
| 329 | 5 | }; | |||||||||||||||||||||||||||
| 330 | 5 | ▶ | while ( $loop < 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 49 | my @ready = $s->can_read(1); | |||||||||||||||||||||||||||
| 332 | 49 | foreach my $fh ( @ready ) { | |||||||||||||||||||||||||||
| 333 | 24 | my $sid = $fileno2sid{fileno($fh)}; | |||||||||||||||||||||||||||
| 334 | 24 | my @lt = localtime; | |||||||||||||||||||||||||||
| 335 | 24 | sysread($fh, my $buf, 65536); | |||||||||||||||||||||||||||
| 336 | 24 | for my $log ( split /\r?\n/, $buf ) { | |||||||||||||||||||||||||||
| 337 | 38 | my $prefix = sprintf('%02d:%02d:%02d %-'.$maxlen.'s |',$lt[2],$lt[1],$lt[0], $sid); | |||||||||||||||||||||||||||
| 338 | 38 | ▶ | $prefix = colored( $prefix, $services->{$sid}->{color} ) if $self->color; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 38 | chomp $log; | |||||||||||||||||||||||||||
| 340 | 38 | chomp $log; | |||||||||||||||||||||||||||
| 341 | 38 | ▶ | if ( $self->logger ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 30 | $self->logger->($prefix . ' ' . $log . "\n"); | |||||||||||||||||||||||||||
| 343 | } else { | ||||||||||||||||||||||||||||
| 344 | 8 | warn $prefix . ' ' . $log . "\n"; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | 5 | debugf "[Proclet] finished log worker"; | |||||||||||||||||||||||||||
| 350 | 24 | }; | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 3 | ▶ | sub current_min { | ||||||||||||||||||||||||||
|
Subroutine
current_min: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 354 | 3 | my $time = time; | |||||||||||||||||||||||||||
| 355 | 3 | $time = $time - ($time % 60); | |||||||||||||||||||||||||||
| 356 | 3 | $time; | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 6 | ▶ | sub cron_worker { | ||||||||||||||||||||||||||
|
Subroutine
cron_worker: called CC=1 SCAR=1.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 360 | 6 | my ($self,$code,$cron) = @_; | |||||||||||||||||||||||||||
| 361 | 1 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=28.1
| |||||||||||||||||||||||||||||
| 362 | 1 | debugf "[Proclet] start cron worker"; | |||||||||||||||||||||||||||
| 363 | 1 | my $live = 1; | |||||||||||||||||||||||||||
| 364 | 1 | ▶ | local $SIG{TERM} = sub { $live = 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 365 | 1 | local $SIG{CHLD} = 'IGNORE'; | |||||||||||||||||||||||||||
| 366 | 1 | my $prev = current_min(); | |||||||||||||||||||||||||||
| 367 | 1 | select undef, undef, undef, 1; ## no critic; | |||||||||||||||||||||||||||
| 368 | 1 | ▶ | while ( $live ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 1 | my $now; | |||||||||||||||||||||||||||
| 370 | 1 | ▶ | while ( $live ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 2 | $now = current_min(); | |||||||||||||||||||||||||||
| 372 | 2 | ▶ | last if $now != $prev; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 2 | select undef, undef, undef, 1; ## no critic; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | 1 | ▶ | last unless $live; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | 0 | $prev = $now; | |||||||||||||||||||||||||||
| 377 | 0 | debugf "[Proclet] check cron"; | |||||||||||||||||||||||||||
| 378 | 0 | ▶ | next unless $cron->($now); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 0 | debugf "[Proclet] cron match and start child worker!"; | |||||||||||||||||||||||||||
| 380 | 0 | my $pid = fork; | |||||||||||||||||||||||||||
| 381 | 0 | ▶ | if ( ! defined $pid ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 382 | 0 | die "Died: fork failed: $!"; | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | elsif ( $pid == 0 ) { | ||||||||||||||||||||||||||||
| 385 | #child | ||||||||||||||||||||||||||||
| 386 | 0 | local $SIG{TERM} = 'DEFAULT'; | |||||||||||||||||||||||||||
| 387 | 0 | local $SIG{CHLD} = 'DEFAULT'; | |||||||||||||||||||||||||||
| 388 | 0 | $code->(); | |||||||||||||||||||||||||||
| 389 | 0 | exit 0; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | #parent | ||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | 6 | }; | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 31 | __PACKAGE__->meta->make_immutable(); | |||||||||||||||||||||||||||
| 397 | 31 | 1; | |||||||||||||||||||||||||||
| 398 | __END__ | ||||||||||||||||||||||||||||