File Coverage

blib/lib/App/EventStreamr/Process.pm
Criterion Covered Total %
statement 18 53 33.9
branch 0 24 0.0
condition 0 12 0.0
subroutine 8 11 72.7
pod n/a
total 26 100 26.0


line stmt bran cond sub pod time code
1             package App::EventStreamr::Process;
2 9     9   73129 use Method::Signatures;
  9         75093  
  9         45  
3 9     9   2610 use Moo;
  9         14  
  9         45  
4 9     9   5184 use Scalar::Util::Reftype;
  9         25130  
  9         517  
5 9     9   71 use Carp 'croak';
  9         23  
  9         386  
6 9     9   34 use namespace::clean;
  9         9  
  9         52  
7              
8             # ABSTRACT: A process object
9              
10             our $VERSION = '0.3'; # 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 9 0   9   8080 method _run() {
  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         my $run = defined $self->{config}{control}{$self->{id}}{run} ? $self->{config}{control}{$self->{id}}{run} : 1;
34              
35 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       if ($run && $self->{config}{run}) {
40 0           return 1;
41             } else {
42 0           return 0;
43             }
44             }
45              
46 9 0   9   4610 method _restart() {
  0     0      
  0            
47 0 0 0       if (defined $self->{config}{control}{$self->{id}}{run} && $self->{config}{control}{$self->{id}}{run} == 2) {
48 0           return 1;
49             } else {
50 0           return 0;
51             }
52             }
53              
54              
55 9 0   9   4194 method run_stop() {
  0     0      
  0            
56 0 0 0       if ($self->_restart) {
    0 0        
    0          
57 0           $self->info("Restarting ".$self->id." with command: ".$self->cmd);
58 0           $self->status->restarting($self->{id},$self->{type});
59              
60 0 0         if (! $self->running) {
61 0           $self->{config}{control}{$self->{id}}{run} = 1;
62             } else {
63 0           $self->status->stopping($self->{id},$self->{type});
64 0           $self->stop();
65            
66             # Give the process time to settle.
67 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 0 0         return if $self->status->threshold($self->{id},$self->{type});
74              
75 0           $self->info("Starting ".$self->id." with command: ".$self->cmd);
76 0           $self->status->starting($self->{id},$self->{type});
77              
78 0           $self->start();
79            
80             # Give the process time to settle.
81 0           sleep $self->sleep_time;
82             } elsif ( (! $self->_run && $self->pid ) ) {
83 0           $self->info("Stopping ".$self->id);
84 0           $self->status->stopping($self->{id},$self->{type});
85              
86 0           $self->stop();
87              
88             # Give the process time to settle.
89 0           sleep $self->sleep_time;
90             }
91            
92              
93             # Write config on state change.
94 0 0         if ( $self->status->set_state($self->running,$self->{id},$self->{type}) ) {
95 0           $self->info("State changed for ".$self->id);
96 0           $self->config->write_config();
97             }
98 0           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.3
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