File Coverage

blib/lib/Continual/Process.pm
Criterion Covered Total %
statement 25 25 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 1 2 50.0
total 37 38 97.3


line stmt bran cond sub pod time code
1             package Continual::Process;
2 1     1   856 use strict;
  1         4  
  1         35  
3 1     1   68 use warnings;
  1         4  
  1         64  
4              
5             our $VERSION = '0.2.1';
6              
7 1     1   787 use POSIX qw(:sys_wait_h);
  1         8718  
  1         10  
8 1     1   2720 use Continual::Process::Instance;
  1         5  
  1         42  
9 1     1   8 use Class::Tiny qw(name code), { instances => 1, };
  1         4  
  1         7  
10              
11             =head1 NAME
12              
13             Continual::Process - (re)start dead process
14              
15             =head1 SYNOPSIS
16              
17             use Continual::Process;
18             use Continual::Process::Loop;
19              
20             my $loop = Continual::Process::Loop->new(
21             instances => [
22             Continual::Process->new(
23             name => 'job1',
24             code => sub {
25             my $pid = fork;
26             if ($pid) {
27             return $pid;
28             }
29              
30             say "Hello world";
31             sleep 5;
32             say "Bye, bye world";
33              
34             exit 1;
35             },
36             instances => 4,
37             )->create_instance(),
38             Continual::Process->new(
39             name => 'job2',
40             code => sub {
41             my $pid = fork;
42             if ($pid) {
43             return $pid;
44             }
45              
46             exec 'perl -ne "sleep 1"';
47              
48             exit 1;
49             },
50             )->create_instance(),
51             ]
52             );
53              
54             $loop->run();
55              
56             =head1 DESCRIPTION
57              
58             Continual::Process with Continual::Process::Loop is a way how to run a process forever.
59              
60             Continual::Process creates Continual::Process::Instance which runs in a loop and if it dies, it starts again.
61              
62             The code for starting a process is OS-agnostic. The only condition is that the code must return PID of the new process.
63              
64             =head2 loop
65              
66             Continual::Process supports more loops:
67              
68             =over 1
69              
70             =item L - simple while/sleep loop
71              
72             =item L - L support
73              
74             =item L - L support
75              
76             =back
77              
78             =head1 METHODS
79              
80             =head2 new(%attributes)
81              
82             =head3 %attributes
83              
84             =head4 name
85              
86             name of process (only for identification)
87              
88             =head4 code
89              
90             CodeRef which start new process and returned C of new process
91              
92             I-sub B return C of the new process or die!
93              
94             for example Linux and fork:
95              
96             code => sub {
97             if (my $pid = fork) {
98             return $pid;
99             }
100              
101             ...
102              
103             exit 1;
104             }
105              
106             or Windows and L
107              
108             code => sub {
109             my ($instance) = @_;
110              
111             Win32::Process::Create(
112             $ProcessObj,
113             "C:\\winnt\\system32\\notepad.exe",
114             "notepad temp.txt",
115             0,
116             NORMAL_PRIORITY_CLASS,
117             "."
118             ) || die "Process ".$instance->name." start fail: ".$^E;
119              
120             return $ProcessObj->GetProcessID();
121             }
122              
123             best way is use L C or C method
124              
125             =head4 instances
126              
127             count of running instances
128              
129             default I<1>
130              
131             =cut
132              
133             sub BUILD {
134 5     5 0 7810 my ($self) = @_;
135              
136 5         16 foreach my $req (qw/name code/) {
137 9 100       285 die "$req attribute required" if !defined $self->$req;
138             }
139              
140 3 100       88 if (ref $self->code ne 'CODE') {
141 1         32 die 'code attribute must be CodeRef';
142             }
143             }
144              
145             =head2 create_instance()
146              
147             create and return list of L
148              
149             =cut
150             sub create_instance {
151 2     2 1 42 my ($self) = @_;
152              
153 2         5 my @instances;
154              
155 2         55 foreach my $instance_id (1 .. $self->instances) {
156 11         385 push @instances,
157             Continual::Process::Instance->new(
158             name => $self->name,
159             instance_id => $instance_id,
160             code => $self->code,
161             );
162             }
163              
164 2         37 return @instances;
165             }
166              
167             =head1 LICENSE
168              
169             Copyright (C) Avast Software.
170              
171             This library is free software; you can redistribute it and/or modify
172             it under the same terms as Perl itself.
173              
174             =head1 AUTHOR
175              
176             Jan Seidl Eseidl@avast.comE
177              
178             =cut
179              
180             1;