Worker/Fork.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Video/Ffmpeg.pm ↑ Summary Worker/Parallel.pm »
1 #
2 # $Id$
3 #
4 # worker::fork Brik
5 #
6 package Metabrik::Worker::Fork;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Default attribute values put here will BE inherited by subclasses
13 sub brik_properties {
14 return {
15 revision => '$Revision$',
16 tags => [ qw(unstable process) ],
17 author => 'GomoR <GomoR[at]metabrik.org>',
18 license => 'http://opensource.org/licenses/BSD-3-Clause',
19 attributes => {
20 pid => [ qw(forked_process_id) ],
21 },
22 commands => {
23 start => [ ],
24 stop => [ ],
25 is_son_alive => [ ],
26 },
27 require_modules => {
28 #'POSIX' => [ ],
29 },
30 };
31 }
32  
33 sub start {
34 my $self = shift;
35  
36 # Fork a new process
37 defined(my $pid = fork()) or return $self->log->error("start: fork: $!");
38 if ($pid) { # Father
39 my $restore = $SIG{INT};
40  
41 $self->pid($pid);
42  
43 $SIG{INT} = sub {
44 $self->log->debug("SIGINT: caught, son [$pid] QUITs now");
45 $self->stop;
46 $SIG{INT} = $restore;
47 return 1;
48 };
49 }
50  
51 # Return to father and son processes
52 return $pid;
53 }
54  
55 sub stop {
56 my $self = shift;
57  
58 if ($self->pid) {
59 kill('TERM', $self->pid);
60 waitpid($self->pid, POSIX::WNOHANG()); # Cleanup zombie state in case it is dead
61 $self->pid(undef);
62 }
63  
64 return 1;
65 }
66  
67 sub is_son_alive {
68 my $self = shift;
69  
70 my $pid = $self->pid;
71 if (defined($pid)) {
72 waitpid($pid, POSIX::WNOHANG()); # Cleanup zombie state in case it is dead
73 my $r = kill('ZERO', $pid);
74 $self->log->debug("is_son_alive: kill returned [$r] for pid [$pid]");
75 if ($r) { # Son still alive
76 return 1;
77 }
78 }
79  
80 return 0;
81 }
82  
83 sub brik_fini {
84 my $self = shift;
85  
86 return $self->stop;
87 }
88  
89 1;
90  
91 __END__
« Video/Ffmpeg.pm ↑ Summary Worker/Parallel.pm »