| 1 | package XAS::Lib::Service; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.04'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use POE; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | my ($mixin, $shutdown); | |||||||||
| 8 | |||||||||||
| 9 | BEGIN { | ||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | if ($^O eq 'MSWin32') { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=8.4
Branch: 1 branch
| |||||||||||
| 12 | |||||||||||
| 13 | 0 | $mixin = 'XAS::Lib::Service::Win32'; | |||||||||
| 14 | 0 | $shutdown = 25; | |||||||||
| 15 | |||||||||||
| 16 | } else { | ||||||||||
| 17 | |||||||||||
| 18 | 1 | $mixin = 'XAS::Lib::Service::Unix'; | |||||||||
| 19 | 1 | $shutdown = 2; | |||||||||
| 20 | |||||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | use XAS::Class | ||||||||||
| 26 | 1 | debug => 0, | |||||||||
| 27 | version => $VERSION, | ||||||||||
| 28 | base => 'XAS::Lib::POE::Session', | ||||||||||
| 29 | mixin => $mixin, | ||||||||||
| 30 | accessors => 'sessions', | ||||||||||
| 31 | mutators => 'last_state', | ||||||||||
| 32 | constants => 'DELIMITER', | ||||||||||
| 33 | vars => { | ||||||||||
| 34 | PARAMS => { | ||||||||||
| 35 | -poll_interval => { optional => 1, default => 2 }, | ||||||||||
| 36 | -shutdown_interval => { optional => 1, default => $shutdown }, | ||||||||||
| 37 | -alias => { optional => 1, default => 'services' }, | ||||||||||
| 38 | } | ||||||||||
| 39 | } | ||||||||||
| 40 | 1 | ▶ | ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | #use Data::Dumper; | ||||||||||
| 43 | |||||||||||
| 44 | # ---------------------------------------------------------------------- | ||||||||||
| 45 | # Public Methods | ||||||||||
| 46 | # ---------------------------------------------------------------------- | ||||||||||
| 47 | |||||||||||
| 48 | sub register { | ||||||||||
| 49 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
register: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 50 | 0 | my $sessions = $self->validate_params(\@_, [ | |||||||||
| 51 | (0) x (@_) | ||||||||||
| 52 | ]); | ||||||||||
| 53 | |||||||||||
| 54 | 0 | foreach my $session (@$sessions) { | |||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | if (my @parts = split(DELIMITER, $session)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | foreach my $part (@parts) { | |||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | next if ($part eq ''); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | push(@{$self->{sessions}}, $part); | |||||||||
| 62 | |||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | } else { | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | next if ($session eq ''); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | push(@{$self->{sessions}}, $session); | |||||||||
| 69 | |||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | sub session_shutdown { | ||||||||||
| 77 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
session_shutdown: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 78 | |||||||||||
| 79 | 0 | $poe_kernel->delay('poll'); | |||||||||
| 80 | 0 | $self->SUPER::session_shutdown(); | |||||||||
| 81 | |||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | # ---------------------------------------------------------------------- | ||||||||||
| 85 | # Public Events | ||||||||||
| 86 | # ---------------------------------------------------------------------- | ||||||||||
| 87 | |||||||||||
| 88 | # ---------------------------------------------------------------------- | ||||||||||
| 89 | # Private Events | ||||||||||
| 90 | # ---------------------------------------------------------------------- | ||||||||||
| 91 | |||||||||||
| 92 | sub _session_init { | ||||||||||
| 93 | 0 | ▶ | my ($self) = $_[OBJECT]; | ||||||||
|
Subroutine
_session_init: not called CC=1 SCAR=6.9
| |||||||||||
| 94 | |||||||||||
| 95 | 0 | my $alias = $self->alias; | |||||||||
| 96 | |||||||||||
| 97 | 0 | $self->log->debug("$alias: _session_init()"); | |||||||||
| 98 | |||||||||||
| 99 | 0 | $poe_kernel->state('poll', $self); | |||||||||
| 100 | |||||||||||
| 101 | 0 | $self->last_state(SERVICE_START_PENDING); | |||||||||
| 102 | 0 | $self->_current_state(SERVICE_START_PENDING); | |||||||||
| 103 | |||||||||||
| 104 | 0 | $self->init_service(); | |||||||||
| 105 | 0 | $self->session_initialize(); | |||||||||
| 106 | |||||||||||
| 107 | 0 | $poe_kernel->post($alias, 'poll'); | |||||||||
| 108 | |||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | # ---------------------------------------------------------------------- | ||||||||||
| 112 | # Private Methods | ||||||||||
| 113 | # ---------------------------------------------------------------------- | ||||||||||
| 114 | |||||||||||
| 115 | sub init { | ||||||||||
| 116 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 117 | |||||||||||
| 118 | 0 | $poe_kernel->run(); # this only initializes POE | |||||||||
| 119 | |||||||||||
| 120 | 0 | my $self = $class->SUPER::init(@_); | |||||||||
| 121 | |||||||||||
| 122 | 0 | $self->{sessions} = []; | |||||||||
| 123 | |||||||||||
| 124 | 0 | return $self; | |||||||||
| 125 | |||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | sub _service_startup { | ||||||||||
| 129 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_service_startup: not called CC=2 SCAR=17.9
| |||||||||||
| 130 | |||||||||||
| 131 | 0 | my $alias = $self->alias; | |||||||||
| 132 | |||||||||||
| 133 | 0 | $self->log->debug("$alias: _service_startup"); | |||||||||
| 134 | |||||||||||
| 135 | 0 | foreach my $session (@{$self->sessions}) { | |||||||||
| 136 | |||||||||||
| 137 | 0 | $poe_kernel->post($session, 'session_startup'); | |||||||||
| 138 | |||||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | sub _service_shutdown { | ||||||||||
| 144 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_service_shutdown: not called CC=2 SCAR=17.9
| |||||||||||
| 145 | |||||||||||
| 146 | 0 | my $alias = $self->alias; | |||||||||
| 147 | |||||||||||
| 148 | 0 | $self->log->debug("$alias: _service_shutdown"); | |||||||||
| 149 | |||||||||||
| 150 | 0 | foreach my $session (@{$self->sessions}) { | |||||||||
| 151 | |||||||||||
| 152 | 0 | $poe_kernel->post($session, 'session_shutdown'); | |||||||||
| 153 | |||||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | sub _service_idle { | ||||||||||
| 159 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_service_idle: not called CC=2 SCAR=17.9
| |||||||||||
| 160 | |||||||||||
| 161 | 0 | my $alias = $self->alias; | |||||||||
| 162 | |||||||||||
| 163 | 0 | $self->log->debug("$alias: _service_idle"); | |||||||||
| 164 | |||||||||||
| 165 | 0 | foreach my $session (@{$self->sessions}) { | |||||||||
| 166 | |||||||||||
| 167 | 0 | $poe_kernel->post($session, 'session_idle'); | |||||||||
| 168 | |||||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | sub _service_paused { | ||||||||||
| 174 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_service_paused: not called CC=2 SCAR=17.9
| |||||||||||
| 175 | |||||||||||
| 176 | 0 | my $alias = $self->alias; | |||||||||
| 177 | |||||||||||
| 178 | 0 | $self->log->debug("$alias: _service_paused"); | |||||||||
| 179 | |||||||||||
| 180 | 0 | foreach my $session (@{$self->sessions}) { | |||||||||
| 181 | |||||||||||
| 182 | 0 | $poe_kernel->post($session, 'session_pause'); | |||||||||
| 183 | |||||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | sub _service_resumed { | ||||||||||
| 189 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_service_resumed: not called CC=2 SCAR=17.9
| |||||||||||
| 190 | |||||||||||
| 191 | 0 | my $alias = $self->alias; | |||||||||
| 192 | |||||||||||
| 193 | 0 | $self->log->debug("$alias: _service_resumed"); | |||||||||
| 194 | |||||||||||
| 195 | 0 | foreach my $session (@{$self->sessions}) { | |||||||||
| 196 | |||||||||||
| 197 | 0 | $poe_kernel->post($session, 'session_resume'); | |||||||||
| 198 | |||||||||||
| 199 | } | ||||||||||
| 200 | |||||||||||
| 201 | } | ||||||||||
| 202 | |||||||||||
| 203 | 1 | 1; | |||||||||
| 204 | |||||||||||
| 205 | __END__ | ||||||||||
| 206 | |||||||||||
| 207 | =head1 NAME | ||||||||||
| 208 | |||||||||||
| 209 | XAS::Lib::Service - A class to interact with Services | ||||||||||
| 210 | |||||||||||
| 211 | =head1 SYNOPSIS | ||||||||||
| 212 | |||||||||||
| 213 | use XAS::Lib::Service; | ||||||||||
| 214 | |||||||||||
| 215 | my $service = XAS::Lib::Service->new( | ||||||||||
| 216 | -alias => 'service', | ||||||||||
| 217 | -poll_interval => 2, | ||||||||||
| 218 | -shutdown_interval => 25 | ||||||||||
| 219 | ); | ||||||||||
| 220 | |||||||||||
| 221 | $service->run(); | ||||||||||
| 222 | |||||||||||
| 223 | =head1 DESCRIPTION | ||||||||||
| 224 | |||||||||||
| 225 | This module provides a generic interface to "Services". A Service is | ||||||||||
| 226 | a managed background process. It responds to external events. On Windows | ||||||||||
| 227 | this would be responding to commands from the SCM. On Unix this would | ||||||||||
| 228 | be responding to a special set of signals. This module provides an | ||||||||||
| 229 | event loop that can interact those external events. The module | ||||||||||
| 230 | L<XAS::Lib::POE::Service|XAS::Lib::POE::Service> can interact with | ||||||||||
| 231 | this event loop. | ||||||||||
| 232 | |||||||||||
| 233 | =head1 METHODS | ||||||||||
| 234 | |||||||||||
| 235 | =head2 new() | ||||||||||
| 236 | |||||||||||
| 237 | This method is used to initialize the service. This module inherits from | ||||||||||
| 238 | L<XAS::Lib::POE::Session|XAS::Lib::POE::Session>. It takes the following | ||||||||||
| 239 | parameters: | ||||||||||
| 240 | |||||||||||
| 241 | =over 4 | ||||||||||
| 242 | |||||||||||
| 243 | =item B<-alias> | ||||||||||
| 244 | |||||||||||
| 245 | The name of this POE session. Defaults to 'service'; | ||||||||||
| 246 | |||||||||||
| 247 | =item B<-poll_interval> | ||||||||||
| 248 | |||||||||||
| 249 | This is the interval were the SCM sends SERVICE_RUNNING message. The | ||||||||||
| 250 | default is every 2 seconds. | ||||||||||
| 251 | |||||||||||
| 252 | =item B<-shutdown_interval> | ||||||||||
| 253 | |||||||||||
| 254 | This is the interval to pause the system shutdown so that the service | ||||||||||
| 255 | can cleanup after itself. The default is 25 seconds. | ||||||||||
| 256 | |||||||||||
| 257 | =back | ||||||||||
| 258 | |||||||||||
| 259 | =head2 register($session) | ||||||||||
| 260 | |||||||||||
| 261 | This allows your process to register whatever modules you want events sent too. | ||||||||||
| 262 | |||||||||||
| 263 | =over 4 | ||||||||||
| 264 | |||||||||||
| 265 | =item B<$session> | ||||||||||
| 266 | |||||||||||
| 267 | This can be an array reference or a text string. The text string may be | ||||||||||
| 268 | delimited with commas. This will be the POE alias for each session. | ||||||||||
| 269 | |||||||||||
| 270 | =back | ||||||||||
| 271 | |||||||||||
| 272 | =head1 EVENTS | ||||||||||
| 273 | |||||||||||
| 274 | When an external event happens this module will trap it and generate a POE | ||||||||||
| 275 | event. These events follow closely the model defined by the Windows Service | ||||||||||
| 276 | Control Manager interface. The event is then sent to all interested modules. | ||||||||||
| 277 | The following POE events have been defined: | ||||||||||
| 278 | |||||||||||
| 279 | =head2 session_startup | ||||||||||
| 280 | |||||||||||
| 281 | This is fired when your process starts up and is used to initialize what ever | ||||||||||
| 282 | processing you are going to do. On a network server, this may be opening a | ||||||||||
| 283 | port to listen on. | ||||||||||
| 284 | |||||||||||
| 285 | =head2 session_shutdown | ||||||||||
| 286 | |||||||||||
| 287 | This is fired when your process is shutting down. | ||||||||||
| 288 | |||||||||||
| 289 | =head2 session_pause | ||||||||||
| 290 | |||||||||||
| 291 | This is fired when your process needs to "pause". | ||||||||||
| 292 | |||||||||||
| 293 | =head2 session_resume | ||||||||||
| 294 | |||||||||||
| 295 | This is fired when your process needs to "resume". | ||||||||||
| 296 | |||||||||||
| 297 | =head2 session_idle | ||||||||||
| 298 | |||||||||||
| 299 | This is fired at every poll_interval. | ||||||||||
| 300 | |||||||||||
| 301 | =head1 SEE ALSO | ||||||||||
| 302 | |||||||||||
| 303 | =over 4 | ||||||||||
| 304 | |||||||||||
| 305 | =item L<XAS|XAS> | ||||||||||
| 306 | |||||||||||
| 307 | =back | ||||||||||
| 308 | |||||||||||
| 309 | =head1 AUTHOR | ||||||||||
| 310 | |||||||||||
| 311 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||
| 312 | |||||||||||
| 313 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 314 | |||||||||||
| 315 | Copyright (C) 2014 Kevin L. Esteb | ||||||||||
| 316 | |||||||||||
| 317 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 318 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||
| 319 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||
| 320 | |||||||||||
| 321 | =cut | ||||||||||