Zing/Logic/Watcher.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 118 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Zing::Logic::Watcher;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry 'Zing::Types';
9 use routines;
10  
11 use Data::Object::Class;
12 use Data::Object::ClassHas;
13  
14 extends 'Zing::Logic';
15  
16 use Zing::Flow;
17 use Zing::Fork;
18  
19 our $VERSION = '0.11'; # VERSION
20  
21 # ATTRIBUTES
22  
23 has 'fork' => (
24 is => 'ro',
25 isa => 'Fork',
26 new => 1
27 );
28  
29 fun new_fork($self) {
30 Zing::Fork->new(parent => $self->process, scheme => $self->scheme)
31 }
32  
33 has 'on_launch' => (
34 is => 'ro',
35 isa => 'CodeRef',
36 new => 1,
37 );
38  
39 fun new_on_launch($self) {
40 $self->can('handle_launch_event')
41 }
42  
43 has 'on_monitor' => (
44 is => 'ro',
45 isa => 'CodeRef',
46 new => 1,
47 );
48  
49 fun new_on_monitor($self) {
50 $self->can('handle_monitor_event')
51 }
52  
53 has 'on_sanitize' => (
54 is => 'ro',
55 isa => 'CodeRef',
56 new => 1,
57 );
58  
59 fun new_on_sanitize($self) {
60 $self->can('handle_sanitize_event')
61 }
62  
63 has 'scheme' => (
64 is => 'ro',
65 isa => 'Scheme',
66 new => 1
67 );
68  
69 fun new_scheme($self) {
70 $self->process->scheme
71 }
72  
73 has 'size' => (
74 is => 'ro',
75 isa => 'Int',
76 new => 1
77 );
78  
79 fun new_size($self) {
80 $self->scheme->[2]
81 }
82  
83 # METHODS
84  
85 method flow() {
86 my $step_0 = $self->next::method;
87  
88 my $step_1 = Zing::Flow->new(
89 name => 'on_launch',
90 code => fun($step, $loop) { $self->trace('on_launch')->($self) }
91 );
92 my $step_2 = $step_1->next(Zing::Flow->new(
93 name => 'on_monitor',
94 code => fun($step, $loop) { $self->trace('on_monitor')->($self) }
95 ));
96 my $step_3 = $step_2->next(Zing::Flow->new(
97 name => 'on_sanitize',
98 code => fun($step, $loop) { $self->trace('on_sanitize')->($self) }
99 ));
100  
101 $step_0->append($step_1);
102 $step_0
103 }
104  
105 method handle_launch_event() {
106 my $fork = $self->fork;
107 my $process = $self->process;
108  
109 if ($self->interupt) {
110 return 0;
111 }
112  
113 if ($process->loop->last) {
114 return 0;
115 }
116  
117 if ($process->loop->stop) {
118 return 0;
119 }
120  
121 my $max_forks = $self->size;
122 my $has_forks = keys %{$fork->processes};
123  
124 if ($has_forks > $max_forks) {
125 return 0; # wtf
126 }
127 if (my $needs = $max_forks - $has_forks) {
128 for (1..$needs) {
129 $fork->execute;
130 }
131 }
132 else {
133 return 0;
134 }
135 }
136  
137 method handle_monitor_event() {
138 my $fork = $self->fork;
139  
140 return $fork->monitor;
141 }
142  
143 method handle_sanitize_event() {
144 my $fork = $self->fork;
145  
146 return $fork->sanitize;
147 }
148  
149 method signals() {
150 my $trapped = {};
151 my $fork = $self->fork;
152  
153 $trapped->{INT} = sub {
154 $self->trace('interupt', 'INT');
155 $fork->terminate($self->interupt);
156 do {0} while ($fork->sanitize); # reap children
157 $self->process->winddown;
158 };
159  
160 $trapped->{QUIT} = sub {
161 $self->trace('interupt', 'QUIT');
162 $fork->terminate($self->interupt);
163 do {0} while ($fork->sanitize); # reap children
164 $self->process->winddown;
165 };
166  
167 $trapped->{TERM} = sub {
168 $self->trace('interupt', 'TERM');
169 $fork->terminate($self->interupt);
170 do {0} while ($fork->sanitize); # reap children
171 $self->process->winddown;
172 };
173  
174 $trapped->{USR1} = sub {
175 $self->debug('handles "interupt", with, USR1');
176 $fork->terminate('INT');
177 do {0} while ($fork->sanitize); # reap children
178 };
179  
180 $trapped->{USR2} = sub {
181 $self->debug('handles "interupt", with, USR2');
182 $fork->terminate('INT');
183 do {0} while ($fork->sanitize); # reap children
184 };
185  
186 return $trapped;
187 }
188  
189 1;
190  
191 =encoding utf8
192  
193 =head1 NAME
194  
195 Zing::Logic::Watcher - Watcher Logic
196  
197 =cut
198  
199 =head1 ABSTRACT
200  
201 Watcher Process Logic Chain
202  
203 =cut
204  
205 =head1 SYNOPSIS
206  
207 package Process;
208  
209 use parent 'Zing::Process';
210  
211 sub scheme {
212 ['MyApp', [], 1]
213 }
214  
215 package main;
216  
217 use Zing::Logic::Watcher;
218  
219 my $logic = Zing::Logic::Watcher->new(process => Process->new);
220  
221 # $logic->execute;
222  
223 =cut
224  
225 =head1 DESCRIPTION
226  
227 This package provides the logic (or logic chain) to be executed by the watcher
228 process event-loop.
229  
230 =cut
231  
232 =head1 INHERITS
233  
234 This package inherits behaviors from:
235  
236 L<Zing::Logic>
237  
238 =cut
239  
240 =head1 LIBRARIES
241  
242 This package uses type constraints from:
243  
244 L<Zing::Types>
245  
246 =cut
247  
248 =head1 ATTRIBUTES
249  
250 This package has the following attributes:
251  
252 =cut
253  
254 =head2 fork
255  
256 fork(Fork)
257  
258 This attribute is read-only, accepts C<(Fork)> values, and is optional.
259  
260 =cut
261  
262 =head2 interupt
263  
264 interupt(Interupt)
265  
266 This attribute is read-only, accepts C<(Interupt)> values, and is optional.
267  
268 =cut
269  
270 =head2 on_launch
271  
272 on_launch(CodeRef)
273  
274 This attribute is read-only, accepts C<(CodeRef)> values, and is optional.
275  
276 =cut
277  
278 =head2 on_monitor
279  
280 on_monitor(CodeRef)
281  
282 This attribute is read-only, accepts C<(CodeRef)> values, and is optional.
283  
284 =cut
285  
286 =head2 on_perform
287  
288 on_perform(CodeRef)
289  
290 This attribute is read-only, accepts C<(CodeRef)> values, and is optional.
291  
292 =cut
293  
294 =head2 on_receive
295  
296 on_receive(CodeRef)
297  
298 This attribute is read-only, accepts C<(CodeRef)> values, and is optional.
299  
300 =cut
301  
302 =head2 on_register
303  
304 on_register(CodeRef)
305  
306 This attribute is read-only, accepts C<(CodeRef)> values, and is optional.
307  
308 =cut
309  
310 =head2 on_reset
311  
312 on_reset(CodeRef)
313  
314 This attribute is read-only, accepts C<(CodeRef)> values, and is optional.
315  
316 =cut
317  
318 =head2 on_sanitize
319  
320 on_sanitize(CodeRef)
321  
322 This attribute is read-only, accepts C<(CodeRef)> values, and is optional.
323  
324 =cut
325  
326 =head2 on_suicide
327  
328 on_suicide(CodeRef)
329  
330 This attribute is read-only, accepts C<(CodeRef)> values, and is optional.
331  
332 =cut
333  
334 =head2 process
335  
336 process(Process)
337  
338 This attribute is read-only, accepts C<(Process)> values, and is required.
339  
340 =cut
341  
342 =head2 scheme
343  
344 scheme(Scheme)
345  
346 This attribute is read-only, accepts C<(Scheme)> values, and is optional.
347  
348 =cut
349  
350 =head2 size
351  
352 size(Int)
353  
354 This attribute is read-only, accepts C<(Int)> values, and is optional.
355  
356 =cut
357  
358 =head1 METHODS
359  
360 This package implements the following methods:
361  
362 =cut
363  
364 =head2 flow
365  
366 flow() : Flow
367  
368 The flow method builds and returns the logic flow for the process event-loop.
369  
370 =over 4
371  
372 =item flow example #1
373  
374 # given: synopsis
375  
376 my $flow = $logic->flow;
377  
378 =back
379  
380 =cut
381  
382 =head2 signals
383  
384 signals() : HashRef
385  
386 The signals method builds and returns the process signal handlers.
387  
388 =over 4
389  
390 =item signals example #1
391  
392 # given: synopsis
393  
394 my $signals = $logic->signals;
395  
396 =back
397  
398 =cut
399  
400 =head1 AUTHOR
401  
402 Al Newkirk, C<awncorp@cpan.org>
403  
404 =head1 LICENSE
405  
406 Copyright (C) 2011-2019, Al Newkirk, et al.
407  
408 This is free software; you can redistribute it and/or modify it under the terms
409 of the The Apache License, Version 2.0, as elucidated in the L<"license
410 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
411  
412 =head1 PROJECT
413  
414 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
415  
416 L<Project|https://github.com/iamalnewkirk/zing>
417  
418 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
419  
420 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
421  
422 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
423  
424 L<Issues|https://github.com/iamalnewkirk/zing/issues>
425  
426 =cut