blib/lib/XAS/Lib/Services.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 159 CC 15 SCAR 44.9
CC
15
Cov
0%
_poll
182.0
BEGIN
6.0
register
6.0
CRAP 89.3
1 package XAS::Lib::Services;
2  
3 our $VERSION = '0.03';
4  
5 use POE;
6  
7 my $mixin;
8 BEGIN {
9 $mixin = ($^O eq 'MSWin32')
10 ? 'XAS::Lib::Services::Win32'
11 : 'XAS::Lib::Services::Unix';
12 }
13  
14 use XAS::Class
15 debug => 0,
16 version => $VERSION,
17 base => 'XAS::Lib::Session',
18 mixin => $mixin,
19 accessors => 'sessions',
20 mutators => 'last_state',
21 constants => 'DELIMITER',
22 vars => {
23 PARAMS => {
24 -poll_interval => { optional => 1, default => 2 },
25 -shutdown_interval => { optional => 1, default => 25 },
26 }
27 }
28 ;
29  
30 # ----------------------------------------------------------------------
31 # Public Methods
32 # ----------------------------------------------------------------------
33  
34 sub register {
35 my $self = shift;
36  
37 my ($sessions) = $self->validate_params(\@_, [1]);
38  
39 if (ref($sessions) eq 'ARRAYREF') {
40  
41 foreach my $session (@$sessions) {
42  
43 push(@{$self->{sessions}}, $session);
44  
45 }
46  
47 } else {
48  
49 my @parts = split(DELIMITER, $sessions);
50  
51 foreach my $session (@parts) {
52  
53 push(@{$self->{sessions}}, $session);
54  
55 }
56  
57 }
58  
59 }
60  
61 # ----------------------------------------------------------------------
62 # Public Events
63 # ----------------------------------------------------------------------
64  
65 sub session_shutdown {
66 my $self = shift;
67  
68 $poe_kernel->delay('poll');
69 $self->SUPER::session_shutdown();
70  
71 }
72  
73 # ----------------------------------------------------------------------
74 # Private Events
75 # ----------------------------------------------------------------------
76  
77 sub _session_init {
78 my ($self) = $_[OBJECT];
79  
80 my $alias = $self->alias;
81  
82 $self->log->debug("$alias: _session_init()");
83  
84 $poe_kernel->state('poll', $self, '_poll');
85  
86 $self->last_state(SERVICE_START_PENDING);
87 $self->_current_state(SERVICE_START_PENDING);
88  
89 $self->init_service();
90 $self->session_initialize();
91  
92 $poe_kernel->post($alias, 'poll');
93  
94 }
95  
96 sub _poll {
97 my ($self) = $_[OBJECT];
98  
99 my $stat;
100 my $alias = $self->alias;
101 my $delay = $self->poll_interval;
102 my $state = $self->_current_state();
103  
104 $self->log->debug("$alias: entering _poll()");
105 $self->log->debug("$alias: state = $state");
106  
107 if ($state == SERVICE_START_PENDING) {
108  
109 $self->log->debug("$alias: state = SERVICE_START_PENDING");
110  
111 # Initialization code
112  
113 $self->last_state(SERVICE_START_PENDING);
114 $self->_current_state(SERVICE_START_PENDING, 6000);
115  
116 # Initialization code
117 # ...do whatever you need to do to start...
118  
119 $self->_service_startup();
120 $self->last_state(SERVICE_RUNNING);
121  
122 } elsif ($state == SERVICE_STOP_PENDING) {
123  
124 $self->log->debug("$alias: state = SERVICE_STOP_PENDING");
125  
126 # Stopping...
127  
128 $self->last_state(SERVICE_STOPPED);
129  
130 } elsif ($state == SERVICE_PAUSE_PENDING) {
131  
132 $self->log->debug("$alias: state = SERVICE_PAUSE_PENDING");
133  
134 # Pausing...
135  
136 $self->_service_paused();
137 $self->last_state(SERVICE_PAUSED);
138  
139 } elsif ($state == SERVICE_CONTINUE_PENDING) {
140  
141 $self->log->debug("$alias: state = SERVICE_CONTINUE_PENDING");
142  
143 # Resuming...
144  
145 if ($self->last_state == SERVICE_PAUSED) {
146  
147 $self->_service_resumed();
148 $self->last_state(SERVICE_RUNNING);
149  
150 } else {
151  
152 $self->log->info_msg('unpaused');
153  
154 }
155  
156 } elsif ($state == SERVICE_RUNNING) {
157  
158 $self->log->debug("$alias: state = SERVICE_RUNNING");
159  
160 # Running...
161 #
162 # Note that here you want to check that the state
163 # is indeed SERVICE_RUNNING. Even though the Running
164 # callback is called it could have done so before
165 # calling the "Start" callback.
166 #
167  
168 if ($self->last_state == SERVICE_RUNNING) {
169  
170 $self->_service_idle();
171 $self->last_state(SERVICE_RUNNING);
172  
173 }
174  
175 } elsif ($state == SERVICE_STOPPED) {
176  
177 $self->log->debug("$alias: state = SERVICE_STOPPED");
178  
179 # stopped...
180  
181 $delay = 0;
182 $poe_kernel->post($alias, 'session_shutdown');
183 $self->last_state(SERVICE_STOPPED);
184  
185 } elsif ($state == SERVICE_CONTROL_SHUTDOWN) {
186  
187 $self->log->debug("$alias: state = SERVICE_CONTROL_SHUTDOWN");
188  
189 # shutdown...
190  
191 unless ($self->last_state == SERVICE_PAUSED) {
192  
193 $self->_service_shutdown();
194 $delay = $self->shutdown_interval;
195 $self->last_state(SERVICE_STOP_PENDING);
196  
197 }
198  
199 }
200  
201 # tell the SCM what is going on
202  
203 $self->_current_state($self->last_state, $delay);
204  
205 # queue the next polling interval
206  
207 unless ($delay == 0) {
208  
209 $stat = $poe_kernel->delay('poll', $self->poll_interval);
210 $self->log->error("unable to queue delay - $stat") if ($stat != 0);
211  
212 }
213  
214 $self->log->debug("$alias: leaving _poll()");
215  
216 }
217  
218 # ----------------------------------------------------------------------
219 # Private Methods
220 # ----------------------------------------------------------------------
221  
222 sub init {
223 my $class = shift;
224  
225 my $self = $class->SUPER::init(@_);
226  
227 $self->{sessions} = [];
228  
229 return $self;
230  
231 }
232  
233 sub _service_startup {
234 my $self = shift;
235  
236 my $alias = $self->alias;
237  
238 $self->log->debug("$alias: _service_startup");
239  
240 foreach my $session (@{$self->sessions}) {
241  
242 $poe_kernel->post($session, 'session_startup');
243  
244 }
245  
246 }
247  
248 sub _service_shutdown {
249 my $self = shift;
250  
251 my $alias = $self->alias;
252  
253 $self->log->debug("$alias: _service_shutdown");
254  
255 foreach my $session (@{$self->sessions}) {
256  
257 $poe_kernel->post($session, 'session_shutdown');
258  
259 }
260  
261 }
262  
263 sub _service_idle {
264 my $self = shift;
265  
266 my $alias = $self->alias;
267  
268 $self->log->debug("$alias: _service_idle");
269  
270 foreach my $session (@{$self->sessions}) {
271  
272 $poe_kernel->post($session, 'session_idle');
273  
274 }
275  
276 }
277  
278 sub _service_paused {
279 my $self = shift;
280  
281 my $alias = $self->alias;
282  
283 $self->log->debug("$alias: _service_paused");
284  
285 foreach my $session (@{$self->sessions}) {
286  
287 $poe_kernel->post($session, 'session_pause');
288  
289 }
290  
291 }
292  
293 sub _service_resumed {
294 my $self = shift;
295  
296 my $alias = $self->alias;
297  
298 $self->log->debug("$alias: _service_resumed");
299  
300 foreach my $session (@{$self->sessions}) {
301  
302 $poe_kernel->post($session, 'session_resume');
303  
304 }
305  
306 }
307  
308 1;
309  
310 __END__
311  
312 =head1 NAME
313  
314 XAS::Lib::Services - A class to interact with Services
315  
316 =head1 SYNOPSIS
317  
318 use POE;
319 use XAS::Lib::Service;
320 use XAS::Lib::Services;
321  
322 my $service = XAS::Lib::Services->new(
323 -alias => 'service',
324 -poll_interval => 2,
325 -shutdown_interval => 25
326 );
327  
328 my $task = XAS::Lib::Service->new(
329 -alias => 'task'
330 );
331  
332 $service->register('task');
333 $poe_kernel->run();
334  
335 =head1 DESCRIPTION
336  
337 This module provides a generic interface to "Services". A Service is
338 a managed background process. It responds to external events. On Windows
339 this would be responding to commands from the Service Control Manager.
340 On Unix this would be responding to a special set of signals. This module
341 provides an event loop that can interact those external events.
342  
343 When an external event happens this module will trap it and generate a POE
344 event. This event is then sent to all interested modules. The following POE
345 events have been defined:
346  
347 =over 4
348  
349 =item B<session_startup>
350  
351 This is fired when your process starts up and is used to initialize what ever
352 processing you are going to do. On a network server, this may be opening a
353 port to listen on.
354  
355 =item B<session_shutdown>
356  
357 This is fired when your process is shutting down.
358  
359 =item B<session_pause>
360  
361 This is fired when your process needs to "pause".
362  
363 =item B<session_resume>
364  
365 This is fired when your process needs to "resume".
366  
367 =item B<session_idle>
368  
369 This is fired at every poll_interval.
370  
371 =back
372  
373 These events follow closely the model defined by the Windows Service
374 Control Manager interface. To use these events it is best to inherit from
375 L<XAS::Lib::Service>.
376  
377 =head1 METHODS
378  
379 =head2 new()
380  
381 This method is used to initialize the service. It takes the following
382 parameters:
383  
384 =over 4
385  
386 =item B<-alias>
387  
388 The name of this POE session.
389  
390 =item B<-poll_interval>
391  
392 This is the interval were the SCM sends SERVICE_RUNNING message. The
393 default is every 2 seconds.
394  
395 =item B<-shutdown_interval>
396  
397 This is the interval to pause the system shutdown so that the service
398 can cleanup after itself. The default is 25 seconds.
399  
400 =back
401  
402 It also use parameters from L<XAS::Lib::Session>.
403  
404 =head2 register($session)
405  
406 This allows your process to register whatever modules you want events sent too.
407  
408 =over 4
409  
410 =item B<$session>
411  
412 This can be an array reference or a text string. The text string may be
413 delimited with commas.
414  
415 =back
416  
417  
418 =head1 SEE ALSO
419  
420 =over 4
421  
422 =item L<XAS|XAS>
423  
424 =back
425  
426 =head1 AUTHOR
427  
428 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
429  
430 =head1 COPYRIGHT AND LICENSE
431  
432 Copyright (C) 2014 Kevin L. Esteb
433  
434 This library is free software; you can redistribute it and/or modify
435 it under the same terms as Perl itself, either Perl version 5.8.8 or,
436 at your option, any later version of Perl 5 you may have available.
437  
438 See L<http://dev.perl.org/licenses/> for more information.
439  
440 =cut