File Coverage

blib/lib/XAS/Lib/Service/Unix.pm
Criterion Covered Total %
statement 6 70 8.5
branch 0 32 0.0
condition n/a
subroutine 2 6 33.3
pod 3 3 100.0
total 11 111 9.9


line stmt bran cond sub pod time code
1             package XAS::Lib::Service::Unix;
2              
3             our $VERSION = '0.01';
4              
5 1     1   857 use POE;
  1         2  
  1         4  
6              
7             use XAS::Class
8 1         11 debug => 0,
9             version => $VERSION,
10             base => 'XAS::Base',
11             utils => ':validation',
12             constant => {
13             SERVICE_START_PENDING => 1,
14             SERVICE_STOP_PENDING => 2,
15             SERVICE_PAUSE_PENDING => 3,
16             SERVICE_CONTINUE_PENDING => 4,
17             SERVICE_CONTROL_SHUTDOWN => 5,
18             SERVICE_RUNNING => 6,
19             SERVICE_STOPPED => 7,
20             SERVICE_PAUSED => 8,
21             },
22             mixins => 'init_service _current_state session_interrupt poll
23             SERVICE_START_PENDING SERVICE_STOP_PENDING
24             SERVICE_PAUSE_PENDING SERVICE_CONTINUE_PENDING
25             SERVICE_CONTROL_SHUTDOWN SERVICE_RUNNING
26             SERVICE_STOPPED SERVICE_PAUSED',
27 1     1   228 ;
  1         1  
28              
29             # ----------------------------------------------------------------------
30             # Public Methods
31             # ----------------------------------------------------------------------
32              
33             sub init_service {
34 0     0 1   my $self = shift;
35              
36 0           my $alias = $self->alias;
37              
38 0           $self->log->debug("$alias: entering init_service() - unix");
39              
40 0           $poe_kernel->sig('CONT', 'session_interrupt');
41 0           $poe_kernel->sig('TSTP', 'session_interrupt');
42              
43 0           $self->log->debug("$alias: leaving int_service() - unix");
44              
45             }
46              
47             sub session_interrupt {
48 0     0 1   my $self = shift;
49 0           my $signal = shift;
50              
51 0           my $alias = $self->alias;
52              
53 0           $self->log->debug("$alias: session_interrupt()");
54 0           $self->log->warn_msg('service_signaled', $alias, $signal);
55              
56 0 0         if ($signal eq 'HUP') {
    0          
    0          
57              
58 0           $self->session_reload();
59              
60             } elsif ($signal eq 'CONT') {
61              
62 0           $poe_kernel->sig_handled();
63 0           $self->_current_state(SERVICE_CONTINUE_PENDING);
64              
65             } elsif ($signal eq 'TSTP') {
66              
67 0           $poe_kernel->sig_handled();
68 0           $self->_current_state(SERVICE_PAUSE_PENDING);
69              
70             } else { # INT, TERM, QUIT
71              
72 0           $poe_kernel->sig_handled();
73 0           $self->_current_state(SERVICE_CONTROL_SHUTDOWN);
74              
75             }
76              
77             }
78              
79             # ----------------------------------------------------------------------
80             # Public Events
81             # ----------------------------------------------------------------------
82              
83             # ----------------------------------------------------------------------
84             # Private Events
85             # ----------------------------------------------------------------------
86              
87             sub poll {
88 0     0 1   my ($self) = $_[OBJECT];
89              
90 0           my $stat;
91 0           my $alias = $self->alias;
92 0           my $delay = $self->poll_interval;
93 0           my $state = $self->_current_state();
94              
95 0           $self->log->debug("$alias: entering _poll()");
96              
97 0 0         if ($state == SERVICE_START_PENDING) {
    0          
    0          
    0          
    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 0         if ($self->last_state == SERVICE_PAUSED) {
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 0         if ($self->last_state == SERVICE_RUNNING) {
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 0         unless ($self->last_state == SERVICE_PAUSED) {
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 0         unless ($delay == 0) {
198              
199 0           $stat = $poe_kernel->delay('poll', $delay);
200 0 0         $self->log->error_msg('service_que_delay', $alias, $stat) if ($stat != 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     0     my $self = shift;
214 0           my ($state, $delay) = validate_params(\@_, [
215             { optional => 1, default => undef },
216             { optional => 1, default => 0 },
217             ]);
218              
219 0 0         if (defined($state)) {
220              
221 0           $self->{'state'} = $state;
222              
223             }
224              
225 0           return $self->{'state'};
226              
227             }
228              
229             1;
230              
231             __END__