Line 0
Link Here
|
|
|
1 |
package Koha::Icarus::Task; |
2 |
|
3 |
use Modern::Perl; |
4 |
use parent 'Koha::Icarus::Base'; |
5 |
|
6 |
use POE qw(Wheel::Run); |
7 |
use DateTime; |
8 |
use DateTime::Format::Strptime; |
9 |
use JSON; |
10 |
use Module::Load::Conditional qw/can_load/; |
11 |
|
12 |
my $datetime_pattern = DateTime::Format::Strptime->new( |
13 |
pattern => '%F %T', |
14 |
time_zone => 'local', |
15 |
); |
16 |
my $epoch_pattern = DateTime::Format::Strptime->new( |
17 |
pattern => '%s', |
18 |
); |
19 |
|
20 |
sub new { |
21 |
my ($class, $args) = @_; |
22 |
$args = {} unless defined $args; |
23 |
$args->{_component} = "task"; |
24 |
$args->{_id} = "undefined"; |
25 |
return bless ($args, $class); |
26 |
} |
27 |
|
28 |
#NOTE: "spawn" inspired by http://poe.perl.org/?POE_Cookbook/Object_Methods |
29 |
sub spawn { |
30 |
my ($class, $args) = @_; |
31 |
my $self = $class->new($args); |
32 |
my $task_session = POE::Session->create( |
33 |
object_states => [ |
34 |
$self => { |
35 |
_start => "on_task_create", |
36 |
"got_child_stdout" => "on_child_stdout", |
37 |
"got_child_stderr" => "on_child_stderr", |
38 |
"got_child_close" => "on_child_close", |
39 |
"got_child_signal" => "on_child_signal", |
40 |
"got_terminal_signal" => "on_terminal_signal", |
41 |
"child_process_success" => "child_process_success", |
42 |
"got_task_stop" => "on_task_stop", |
43 |
"on_task_init" => "on_task_init", |
44 |
"on_task_start" => "on_task_start", |
45 |
}, |
46 |
], |
47 |
); |
48 |
return $task_session; |
49 |
} |
50 |
|
51 |
sub on_task_create { |
52 |
my ($self, $session, $kernel, $heap) = @_[OBJECT, SESSION, KERNEL, HEAP]; |
53 |
|
54 |
#Trap terminal signals so that the task can stop gracefully. |
55 |
$kernel->sig(INT => "got_terminal_signal"); |
56 |
$kernel->sig(TERM => "got_terminal_signal"); |
57 |
|
58 |
my $task_id = $session->ID; |
59 |
if ($task_id){ |
60 |
#Tell the kernel that this task is waiting for an external action (ie keepalive counter) |
61 |
$kernel->refcount_increment($task_id,"waiting task"); |
62 |
$self->{_id} = $task_id; #Set internal id for logging purposes |
63 |
} |
64 |
|
65 |
my $server_id = $self->{server_id}; |
66 |
if ($server_id){ |
67 |
$heap->{server_id} = $server_id; |
68 |
} |
69 |
|
70 |
my $task = undef; |
71 |
my $message = $self->{message}; |
72 |
if ($message){ |
73 |
$task = $message->{task}; |
74 |
if ($task){ |
75 |
$task->{status} = 'new'; |
76 |
$heap->{task} = $task; |
77 |
} |
78 |
} |
79 |
return $task; #This return value is used by the parent POE session's _child handler |
80 |
} |
81 |
|
82 |
#This sub is just to start it now, or set it to start in the future... if the time is now or in the past, it starts now... if it's in the future, it starts in the future... |
83 |
sub on_task_init { |
84 |
my ($self, $session, $kernel, $heap) = @_[OBJECT, SESSION, KERNEL, HEAP]; |
85 |
my $response = 'pending'; |
86 |
my $task = $heap->{task}; |
87 |
my $status = $task->{status}; |
88 |
if ($status){ |
89 |
if ($status eq 'started'){ |
90 |
$response = 'already started'; |
91 |
} elsif ($status eq 'pending'){ |
92 |
$response = 'already pending'; |
93 |
} else { |
94 |
$task->{status} = 'pending'; |
95 |
|
96 |
my $start = $task->{start}; |
97 |
my $start_message = $start; |
98 |
|
99 |
|
100 |
my $dt; |
101 |
if ( $dt = $datetime_pattern->parse_datetime($start) ){ |
102 |
#e.g. 2016-04-06 00:00:00 |
103 |
} elsif ( $dt = $epoch_pattern->parse_datetime($start) ){ |
104 |
#e.g. 1459837498 or apparently 0000-00-00 00:00:00 |
105 |
} else { |
106 |
#If we don't match the datetime_pattern or epoch_pattern, then we start right now. |
107 |
$dt = DateTime->now( time_zone => 'local', ); |
108 |
} |
109 |
if ($dt){ |
110 |
$start = $dt->epoch; |
111 |
$start_message = $dt; |
112 |
} |
113 |
|
114 |
|
115 |
$self->log("Start task at $start_message"); |
116 |
#NOTE: $start must be in UNIX epoch time (ie number of seconds that have elapsed since 00:00:00 UTC Thursday 1 January 1970) |
117 |
$kernel->alarm("on_task_start",$start); |
118 |
} |
119 |
} |
120 |
return $response; |
121 |
} |
122 |
|
123 |
sub on_task_start { |
124 |
my ($self, $session, $kernel, $heap) = @_[OBJECT, SESSION, KERNEL, HEAP]; |
125 |
my $task = $heap->{task}; |
126 |
$task->{status} = 'started'; |
127 |
|
128 |
if (my $repeat_interval = $task->{repeat_interval}){ |
129 |
#NOTE: Reset the start time with a human readable timestamp |
130 |
my $dt = DateTime->now( time_zone => 'local', ); |
131 |
$dt->add( seconds => $repeat_interval ); |
132 |
$task->{start} = $dt->strftime("%F %T"); |
133 |
} |
134 |
#FIXME: You need to impose child process limits here! How many child processes are allowed to be running at any given time? Well, you can only have one child process per task... |
135 |
#so it's really more of a limit on the number of tasks... you probably need to have an internal task queue... that's easy enough though. |
136 |
my $child = POE::Wheel::Run->new( |
137 |
ProgramArgs => [ $task, ], |
138 |
Program => sub { |
139 |
my ($task) = @_; |
140 |
|
141 |
#Perform some last minute POE calls before running the task module plugin |
142 |
my $session = $poe_kernel->get_active_session(); |
143 |
if ($session){ |
144 |
my $heap = $session->get_heap(); |
145 |
$poe_kernel->call($heap->{server_id},"set_verbosity",0); #This turns off the server logging in this forked process, so the following call() doesn't mess up our logs |
146 |
$poe_kernel->call($heap->{server_id},"shutdown"); #Shutdown the socket listener on the child process, so there's zero chance of writing to or reading from the socket in the child process |
147 |
} |
148 |
|
149 |
#NOTE: I don't know if this really needs to be run, but it shouldn't hurt. |
150 |
$poe_kernel->stop(); |
151 |
|
152 |
#Try to load the task type module. |
153 |
my $task_type = $task->{type}; |
154 |
if ( can_load ( modules => { $task_type => undef, }, ) ){ |
155 |
#Create the object |
156 |
my $task_object = $task_type->new({task => $task, Verbosity => $self->{Verbosity}, }); |
157 |
if ($task_object){ |
158 |
#Synchronous action: run the task module |
159 |
$task_object->run; |
160 |
} |
161 |
} else { |
162 |
die "Couldn't load module $task_type: $Module::Load::Conditional::ERROR" |
163 |
} |
164 |
}, |
165 |
StdoutEvent => "got_child_stdout", |
166 |
StderrEvent => "got_child_stderr", |
167 |
CloseEvent => "got_child_close", |
168 |
NoSetPgrp => 1, #Keep child processes in same group as parent. This is especially useful when using Ctrl+C to kill the whole group. |
169 |
); |
170 |
|
171 |
$kernel->sig_child($child->PID, "got_child_signal"); |
172 |
# Wheel events include the wheel's ID. |
173 |
$_[HEAP]{children_by_wid}{$child->ID} = $child; |
174 |
# Signal events include the process ID. |
175 |
$_[HEAP]{children_by_pid}{$child->PID} = $child; |
176 |
|
177 |
$self->debug("child pid ".$child->PID." started as wheel ".$child->ID); |
178 |
} |
179 |
|
180 |
sub on_task_stop { |
181 |
my ($self, $session, $kernel, $heap) = @_[OBJECT, SESSION, KERNEL, HEAP]; |
182 |
my $task = $heap->{task}; |
183 |
$task->{status} = 'stopping'; |
184 |
my $task_id = $session->ID; |
185 |
my $server_id = $heap->{server_id}; |
186 |
|
187 |
if ($heap->{stopping}){ |
188 |
$self->debug("Task is already in the process of stopping..."); |
189 |
|
190 |
} else { |
191 |
|
192 |
$self->log("Trying to stop task."); |
193 |
|
194 |
|
195 |
#Mark this task as stopping |
196 |
$heap->{stopping} = 1; |
197 |
|
198 |
#Stop the task from spawning new jobs |
199 |
$kernel->alarm("on_task_start"); |
200 |
|
201 |
my $children_by_pid = $heap->{children_by_pid}; |
202 |
if ($children_by_pid && %$children_by_pid){ |
203 |
|
204 |
$self->debug("Child processes in progres..."); |
205 |
my $child_processes = $heap->{children_by_pid}; |
206 |
foreach my $child_pid (keys %$child_processes){ |
207 |
my $child = $child_processes->{$child_pid}; |
208 |
$self->debug("Telling child pid $child_pid to stop"); |
209 |
$child->put("quit"); |
210 |
#TODO: Perhaps it would be worthwhile having a kill switch too? |
211 |
# my $rv = $child->kill("TERM"); |
212 |
} |
213 |
} |
214 |
|
215 |
$self->log("Removing task keepalive."); |
216 |
|
217 |
$kernel->refcount_decrement($task_id,"waiting task"); |
218 |
} |
219 |
} |
220 |
|
221 |
sub on_terminal_signal { |
222 |
my ($self, $signal,$session,$kernel) = @_[OBJECT, ARG0,SESSION,KERNEL]; |
223 |
$self->debug("Trapped SIGNAL: $signal."); |
224 |
#Gracefully stop the task |
225 |
$kernel->call($session, "got_task_stop"); |
226 |
} |
227 |
|
228 |
sub child_process_success { |
229 |
my ($self, $heap,$session,$kernel) = @_[OBJECT, HEAP,SESSION,KERNEL]; |
230 |
my $task = $heap->{task}; |
231 |
if (my $repeat_interval = $task->{repeat_interval}){ |
232 |
if ($heap->{stopping}){ |
233 |
$self->log("Will skip repeating the task, as task is stopping."); |
234 |
} else { |
235 |
$self->log("Will repeat the task"); |
236 |
$task->{status} = "restarting"; |
237 |
$kernel->yield("on_task_init"); |
238 |
} |
239 |
} else { |
240 |
$self->debug("I'm going to stop this task"); |
241 |
$kernel->yield("got_task_stop"); |
242 |
} |
243 |
} |
244 |
|
245 |
############################################################# |
246 |
# # |
247 |
# Methods for communicating with child processes # |
248 |
# # |
249 |
############################################################# |
250 |
# Originally inspired by the POE::Wheel::Run perldoc example |
251 |
|
252 |
# Wheel event, including the wheel's ID |
253 |
sub on_child_stdout { |
254 |
my ($self, $stdout_line, $wheel_id, $session) = @_[OBJECT, ARG0, ARG1, SESSION]; |
255 |
my $child = $_[HEAP]{children_by_wid}{$wheel_id}; |
256 |
#NOTE: Log everything child process sends to STDOUT |
257 |
$self->log("[pid ".$child->PID."] STDOUT: $stdout_line"); |
258 |
|
259 |
#If the child outputs a line to STDOUT which starts with UPDATE_PARAMS=, we capture the data, |
260 |
#and update the task params. |
261 |
if ($stdout_line =~ /^UPDATE_PARAMS=(.*)$/){ |
262 |
my $json_string = $1; |
263 |
my $json = from_json($json_string); |
264 |
my $task = $_[HEAP]->{task}; |
265 |
my $params = $task->{params}; |
266 |
foreach my $key (%$json){ |
267 |
if (defined $params->{$key}){ |
268 |
#FIXME: Don't just overwrite? Only update differences? |
269 |
$params->{$key} = $json->{$key}; |
270 |
} |
271 |
} |
272 |
$_[HEAP]->{task} = $task; |
273 |
} |
274 |
} |
275 |
|
276 |
# Wheel event, including the wheel's ID. |
277 |
sub on_child_stderr { |
278 |
my ($self, $stderr_line, $wheel_id, $session) = @_[OBJECT, ARG0, ARG1,SESSION]; |
279 |
my $child = $_[HEAP]{children_by_wid}{$wheel_id}; |
280 |
#NOTE: Log everything child process sends to STDERR |
281 |
$self->log("[pid ".$child->PID."] STDERR: $stderr_line"); |
282 |
} |
283 |
|
284 |
# Wheel event, including the wheel's ID. |
285 |
sub on_child_close { |
286 |
my ($self, $wheel_id,$session,$kernel) = @_[OBJECT, ARG0,SESSION,KERNEL]; |
287 |
|
288 |
my $child = delete $_[HEAP]{children_by_wid}{$wheel_id}; |
289 |
|
290 |
# May have been reaped by on_child_signal(). |
291 |
unless (defined $child) { |
292 |
$self->debug("[wid $wheel_id] closed all pipes."); |
293 |
return; |
294 |
} |
295 |
$self->debug("[pid ".$child->PID."] closed all pipes."); |
296 |
delete $_[HEAP]{children_by_pid}{$child->PID}; |
297 |
} |
298 |
|
299 |
sub on_child_signal { |
300 |
my ($self, $heap,$kernel,$pid,$exit_code,$session) = @_[OBJECT, HEAP,KERNEL,ARG1,ARG2,SESSION]; |
301 |
|
302 |
#If the child's exit code is 0, handle this successful exit status |
303 |
if ($exit_code == 0){ |
304 |
$kernel->yield("child_process_success"); |
305 |
} |
306 |
$self->debug("pid $pid exited with status $exit_code."); |
307 |
my $child = delete $_[HEAP]{children_by_pid}{$pid}; |
308 |
|
309 |
# May have been reaped by on_child_close(). |
310 |
return unless defined $child; |
311 |
|
312 |
delete $_[HEAP]{children_by_wid}{$child->ID}; |
313 |
} |
314 |
|
315 |
1; |