line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Continual::Process; |
2
|
1
|
|
|
1
|
|
444
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
38
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.2.0'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
435
|
use POSIX qw(:sys_wait_h); |
|
1
|
|
|
|
|
4176
|
|
|
1
|
|
|
|
|
5
|
|
8
|
1
|
|
|
1
|
|
1294
|
use Continual::Process::Instance; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
9
|
1
|
|
|
1
|
|
4
|
use Class::Tiny qw(name code), { instances => 1, }; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
3
|
|
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
|
3281
|
my ($self) = @_; |
135
|
|
|
|
|
|
|
|
136
|
5
|
|
|
|
|
9
|
foreach my $req (qw/name code/) { |
137
|
9
|
100
|
|
|
|
191
|
die "$req attribute required" if !defined $self->$req; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
3
|
100
|
|
|
|
45
|
if (ref $self->code ne 'CODE') { |
141
|
1
|
|
|
|
|
11
|
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
|
19
|
my ($self) = @_; |
152
|
|
|
|
|
|
|
|
153
|
2
|
|
|
|
|
1
|
my @instances; |
154
|
|
|
|
|
|
|
|
155
|
2
|
|
|
|
|
26
|
foreach my $instance_id (1 .. $self->instances) { |
156
|
11
|
|
|
|
|
232
|
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
|
|
|
|
|
18
|
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; |