File Coverage

blib/lib/App/EventStreamr/Process.pm
Criterion Covered Total %
statement 28 53 52.8
branch 5 24 20.8
condition 1 12 8.3
subroutine 9 11 81.8
pod n/a
total 43 100 43.0


line stmt bran cond sub pod time code
1             package App::EventStreamr::Process;
2 2     2   1254 use Method::Signatures;
  2         3  
  2         13  
3 2     2   670 use Moo;
  2         4  
  2         10  
4 2     2   1795 use Scalar::Util::Reftype;
  2         10333  
  2         109  
5 2     2   13 use Carp 'croak';
  2         3  
  2         91  
6 2     2   10 use namespace::clean;
  2         3  
  2         12  
7              
8             # ABSTRACT: A process object
9              
10             our $VERSION = '0.5'; # VERSION: Generated by DZP::OurPkg:Version
11              
12              
13             my $ConfigRef = sub {
14             croak "auth isn't a 'App::EventStreamr::Config' object!" unless reftype( $_[0] )->class eq "App::EventStreamr::Config";
15             };
16              
17             my $StatusRef = sub {
18             croak "auth isn't a 'App::EventStreamr::Status' object!" unless reftype( $_[0] )->class eq "App::EventStreamr::Status";
19             };
20              
21             has 'config' => ( is => 'rw', required => 1, isa => $ConfigRef );
22             has 'status' => ( is => 'rw', required => 1, isa => $StatusRef );
23             has 'cmd' => ( is => 'ro', required => 1 );
24             has 'id' => ( is => 'ro', required => 1 );
25             has 'type' => ( is => 'ro', default => sub { 'internal' } );
26             has 'sleep_time' => ( is => 'ro', default => sub { 1 } );
27             has 'cmd_regex' => ( is => 'ro' );
28              
29 2 0   2   2019 method _run() {
  0     0   0  
  0         0  
30             # Unless we spefically set our state we're going to want to run
31             # By default our control config will be empty and this shortcut
32             # attriubute makes things look a little cleaner.
33 0 0       0 my $run = defined $self->{config}{control}{$self->{id}}{run} ? $self->{config}{control}{$self->{id}}{run} : 1;
34              
35 0         0 $self->{config}{control}{$self->{id}}{run} = $run;
36            
37             # We only want to run if both the process is set to run
38             # and the system is set to run.
39 0 0 0     0 if ($run && $self->{config}{run}) {
40 0         0 return 1;
41             } else {
42 0         0 return 0;
43             }
44             }
45              
46 2 0   2   1218 method _restart() {
  0     0   0  
  0         0  
47 0 0 0     0 if (defined $self->{config}{control}{$self->{id}}{run} && $self->{config}{control}{$self->{id}}{run} == 2) {
48 0         0 return 1;
49             } else {
50 0         0 return 0;
51             }
52             }
53              
54              
55 2 50   2   1130 method run_stop() {
  1     1   14  
  1         7  
56 1 50 33     5 if ($self->_restart) {
    50 0        
    0          
57 0         0 $self->info("Restarting ".$self->id." with command: ".$self->cmd);
58 0         0 $self->status->restarting($self->{id},$self->{type});
59              
60 0 0       0 if (! $self->running) {
61 0         0 $self->{config}{control}{$self->{id}}{run} = 1;
62             } else {
63 0         0 $self->status->stopping($self->{id},$self->{type});
64 0         0 $self->stop();
65            
66             # Give the process time to settle.
67 0         0 sleep $self->sleep_time;
68             }
69              
70             } elsif ( $self->_run && ! $self->pid) {
71             # Slows down the restarts to ensure we don't flood logs
72             # and control systems.
73 1 50       13 return if $self->status->threshold($self->{id},$self->{type});
74              
75 1         37 $self->info("Starting ".$self->id." with command: ".$self->cmd);
76 1         35 $self->status->starting($self->{id},$self->{type});
77              
78 1         9 $self->start();
79            
80             # Give the process time to settle.
81 1         1007643 sleep $self->sleep_time;
82             } elsif ( (! $self->_run && $self->pid ) ) {
83 0         0 $self->info("Stopping ".$self->id);
84 0         0 $self->status->stopping($self->{id},$self->{type});
85              
86 0         0 $self->stop();
87              
88             # Give the process time to settle.
89 0         0 sleep $self->sleep_time;
90             }
91            
92              
93             # Write config on state change.
94 1 50       352 if ( $self->status->set_state($self->running,$self->{id},$self->{type}) ) {
95 0         0 $self->info("State changed for ".$self->id);
96 0         0 $self->config->write_config();
97             }
98 1         19 return;
99             }
100              
101             with('App::EventStreamr::Roles::Logger','App::EventStreamr::Roles::ProcessControl');
102              
103             1;
104              
105             __END__
106              
107             =pod
108              
109             =encoding UTF-8
110              
111             =head1 NAME
112              
113             App::EventStreamr::Process - A process object
114              
115             =head1 VERSION
116              
117             version 0.5
118              
119             =head1 SYNOPSIS
120              
121             This package provides core start/stop logic for all processes
122             and devices.
123              
124             =head1 DESCRIPTION
125              
126             This package provides the core run/stop logic for EventStreamr. A
127             process will extend this and provide any extra logic specific to its
128             needs.
129              
130             It consumes the 'ProcessControl' role with requires a 'cmd' attribute
131             and will utilise an optional 'cmd_regex' if one exists.
132              
133             =head1 METHODS
134              
135             =head2 run_stop
136             $device->run_stop()
137              
138             Will start the process if it's intended to be running or stop it
139             if isn't.
140              
141             =head1 AUTHOR
142              
143             Leon Wright < techman@cpan.org >
144              
145             =head1 COPYRIGHT AND LICENSE
146              
147             This software is Copyright (c) 2014 by Leon Wright.
148              
149             This is free software, licensed under:
150              
151             The GNU Affero General Public License, Version 3, November 2007
152              
153             =cut