line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Proc::tored::Machine; |
2
|
|
|
|
|
|
|
$Proc::tored::Machine::VERSION = '0.20'; |
3
|
2
|
|
|
2
|
|
20
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
81
|
|
4
|
2
|
|
|
2
|
|
24
|
use warnings; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
83
|
|
5
|
2
|
|
|
2
|
|
15
|
use Moo; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
17
|
|
6
|
2
|
|
|
2
|
|
1051
|
use Carp; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
258
|
|
7
|
2
|
|
|
2
|
|
772
|
use Auto::Mata '!with'; |
|
2
|
|
|
|
|
237101
|
|
|
2
|
|
|
|
|
526
|
|
8
|
2
|
|
|
2
|
|
42
|
use Proc::tored::Flag; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
75
|
|
9
|
2
|
|
|
2
|
|
843
|
use Proc::tored::PidFile; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
133
|
|
10
|
2
|
|
|
2
|
|
777
|
use Proc::tored::Types -types; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
36
|
|
11
|
2
|
|
|
2
|
|
1689
|
use Time::HiRes; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
19
|
|
12
|
2
|
|
|
2
|
|
236
|
use Type::Utils qw(declare as where); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
16
|
|
13
|
2
|
|
|
2
|
|
1413
|
use Types::Standard -types; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
18
|
|
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
12980
|
use constant READY => 'READY'; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
328
|
|
16
|
2
|
|
|
2
|
|
22
|
use constant STATUS => 'STATUS'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
220
|
|
17
|
2
|
|
|
2
|
|
15
|
use constant LOCK => 'LOCK'; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
139
|
|
18
|
2
|
|
|
2
|
|
15
|
use constant TOUCH => 'TOUCH'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
105
|
|
19
|
2
|
|
|
2
|
|
14
|
use constant STOP => 'STOP'; |
|
2
|
|
|
|
|
47
|
|
|
2
|
|
|
|
|
106
|
|
20
|
2
|
|
|
2
|
|
15
|
use constant TERM => 'TERM'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
3461
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $Lock = Maybe[InstanceOf['Guard']]; |
23
|
|
|
|
|
|
|
my $Flag = InstanceOf['Proc::tored::Flag']; |
24
|
|
|
|
|
|
|
my $PidFile = InstanceOf['Proc::tored::PidFile']; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $Proctor = declare 'Proctor', as Dict[ |
27
|
|
|
|
|
|
|
pidfile => $PidFile, |
28
|
|
|
|
|
|
|
stopped => $Flag, |
29
|
|
|
|
|
|
|
paused => $Flag, |
30
|
|
|
|
|
|
|
traps => SignalList, |
31
|
|
|
|
|
|
|
call => CodeRef, |
32
|
|
|
|
|
|
|
lock => $Lock, |
33
|
|
|
|
|
|
|
started => Bool, |
34
|
|
|
|
|
|
|
quit => Bool, |
35
|
|
|
|
|
|
|
finish => Bool, |
36
|
|
|
|
|
|
|
]; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $Stopped = declare 'Stopped', as $Proctor, where { $_->{stopped}->is_set || $_->{quit} }; |
39
|
|
|
|
|
|
|
my $NotStopped = declare 'NotStopped', as ~$Stopped; |
40
|
|
|
|
|
|
|
my $Unlocked = declare 'Unlocked', as $NotStopped, where { !$_->{lock} }; |
41
|
|
|
|
|
|
|
my $Locked = declare 'Locked', as $NotStopped, where { $_->{lock} }; |
42
|
|
|
|
|
|
|
my $Started = declare 'Started', as $Locked, where { $_->{started} }; |
43
|
|
|
|
|
|
|
my $Paused = declare 'Paused', as $Started, where { $_->{paused}->is_set }; |
44
|
|
|
|
|
|
|
my $NotPaused = declare 'NotPaused', as ~$Paused; |
45
|
|
|
|
|
|
|
my $Running = declare 'Running', as $NotPaused & $Started, where { !$_->{finish} }; |
46
|
|
|
|
|
|
|
my $Finished = declare 'Finished', as $Started, where { $_->{finish} }; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub pause_sleep { |
49
|
0
|
|
|
0
|
0
|
0
|
my ($acc, $time) = @_; |
50
|
0
|
|
|
|
|
0
|
Time::HiRes::sleep($time); |
51
|
0
|
|
|
|
|
0
|
$acc; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub sigtrap { |
55
|
8
|
|
|
8
|
0
|
24
|
my $acc = shift; |
56
|
8
|
|
|
|
|
21
|
foreach my $signal (@{$acc->{traps}}) { |
|
8
|
|
|
|
|
36
|
|
57
|
|
|
|
|
|
|
$SIG{$signal} = sub { |
58
|
2
|
|
|
2
|
|
366
|
warn "Caught SIG$signal\n"; |
59
|
2
|
|
|
|
|
28
|
$acc->{quit} = 1; |
60
|
2
|
|
|
|
|
17
|
$acc; |
61
|
2
|
|
|
|
|
76
|
}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my $FSM = machine { |
66
|
|
|
|
|
|
|
ready READY; |
67
|
|
|
|
|
|
|
term TERM; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
transition READY, to STATUS, on $Proctor; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
transition STATUS, to STOP, on $Finished; |
74
|
|
|
|
|
|
|
transition STATUS, to STOP, on $Stopped; |
75
|
|
|
|
|
|
|
transition STATUS, to TOUCH, on $Paused, using { pause_sleep($_, 0.2) }; |
76
|
|
|
|
|
|
|
transition STATUS, to TOUCH, on $Running, using { $_->{finish} = $_->{call}->() ? 0 : 1; $_ }; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
transition TOUCH, to STATUS, using { $_->{pidfile}->touch; $_ }; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
transition STATUS, to LOCK, on $Unlocked, using { $_->{lock} = $_->{pidfile}->lock; $_ }; |
83
|
|
|
|
|
|
|
transition LOCK, to STATUS, on $Locked, using { sigtrap($_); $_->{started} = 1; $_ }; |
84
|
|
|
|
|
|
|
transition LOCK, to TERM, on $Unlocked; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
transition STOP, to TERM, on $Proctor, using { undef $_->{lock}; undef $SIG{$_} foreach @{$_->{traps}}; $_ }; |
88
|
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
has pidfile_path => (is => 'ro', isa => NonEmptyStr, required => 1); |
91
|
|
|
|
|
|
|
has stop_path => (is => 'ro', isa => NonEmptyStr, required => 1); |
92
|
|
|
|
|
|
|
has pause_path => (is => 'ro', isa => NonEmptyStr, required => 1); |
93
|
|
|
|
|
|
|
has traps => (is => 'ro', isa => SignalList, default => sub {[]}); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
has pidfile => ( |
96
|
|
|
|
|
|
|
is => 'lazy', |
97
|
|
|
|
|
|
|
isa => $PidFile, |
98
|
|
|
|
|
|
|
handles => { |
99
|
|
|
|
|
|
|
read_pid => 'read_file', |
100
|
|
|
|
|
|
|
running_pid => 'running_pid', |
101
|
|
|
|
|
|
|
is_running => 'is_running', |
102
|
|
|
|
|
|
|
}, |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
has stop_flag => ( |
106
|
|
|
|
|
|
|
is => 'lazy', |
107
|
|
|
|
|
|
|
isa => $Flag, |
108
|
|
|
|
|
|
|
handles => { |
109
|
|
|
|
|
|
|
stop => 'set', |
110
|
|
|
|
|
|
|
start => 'unset', |
111
|
|
|
|
|
|
|
is_stopped => 'is_set', |
112
|
|
|
|
|
|
|
}, |
113
|
|
|
|
|
|
|
); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
has pause_flag => ( |
116
|
|
|
|
|
|
|
is => 'lazy', |
117
|
|
|
|
|
|
|
isa => $Flag, |
118
|
|
|
|
|
|
|
handles => { |
119
|
|
|
|
|
|
|
pause => 'set', |
120
|
|
|
|
|
|
|
resume => 'unset', |
121
|
|
|
|
|
|
|
is_paused => 'is_set', |
122
|
|
|
|
|
|
|
}, |
123
|
|
|
|
|
|
|
); |
124
|
|
|
|
|
|
|
|
125
|
5
|
|
|
5
|
|
264
|
sub _build_pidfile { Proc::tored::PidFile->new(file_path => shift->pidfile_path) } |
126
|
5
|
|
|
5
|
|
318
|
sub _build_stop_flag { Proc::tored::Flag->new(touch_file_path => shift->stop_path) } |
127
|
5
|
|
|
5
|
|
975
|
sub _build_pause_flag { Proc::tored::Flag->new(touch_file_path => shift->pause_path) } |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub clear_flags { |
130
|
11
|
|
|
11
|
0
|
25959
|
my $self = shift; |
131
|
11
|
|
|
|
|
279
|
$self->start; |
132
|
11
|
|
|
|
|
1802
|
$self->resume; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub run { |
136
|
9
|
|
|
9
|
0
|
135
|
my ($self, $code) = @_; |
137
|
|
|
|
|
|
|
|
138
|
9
|
|
|
|
|
469
|
my $acc = { |
139
|
|
|
|
|
|
|
pidfile => $self->pidfile, |
140
|
|
|
|
|
|
|
stopped => $self->stop_flag, |
141
|
|
|
|
|
|
|
paused => $self->pause_flag, |
142
|
|
|
|
|
|
|
traps => $self->traps, |
143
|
|
|
|
|
|
|
call => $code, |
144
|
|
|
|
|
|
|
lock => undef, |
145
|
|
|
|
|
|
|
started => 0, |
146
|
|
|
|
|
|
|
finish => 0, |
147
|
|
|
|
|
|
|
quit => 0, |
148
|
|
|
|
|
|
|
}; |
149
|
|
|
|
|
|
|
|
150
|
9
|
|
|
|
|
1157
|
my $service = $FSM->(); |
151
|
|
|
|
|
|
|
|
152
|
9
|
|
|
|
|
437
|
$service->($acc); |
153
|
|
|
|
|
|
|
|
154
|
9
|
|
|
|
|
870
|
return $acc->{started}; |
155
|
|
|
|
|
|
|
}; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
1; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
__END__ |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=pod |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=encoding UTF-8 |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 NAME |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Proc::tored::Machine |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 VERSION |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
version 0.20 |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 AUTHOR |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Jeff Ober <sysread@fastmail.fm> |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Jeff Ober. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
182
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
|