blib/lib/XAS/Lib/Service/Win32.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 105 CC 14 SCAR 45.1
CC
14
Cov
0%
poll
156.0
init_service
6.0
_current_state
6.0
CRAP 90.8
1 package XAS::Lib::Service::Win32;
2  
3 our $VERSION = '0.01';
4  
5 use POE;
6 use Win32;
7 use Win32::Daemon;
8  
9 use XAS::Class
10 debug => 0,
11 version => $VERSION,
12 base => 'XAS::Base',
13 mixins => 'init_service _current_state poll
14 SERVICE_START_PENDING
15 SERVICE_STOP_PENDING SERVICE_PAUSE_PENDING
16 SERVICE_CONTINUE_PENDING SERVICE_CONTROL_SHUTDOWN
17 SERVICE_RUNNING SERVICE_STOPPED SERVICE_PAUSED'
18 ;
19  
20 # ----------------------------------------------------------------------
21 # Public Methods
22 # ----------------------------------------------------------------------
23  
24 sub init_service {
25 my $self = shift;
26  
27 my $alias = $self->alias;
28  
29 $self->log->debug("$alias: entering init_service() - win32");
30  
31 unless (Win32::Daemon::StartService()) {
32  
33 $self->throw_msg(
34 'xas.lib.service.win32.startup.startservice',
35 'service_noservice',
36 _get_error()
37 );
38  
39 }
40  
41 $self->log->debug("$alias: leaving init_service() - win32");
42  
43 }
44  
45 # ----------------------------------------------------------------------
46 # Public Events
47 # ----------------------------------------------------------------------
48  
49 # ----------------------------------------------------------------------
50 # Private Events
51 # ----------------------------------------------------------------------
52  
53 sub poll {
54 my ($self) = $_[OBJECT];
55  
56 my $stat;
57 my $alias = $self->alias;
58 my $delay = $self->poll_interval;
59 my $state = $self->_current_state();
60  
61 $self->log->debug("$alias: entering _poll()");
62 $self->log->debug("$alias: state = $state");
63  
64 if ($state == SERVICE_START_PENDING) {
65  
66 $self->log->debug("$alias: state = SERVICE_START_PENDING");
67  
68 # Initialization code
69  
70 $self->last_state(SERVICE_START_PENDING);
71 $self->_current_state(SERVICE_START_PENDING, 6000);
72  
73 # Initialization code
74 # ...do whatever you need to do to start...
75  
76 $self->_service_startup();
77 $self->last_state(SERVICE_RUNNING);
78  
79 } elsif ($state == SERVICE_STOP_PENDING) {
80  
81 $self->log->debug("$alias: state = SERVICE_STOP_PENDING");
82  
83 # Stopping...
84  
85 $self->last_state(SERVICE_STOPPED);
86  
87 } elsif ($state == SERVICE_PAUSE_PENDING) {
88  
89 $self->log->debug("$alias: state = SERVICE_PAUSE_PENDING");
90  
91 # Pausing...
92  
93 $self->_service_paused();
94 $self->last_state(SERVICE_PAUSED);
95  
96 } elsif ($state == SERVICE_CONTINUE_PENDING) {
97  
98 $self->log->debug("alias: state = SERVICE_CONTINUE_PENDING");
99  
100 # Resuming...
101  
102 if ($self->last_state == SERVICE_PAUSED) {
103  
104 $self->_service_resumed();
105 $self->last_state(SERVICE_RUNNING);
106  
107 } else {
108  
109 $self->log->info_msg('service_unpaused');
110  
111 }
112  
113 } elsif ($state == SERVICE_RUNNING) {
114  
115 $self->log->debug("$alias: state = SERVICE_RUNNING");
116  
117 # Running...
118 #
119 # Note that here you want to check that the state
120 # is indeed SERVICE_RUNNING. Even though the Running
121 # callback is called it could have done so before
122 # calling the "Start" callback.
123 #
124  
125 if ($self->last_state == SERVICE_RUNNING) {
126  
127 $self->_service_idle();
128 $self->last_state(SERVICE_RUNNING);
129  
130 }
131  
132 } elsif ($state == SERVICE_STOPPED) {
133  
134 $self->log->debug("$alias: state = SERVICE_STOPPED");
135  
136 # stopped...
137  
138 $delay = 0;
139 $self->_service_shutdown();
140 $self->last_state(SERVICE_STOPPED);
141  
142 } elsif ($state == SERVICE_CONTROL_SHUTDOWN) {
143  
144 $self->log->debug("$alias: state = SERVICE_CONTROL_SHUTDOWN");
145  
146 # shutdown...
147  
148 $self->_service_shutdown();
149 $delay = $self->shutdown_interval;
150 $self->last_state(SERVICE_STOP_PENDING);
151  
152 }
153  
154 # tell the SCM what is going on
155  
156 $self->_current_state($self->last_state, $delay);
157  
158 # queue the next polling interval
159  
160 unless ($delay == 0) {
161  
162 $stat = $poe_kernel->delay('poll', $delay);
163 $self->log->error('service_que_delay', $alias, $stat) if ($stat != 0);
164  
165 }
166  
167 $self->log->debug("$alias: leaving _poll()");
168  
169 }
170  
171 # ----------------------------------------------------------------------
172 # Private Methods
173 # ----------------------------------------------------------------------
174  
175 sub _get_error {
176  
177 return(Win32::FormatMessage(Win32::Daemon::GetLastError()));
178  
179 }
180  
181 sub _current_state {
182 my $self = shift;
183  
184 my ($state, $delay) = $self->validate_params(\@_, [
185 { optional => 1, default => undef },
186 { optional => 1, default => 0 },
187 ]);
188  
189 if (defined($state)) {
190  
191 Win32::Daemon::State($state, $delay);
192  
193 }
194  
195 return Win32::Daemon::State();
196  
197 }
198  
199 1;
200  
201 __END__
202  
203 =head1 NAME
204  
205 XAS::Lib::Service::Win32 - A mixin class for Win32 Services
206  
207 =head1 DESCRIPTION
208  
209 This module is a mixin that provides an interface between Services and
210 the Win32 SCM. It allows POE to manage the scheduling of sessions while
211 referencing the Win32 SCM event stream.
212  
213 =head1 METHODS
214  
215 =head2 init_service
216  
217 Perform initialize for this mixin.
218  
219 =head2 poll
220  
221 Handle the services polling loop.
222  
223 =head1 SEE ALSO
224  
225 =over 4
226  
227 =item L<XAS|XAS>
228  
229 =item L<XAS::Lib::Service|XAS::Lib::Service>
230  
231 =back
232  
233 =head1 AUTHOR
234  
235 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
236  
237 =head1 COPYRIGHT AND LICENSE
238  
239 Copyright (C) 2014 Kevin L. Esteb
240  
241 This is free software; you can redistribute it and/or modify it under
242 the terms of the Artistic License 2.0. For details, see the full text
243 of the license at http://www.perlfoundation.org/artistic_license_2_0.
244  
245 =cut