blib/lib/XAS/Lib/Service/Unix.pm

filter: Statementstmt 10.9% 8 / 73 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 2 / 6 Podpod 100% 3 / 3 totaltotal 11.4% 13 / 114 CC 17 SCAR 47.2
CC
17
Cov
8%
poll
182.0
session_interrupt
20.0
_current_state
6.0
CRAP 111.9
« blib/lib/XAS/Lib/Stomp/Parser.pm ↑ Summary blib/lib/XAS/Lib/Process.pm »
1 package XAS::Lib::Service::Unix;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use XAS::Class
8 1 debug => 0,
9 version => $VERSION,
10 base => 'XAS::Base',
11 constant => {
12 SERVICE_START_PENDING => 1,
13 SERVICE_STOP_PENDING => 2,
14 SERVICE_PAUSE_PENDING => 3,
15 SERVICE_CONTINUE_PENDING => 4,
16 SERVICE_CONTROL_SHUTDOWN => 5,
17 SERVICE_RUNNING => 6,
18 SERVICE_STOPPED => 7,
19 SERVICE_PAUSED => 8,
20 },
21 mixins => 'init_service _current_state session_interrupt poll
22 SERVICE_START_PENDING SERVICE_STOP_PENDING
23 SERVICE_PAUSE_PENDING SERVICE_CONTINUE_PENDING
24 SERVICE_CONTROL_SHUTDOWN SERVICE_RUNNING
25 SERVICE_STOPPED SERVICE_PAUSED',
26 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # ----------------------------------------------------------------------
29 # Public Methods
30 # ----------------------------------------------------------------------
31  
32 sub init_service {
33 0 ▶ my $self = shift;
Subroutine init_service: not called CC=1 SCAR=6.9
Pod documented
34  
35 0 my $alias = $self->alias;
36  
37 0 $self->log->debug("$alias: entering init_service() - unix");
38  
39 0 $poe_kernel->sig('CONT', 'session_interrupt');
40 0 $poe_kernel->sig('TSTP', 'session_interrupt');
41  
42 0 $self->log->debug("$alias: leaving int_service() - unix");
43  
44 }
45  
46 sub session_interrupt {
47 0 ▶ my $self = shift;
Subroutine session_interrupt: not called CC=4 SCAR=30.0
Pod documented
48 0 my $signal = shift;
49  
50 0 my $alias = $self->alias;
51  
52 0 $self->log->debug("$alias: session_interrupt()");
53 0 $self->log->warn_msg('service_signaled', $alias, $signal);
54  
55 0 ▶ if ($signal eq 'HUP') {
Branch: 3 branches
BranchTrueFalseTotal
if ($signal eq 'HUP') { } 0 0 0
elsif ($signal eq 'CONT') { } 0 0 0
elsif ($signal eq 'TSTP') { } 0 0 0
56  
57 0 $self->session_reload();
58  
59 } elsif ($signal eq 'CONT') {
60  
61 0 $poe_kernel->sig_handled();
62 0 $self->_current_state(SERVICE_CONTINUE_PENDING);
63  
64 } elsif ($signal eq 'TSTP') {
65  
66 0 $poe_kernel->sig_handled();
67 0 $self->_current_state(SERVICE_PAUSE_PENDING);
68  
69 } else { # INT, TERM, QUIT
70  
71 0 $poe_kernel->sig_handled();
72 0 $self->_current_state(SERVICE_CONTROL_SHUTDOWN);
73  
74 }
75  
76 }
77  
78 # ----------------------------------------------------------------------
79 # Public Events
80 # ----------------------------------------------------------------------
81  
82 # ----------------------------------------------------------------------
83 # Private Events
84 # ----------------------------------------------------------------------
85  
86 sub poll {
87 0 ▶ my ($self) = $_[OBJECT];
Subroutine poll: not called CC=13 SCAR=52.0
Pod documented
88  
89 0 my $stat;
90 0 my $alias = $self->alias;
91 0 my $delay = $self->poll_interval;
92 0 my $state = $self->_current_state();
93  
94 0 $self->log->debug("$alias: entering _poll()");
95 0 $self->log->debug("$alias: state = $state");
96  
97 0 ▶ if ($state == SERVICE_START_PENDING) {
Branch: 7 branches
BranchTrueFalseTotal
if ($state == 1) { } 0 0 0
elsif ($state == 2) { } 0 0 0
elsif ($state == 3) { } 0 0 0
elsif ($state == 4) { } 0 0 0
elsif ($state == 6) { } 0 0 0
elsif ($state == 7) { } 0 0 0
elsif ($state == 5) { } 0 0 0
98  
99 0 $self->log->debug("$alias: state = SERVICE_START_PENDING");
100  
101 # Initialization code
102  
103 0 $self->last_state(SERVICE_START_PENDING);
104 0 $self->_current_state(SERVICE_START_PENDING, 6000);
105  
106 # Initialization code
107 # ...do whatever you need to do to start...
108  
109 0 $self->_service_startup();
110 0 $self->last_state(SERVICE_RUNNING);
111  
112 } elsif ($state == SERVICE_STOP_PENDING) {
113  
114 0 $self->log->debug("$alias: state = SERVICE_STOP_PENDING");
115  
116 # Stopping...
117  
118 0 $self->last_state(SERVICE_STOPPED);
119  
120 } elsif ($state == SERVICE_PAUSE_PENDING) {
121  
122 0 $self->log->debug("$alias: state = SERVICE_PAUSE_PENDING");
123  
124 # Pausing...
125  
126 0 $self->_service_paused();
127 0 $self->last_state(SERVICE_PAUSED);
128  
129 } elsif ($state == SERVICE_CONTINUE_PENDING) {
130  
131 0 $self->log->debug("alias: state = SERVICE_CONTINUE_PENDING");
132  
133 # Resuming...
134  
135 0 ▶ if ($self->last_state == SERVICE_PAUSED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last_state == 8) { } 0 0 0
136  
137 0 $self->_service_resumed();
138 0 $self->last_state(SERVICE_RUNNING);
139  
140 } else {
141  
142 0 $self->log->info_msg('service_unpaused');
143  
144 }
145  
146 } elsif ($state == SERVICE_RUNNING) {
147  
148 0 $self->log->debug("$alias: state = SERVICE_RUNNING");
149  
150 # Running...
151 #
152 # Note that here you want to check that the state
153 # is indeed SERVICE_RUNNING. Even though the Running
154 # callback is called it could have done so before
155 # calling the "Start" callback.
156 #
157  
158 0 ▶ if ($self->last_state == SERVICE_RUNNING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last_state == 6) 0 0 0
159  
160 0 $self->_service_idle();
161 0 $self->last_state(SERVICE_RUNNING);
162  
163 }
164  
165 } elsif ($state == SERVICE_STOPPED) {
166  
167 0 $self->log->debug("$alias: state = SERVICE_STOPPED");
168  
169 # stopped...
170  
171 0 $delay = 0;
172 0 $poe_kernel->post($alias, 'session_shutdown');
173 0 $self->last_state(SERVICE_STOPPED);
174  
175 } elsif ($state == SERVICE_CONTROL_SHUTDOWN) {
176  
177 0 $self->log->debug("$alias: state = SERVICE_CONTROL_SHUTDOWN");
178  
179 # shutdown...
180  
181 0 ▶ unless ($self->last_state == SERVICE_PAUSED) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->last_state == 8) 0 0 0
182  
183 0 $self->_service_shutdown();
184 0 $delay = $self->shutdown_interval;
185 0 $self->last_state(SERVICE_STOP_PENDING);
186  
187 }
188  
189 }
190  
191 # tell the SCM what is going on
192  
193 0 $self->_current_state($self->last_state, $delay);
194  
195 # queue the next polling interval
196  
197 0 ▶ unless ($delay == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($delay == 0) 0 0 0
198  
199 0 $stat = $poe_kernel->delay('poll', $delay);
200 0 ▶ $self->log->error_msg('service_que_delay', $alias, $stat) if ($stat != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $stat != 0 0 0 0
201  
202 }
203  
204 0 $self->log->debug("$alias: leaving _poll()");
205  
206 }
207  
208 # ----------------------------------------------------------------------
209 # Private Methods
210 # ----------------------------------------------------------------------
211  
212 sub _current_state {
213 0 ▶ my $self = shift;
Subroutine _current_state: not called CC=2 SCAR=17.9
214 0 my ($state, $delay) = $self->validate_params(\@_, [
215 { optional => 1, default => undef },
216 { optional => 1, default => 0 },
217 ]);
218  
219 0 ▶ if (defined($state)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state) 0 0 0
220  
221 0 $self->{state} = $state;
222  
223 }
224  
225 0 return $self->{state};
226  
227 }
228  
229 1 1;
230  
231 __END__
232  
233 =head1 NAME
234  
235 XAS::Lib::Service::Unix - A mixin class for Unix Services
236  
237 =head1 DESCRIPTION
238  
239 This module is a mixin that provides an interface between a Unix like system
240 and a Service. It responds to these additional signals.
241  
242 TSTP - pause the session
243 CONT - resume the session
244  
245 It allows POE to manage the scheduling of sessions while handling the
246 additional signals to emulate the Windows SCM.
247  
248 =head1 METHODS
249  
250 =head2 init_service
251  
252 Perform initialize for this mixin.
253  
254 =head2 session_interrupt
255  
256 Handle Unix signals and translate them into service events.
257  
258 =head2 poll
259  
260 Handle the services polling loop.
261  
262 =head1 SEE ALSO
263  
264 =over 4
265  
266 =item L<XAS|XAS>
267  
268 =item L<XAS::Lib::Service|XAS::Lib::Service>
269  
270 =back
271  
272 =head1 AUTHOR
273  
274 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
275  
276 =head1 COPYRIGHT AND LICENSE
277  
278 Copyright (C) 2014 Kevin L. Esteb
279  
280 This is free software; you can redistribute it and/or modify it under
281 the terms of the Artistic License 2.0. For details, see the full text
282 of the license at http://www.perlfoundation.org/artistic_license_2_0.
283  
284 =cut
« blib/lib/XAS/Lib/Stomp/Parser.pm ↑ Summary blib/lib/XAS/Lib/Process.pm »