File Coverage

blib/lib/Pipeline/Segment/Async/Fork.pm
Criterion Covered Total %
statement 51 59 86.4
branch 7 10 70.0
condition n/a
subroutine 13 14 92.8
pod 4 8 50.0
total 75 91 82.4


line stmt bran cond sub pod time code
1             package Pipeline::Segment::Async::Fork;
2              
3 1     1   5 use strict;
  1         2  
  1         30  
4 1     1   5 use warnings;
  1         2  
  1         22  
5              
6 1     1   807 use Data::UUID;
  1         1279  
  1         74  
7 1     1   630 use Pipeline::Segment::Async::Handler;
  1         3  
  1         26  
8 1     1   5 use base qw( Pipeline::Segment::Async::Handler );
  1         2  
  1         895  
9              
10             our $VERSION = "3.12";
11              
12 1     1 1 4 sub canop { 1 }
13              
14             sub run {
15 1     1 1 2 my $self = shift;
16 1         2 my $sub = shift;
17 1         3 my @args = @_;
18 1         3 $self->create_id;
19 1 50       1100 if (!(my $pid = fork())) {
20 0         0 require Storable;
21 0         0 require File::Spec;
22 0         0 my $results = $sub->( @args );
23 0         0 Storable::nstore(
24             $results,
25             File::Spec->catfile(
26             File::Spec->tmpdir(),
27             $self->id
28             )
29             );
30 0         0 kill 9, $$;
31             } else {
32 1         65 $self->pid( $pid );
33 1         44 return;
34             }
35             }
36              
37             sub create_id {
38 1     1 0 2 my $self = shift;
39 1         209 my $ug = Data::UUID->new();
40 1         217 my $uuid = $ug->create;
41 1         12 my $id = $ug->to_string( $uuid );
42 1         4 $self->id( $id );
43             }
44              
45             sub id {
46 3     3 0 6 my $self = shift;
47 3         5 my $id = shift;
48 3 100       11 if (defined( $id )) {
49 1         4 $self->{ thread_id } = $id;
50 1         87 return $self;
51             } else {
52 2         201 return $self->{ thread_id };
53             }
54             }
55              
56             sub pid {
57 3     3 0 14 my $self = shift;
58 3         9 my $pid = shift;
59 3 100       21 if (defined( $pid )) {
60 1         24 $self->{ pid } = $pid;
61 1         8 return $self;
62             } else {
63 2         1390365 return $self->{ pid };
64             }
65             }
66              
67             sub reattach {
68 1     1 1 133 my $self = shift;
69 1 50       4 if (waitpid($self->pid, 0) == $self->pid) {
70 1         1423 require Storable;
71 1         3672 require File::Spec;
72 1         184 my $return = Storable::retrieve(
73             File::Spec->catfile(
74             File::Spec->tmpdir(),
75             $self->id
76             )
77             );
78 1         218 $self->unlink();
79 1         7 return $return;
80             } else {
81 0         0 die "cannot wait for pid ". $self->pid;
82             }
83             }
84              
85             sub discard {
86 0     0 1 0 my $self = shift;
87 0         0 $self->{ DESTROY_SHOULD_UNLINK } = 1;
88             }
89              
90             sub unlink {
91 1     1 0 2 my $self = shift;
92 1         16 unlink(
93             File::Spec->catfile(
94             File::Spec->tmpdir(),
95             $self->id
96             )
97             );
98             }
99              
100             sub DESTROY {
101 1     1   3 my $self = shift;
102 1 50       19 $self->unlink if $self->{ DESTROY_SHOULD_UNLINK };
103             }
104              
105              
106             1;
107             __END__