Line 0
Link Here
|
|
|
1 |
package Koha::OAI::Harvester; |
2 |
|
3 |
# Copyright 2017 Prosentient Systems |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
# |
20 |
|
21 |
use Modern::Perl; |
22 |
use POE qw(Component::JobQueue); |
23 |
use JSON; |
24 |
use Sereal::Encoder; |
25 |
use Sereal::Decoder; |
26 |
use IO::Handle; |
27 |
use File::Copy; |
28 |
use File::Path qw(make_path remove_tree); |
29 |
use DateTime; |
30 |
use DateTime::Format::Strptime; |
31 |
|
32 |
use C4::Context; |
33 |
use Koha::Database; |
34 |
|
35 |
=head1 API |
36 |
|
37 |
=head2 Class Methods |
38 |
|
39 |
=cut |
40 |
|
41 |
my $day_granularity = DateTime::Format::Strptime->new( |
42 |
pattern => '%F', |
43 |
); |
44 |
my $seconds_granularity = DateTime::Format::Strptime->new( |
45 |
pattern => '%FT%TZ', |
46 |
); |
47 |
|
48 |
sub new { |
49 |
my ($class, $args) = @_; |
50 |
$args = {} unless defined $args; |
51 |
return bless ($args, $class); |
52 |
} |
53 |
|
54 |
sub spawn { |
55 |
my ($class, $args) = @_; |
56 |
my $self = $class->new($args); |
57 |
my $downloader = $self->{Downloader}; |
58 |
my $importer = $self->{Importer}; |
59 |
my $download_worker_limit = ( $self->{DownloaderWorkers} && int($self->{DownloaderWorkers}) ) ? $self->{DownloaderWorkers} : 1; |
60 |
my $import_worker_limit = ( $self->{ImporterWorkers} && int($self->{ImporterWorkers}) ) ? $self->{ImporterWorkers} : 1; |
61 |
my $import_queue_poll = ( $self->{ImportQueuePoll} && int($self->{ImportQueuePoll}) ) ? $self->{ImportQueuePoll} : 5; |
62 |
|
63 |
#NOTE: This job queue should always be created before the |
64 |
#harvester so that you can start download jobs immediately |
65 |
#upon spawning the harvester. |
66 |
POE::Component::JobQueue->spawn( |
67 |
Alias => 'oai-downloader', |
68 |
WorkerLimit => $download_worker_limit, |
69 |
Worker => sub { |
70 |
my ($postback, $task) = @_; |
71 |
if ($downloader){ |
72 |
if ($task->{status} eq "active"){ |
73 |
$downloader->run({ |
74 |
postback => $postback, |
75 |
task => $task, |
76 |
}); |
77 |
} |
78 |
} |
79 |
}, |
80 |
Passive => {}, |
81 |
); |
82 |
|
83 |
POE::Session->create( |
84 |
object_states => [ |
85 |
$self => { |
86 |
_start => "on_start", |
87 |
get_task => "get_task", |
88 |
list_tasks => "list_tasks", |
89 |
create_task => "create_task", |
90 |
start_task => "start_task", |
91 |
stop_task => "stop_task", |
92 |
delete_task => "delete_task", |
93 |
repeat_task => "repeat_task", |
94 |
register => "register", |
95 |
deregister => "deregister", |
96 |
restore_state => "restore_state", |
97 |
save_state => "save_state", |
98 |
is_task_finished => "is_task_finished", |
99 |
does_task_repeat => "does_task_repeat", |
100 |
download_postback => "download_postback", |
101 |
reset_imports_status => "reset_imports_status", |
102 |
}, |
103 |
], |
104 |
); |
105 |
|
106 |
POE::Component::JobQueue->spawn( |
107 |
Alias => 'oai-importer', |
108 |
WorkerLimit => $import_worker_limit, |
109 |
Worker => sub { |
110 |
my $meta_postback = shift; |
111 |
|
112 |
#NOTE: We need to only retrieve queue items for active tasks. Otherwise, |
113 |
#the importer will just spin its wheels on inactive tasks and do nothing. |
114 |
my $active_tasks = $poe_kernel->call("harvester","list_tasks","active"); |
115 |
my @active_uuids = map { $_->{uuid} } @$active_tasks; |
116 |
|
117 |
my $schema = Koha::Database->new()->schema(); |
118 |
my $rs = $schema->resultset('OaiHarvesterImportQueue')->search({ |
119 |
uuid => \@active_uuids, |
120 |
status => "new" |
121 |
},{ |
122 |
order_by => { -asc => 'id' }, |
123 |
rows => 1, |
124 |
}); |
125 |
my $result = $rs->first; |
126 |
if ($result){ |
127 |
$result->status("wip"); |
128 |
$result->update; |
129 |
my $task = { |
130 |
id => $result->id, |
131 |
uuid => $result->uuid, |
132 |
result => $result->result, |
133 |
}; |
134 |
|
135 |
my $postback = $meta_postback->($task); |
136 |
$importer->run({ |
137 |
postback => $postback, |
138 |
task => $task, |
139 |
}); |
140 |
} |
141 |
}, |
142 |
Active => { |
143 |
PollInterval => $import_queue_poll, |
144 |
AckAlias => undef, |
145 |
AckState => undef, |
146 |
}, |
147 |
); |
148 |
|
149 |
return; |
150 |
} |
151 |
|
152 |
sub on_start { |
153 |
my ($self, $kernel, $heap) = @_[OBJECT, KERNEL,HEAP]; |
154 |
$kernel->alias_set('harvester'); |
155 |
$heap->{scoreboard} = {}; |
156 |
|
157 |
#Reset any 'wip' imports to 'new' so they can be re-tried. |
158 |
$kernel->call("harvester","reset_imports_status"); |
159 |
|
160 |
#Restore state from state file |
161 |
$kernel->call("harvester","restore_state"); |
162 |
} |
163 |
|
164 |
#NOTE: This isn't really implemented at the moment, as it's not really necessary. |
165 |
sub download_postback { |
166 |
my ($kernel, $request_packet, $response_packet) = @_[KERNEL, ARG0, ARG1]; |
167 |
my $message = $response_packet->[0]; |
168 |
} |
169 |
|
170 |
=head3 deregister |
171 |
|
172 |
Remove the worker session from the harvester's in-memory scoreboard, |
173 |
unset the downloading flag if downloading is completed. |
174 |
|
175 |
=cut |
176 |
|
177 |
sub deregister { |
178 |
my ($self, $kernel, $heap, $session, $sender, $type) = @_[OBJECT, KERNEL,HEAP,SESSION,SENDER,ARG0]; |
179 |
|
180 |
my $scoreboard = $heap->{scoreboard}; |
181 |
|
182 |
my $logger = $self->{logger}; |
183 |
$logger->debug("Start deregistering $sender as $type task"); |
184 |
|
185 |
my $task_uuid = delete $scoreboard->{session}->{$sender}; |
186 |
#NOTE: If you don't check each step of the hashref, autovivication can lead to surprises. |
187 |
if ($task_uuid){ |
188 |
if ($scoreboard->{task}->{$task_uuid}){ |
189 |
if ($scoreboard->{task}->{$task_uuid}->{$type}){ |
190 |
delete $scoreboard->{task}->{$task_uuid}->{$type}->{$sender}; |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
my $task = $heap->{tasks}->{$task_uuid}; |
196 |
if ($task && $task->{status} && ($task->{status} eq "active") ){ |
197 |
#NOTE: Each task only has 1 download session, so we can now set/unset flags for the task. |
198 |
#NOTE: We should unset the downloading flag, if we're not going to repeat the task. |
199 |
if ($type eq "download"){ |
200 |
my $task_repeats = $kernel->call("harvester","does_task_repeat",$task_uuid); |
201 |
if ($task_repeats){ |
202 |
my $interval = $task->{interval}; |
203 |
|
204 |
$task->{effective_from} = delete $task->{effective_until}; |
205 |
$task->{download_timer} = $kernel->delay_set("repeat_task", $interval, $task_uuid); |
206 |
} |
207 |
else { |
208 |
$task->{downloading} = 0; |
209 |
$kernel->call("harvester","save_state"); |
210 |
$kernel->call("harvester","is_task_finished",$task_uuid); |
211 |
} |
212 |
} |
213 |
elsif ($type eq 'import'){ |
214 |
$kernel->call("harvester","is_task_finished",$task_uuid); |
215 |
} |
216 |
} |
217 |
$logger->debug("End deregistering $sender as $type task"); |
218 |
} |
219 |
|
220 |
|
221 |
=head3 is_task_finished |
222 |
|
223 |
This event handler checks if the task has finished downloading and importing record. |
224 |
If it is finished downloading and importing, the task is deleted from the harvester. |
225 |
|
226 |
This only applies to non-repeating tasks. |
227 |
|
228 |
=cut |
229 |
|
230 |
sub is_task_finished { |
231 |
my ($self, $kernel, $heap, $session, $uuid) = @_[OBJECT, KERNEL,HEAP,SESSION,ARG0]; |
232 |
my $task = $kernel->call("harvester","get_task",$uuid); |
233 |
if ($task && (! $task->{downloading}) ){ |
234 |
my $count = $self->get_import_count_for_task($uuid); |
235 |
if ( ! $count ) { |
236 |
#Clear this task out of the harvester as it's finished. |
237 |
$kernel->call("harvester","delete_task",$uuid); |
238 |
return 1; |
239 |
} |
240 |
} |
241 |
return 0; |
242 |
} |
243 |
|
244 |
sub register { |
245 |
my ($self, $kernel, $heap, $session, $sender, $type, $task_uuid) = @_[OBJECT, KERNEL,HEAP,SESSION,SENDER,ARG0,ARG1]; |
246 |
my $logger = $self->{logger}; |
247 |
|
248 |
my $scoreboard = $heap->{scoreboard}; |
249 |
|
250 |
|
251 |
if ($type && $task_uuid){ |
252 |
$logger->debug("Registering $sender as $type for $task_uuid"); |
253 |
|
254 |
my $task = $heap->{tasks}->{$task_uuid}; |
255 |
if ($task){ |
256 |
|
257 |
if ($type){ |
258 |
#Register the task uuid with the session id as a key for later recall |
259 |
$scoreboard->{session}->{$sender} = $task_uuid; |
260 |
|
261 |
#Register the session id as a certain type of session for a task |
262 |
$scoreboard->{task}->{$task_uuid}->{$type}->{$sender} = 1; |
263 |
|
264 |
if ($type eq "download"){ |
265 |
$task->{downloading} = 1; |
266 |
|
267 |
my $task_repeats = $kernel->call("harvester","does_task_repeat",$task_uuid); |
268 |
if ($task_repeats){ |
269 |
|
270 |
#NOTE: Set an effective until, so we know we're not getting records any newer than |
271 |
#this moment. |
272 |
my $dt = DateTime->now(); |
273 |
if ($dt){ |
274 |
#NOTE: Ideally, I'd like to make sure that we can use 'seconds' granularity, but |
275 |
#it's valid for 'from' to be null, so it's impossible to know from the data whether |
276 |
#or not the repository will support the seconds granularity. |
277 |
#NOTE: Ideally, it would be good to use either 'day' granularity or 'seconds' granularity, |
278 |
#but at the moment the interval is expressed as seconds only. |
279 |
$dt->set_formatter($seconds_granularity); |
280 |
$task->{effective_until} = "$dt"; |
281 |
} |
282 |
} |
283 |
|
284 |
$kernel->call("harvester","save_state"); |
285 |
} |
286 |
} |
287 |
} |
288 |
} |
289 |
} |
290 |
|
291 |
sub does_task_repeat { |
292 |
my ($self, $kernel, $heap, $session, $uuid) = @_[OBJECT, KERNEL,HEAP,SESSION,ARG0]; |
293 |
my $task = $kernel->call("harvester","get_task",$uuid); |
294 |
if ($task){ |
295 |
my $interval = $task->{interval}; |
296 |
my $parameters = $task->{parameters}; |
297 |
if ($parameters){ |
298 |
my $oai_pmh = $parameters->{oai_pmh}; |
299 |
if ($oai_pmh){ |
300 |
if ( $interval && ($oai_pmh->{verb} eq "ListRecords") && (! $oai_pmh->{until}) ){ |
301 |
return 1; |
302 |
} |
303 |
} |
304 |
} |
305 |
} |
306 |
return 0; |
307 |
} |
308 |
|
309 |
|
310 |
|
311 |
sub reset_imports_status { |
312 |
my ($self, $kernel, $heap, $session) = @_[OBJECT, KERNEL,HEAP,SESSION]; |
313 |
|
314 |
my $schema = Koha::Database->new()->schema(); |
315 |
my $rs = $schema->resultset('OaiHarvesterImportQueue')->search({ |
316 |
status => "wip", |
317 |
}); |
318 |
$rs->update({ |
319 |
status => "new", |
320 |
}); |
321 |
} |
322 |
|
323 |
sub restore_state { |
324 |
my ($self, $kernel, $heap, $session) = @_[OBJECT, KERNEL,HEAP,SESSION]; |
325 |
|
326 |
my $state_file = $self->{state_file}; |
327 |
if ($state_file){ |
328 |
my $state_backup = "$state_file~"; |
329 |
|
330 |
#NOTE: If there is a state backup, it means we crashed while saving the state. Otherwise, |
331 |
#let's try the regular state file if it exists. |
332 |
my $file_to_restore = ( -f $state_backup ) ? $state_backup : ( ( -f $state_file ) ? $state_file : undef ); |
333 |
if ( $file_to_restore ){ |
334 |
my $opened = open( my $fh, '<', $file_to_restore ) or die "Couldn't open state: $!"; |
335 |
if ($opened){ |
336 |
local $/; |
337 |
my $in = <$fh>; |
338 |
my $decoder = Sereal::Decoder->new; |
339 |
my $state = $decoder->decode($in); |
340 |
if ($state){ |
341 |
if ($state->{tasks}){ |
342 |
#Restore tasks from our saved state |
343 |
$heap->{tasks} = $state->{tasks}; |
344 |
foreach my $uuid ( keys %{$heap->{tasks}} ){ |
345 |
my $task = $heap->{tasks}->{$uuid}; |
346 |
|
347 |
#If tasks were still downloading, restart the task |
348 |
if ( ($task->{status} && $task->{status} eq "active") && $task->{downloading} ){ |
349 |
$task->{status} = "new"; |
350 |
$kernel->call("harvester","start_task",$task->{uuid}); |
351 |
} |
352 |
} |
353 |
} |
354 |
} |
355 |
} |
356 |
} |
357 |
} |
358 |
} |
359 |
|
360 |
sub save_state { |
361 |
my ($self, $kernel, $heap, $session) = @_[OBJECT, KERNEL,HEAP,SESSION]; |
362 |
my $state_file = $self->{state_file}; |
363 |
my $state_backup = "$state_file~"; |
364 |
|
365 |
#Make a backup of existing state record |
366 |
my $moved = move($state_file,$state_backup); |
367 |
|
368 |
my $opened = open(my $fh, ">", $state_file) or die "Couldn't save state: $!"; |
369 |
if ($opened){ |
370 |
$fh->autoflush(1); |
371 |
my $tasks = $heap->{tasks}; |
372 |
my $harvester_state = { |
373 |
tasks => $tasks, |
374 |
}; |
375 |
my $encoder = Sereal::Encoder->new; |
376 |
my $out = $encoder->encode($harvester_state); |
377 |
local $\; |
378 |
my $printed = print $fh $out; |
379 |
if ($printed){ |
380 |
close $fh; |
381 |
unlink($state_backup); |
382 |
return 1; |
383 |
} |
384 |
} |
385 |
return 0; |
386 |
} |
387 |
|
388 |
=head3 get_task |
389 |
|
390 |
This event handler returns a task from a harvester using the task's |
391 |
uuid as an argument. |
392 |
|
393 |
=cut |
394 |
|
395 |
sub get_task { |
396 |
my ($self, $kernel, $heap, $session, $uuid, $sender) = @_[OBJECT, KERNEL,HEAP,SESSION,ARG0, SENDER]; |
397 |
|
398 |
if ( ! $uuid && $sender ){ |
399 |
my $scoreboard = $heap->{scoreboard}; |
400 |
my $uuid_by_session = $scoreboard->{session}->{$sender}; |
401 |
if ($uuid_by_session){ |
402 |
$uuid = $uuid_by_session; |
403 |
} |
404 |
} |
405 |
|
406 |
my $tasks = $heap->{tasks}; |
407 |
if ($tasks && $uuid){ |
408 |
my $task = $tasks->{$uuid}; |
409 |
if ($task){ |
410 |
return $task; |
411 |
} |
412 |
} |
413 |
return 0; |
414 |
} |
415 |
|
416 |
=head3 get_import_count_for_task |
417 |
|
418 |
=cut |
419 |
|
420 |
sub get_import_count_for_task { |
421 |
my ($self,$uuid) = @_; |
422 |
my $count = undef; |
423 |
if ($uuid){ |
424 |
my $schema = Koha::Database->new()->schema(); |
425 |
my $items = $schema->resultset('OaiHarvesterImportQueue')->search({ |
426 |
uuid => $uuid, |
427 |
}); |
428 |
$count = $items->count; |
429 |
} |
430 |
return $count; |
431 |
} |
432 |
|
433 |
=head3 list_tasks |
434 |
|
435 |
This event handler returns a list of tasks that have been submitted |
436 |
to the harvester. It returns data like uuid, status, parameters, |
437 |
number of pending imports, etc. |
438 |
|
439 |
=cut |
440 |
|
441 |
sub list_tasks { |
442 |
my ($self, $kernel, $heap, $session, $status) = @_[OBJECT, KERNEL,HEAP,SESSION, ARG0]; |
443 |
my $schema = Koha::Database->new()->schema(); |
444 |
my @tasks = (); |
445 |
foreach my $uuid (sort keys %{$heap->{tasks}}){ |
446 |
my $task = $heap->{tasks}->{$uuid}; |
447 |
my $items = $schema->resultset('OaiHarvesterImportQueue')->search({ |
448 |
uuid => $uuid, |
449 |
}); |
450 |
my $count = $items->count // 0; |
451 |
$task->{pending_imports} = $count; |
452 |
if ( ( ! $status ) || ( $status && $status eq $task->{status} ) ){ |
453 |
push(@tasks, $task); |
454 |
} |
455 |
|
456 |
} |
457 |
return \@tasks; |
458 |
} |
459 |
|
460 |
=head3 create_task |
461 |
|
462 |
This event handler creates a spool directory for the task's imports. |
463 |
It also adds it to the harvester's memory and then saves memory to |
464 |
a persistent datastore. |
465 |
|
466 |
Newly created tasks have a status of "new". |
467 |
|
468 |
=cut |
469 |
|
470 |
sub create_task { |
471 |
my ($self, $kernel, $heap, $session, $incoming_task) = @_[OBJECT, KERNEL,HEAP,SESSION,ARG0]; |
472 |
my $logger = $self->{logger}; |
473 |
if ($incoming_task){ |
474 |
my $uuid = $incoming_task->{uuid}; |
475 |
if ( ! $heap->{tasks}->{$uuid} ){ |
476 |
|
477 |
#Step One: assign a spool directory to this task |
478 |
my $spooldir = $self->{spooldir} // "/tmp"; |
479 |
my $task_spooldir = "$spooldir/$uuid"; |
480 |
if ( ! -d $task_spooldir ){ |
481 |
my $made_spool_directory = make_path($task_spooldir); |
482 |
if ( ! $made_spool_directory ){ |
483 |
if ($logger){ |
484 |
$logger->warn("Unable to make task-specific spool directory at '$task_spooldir'"); |
485 |
} |
486 |
return 0; |
487 |
} |
488 |
} |
489 |
$incoming_task->{spooldir} = $task_spooldir; |
490 |
|
491 |
#Step Two: assign new status |
492 |
$incoming_task->{status} = "new"; |
493 |
|
494 |
#Step Three: add task to harvester's memory |
495 |
$heap->{tasks}->{ $uuid } = $incoming_task; |
496 |
|
497 |
#Step Four: save state |
498 |
$kernel->call($session,"save_state"); |
499 |
return 1; |
500 |
} |
501 |
} |
502 |
return 0; |
503 |
} |
504 |
|
505 |
=head3 start_task |
506 |
|
507 |
This event handler marks a task as active in the harvester's memory, |
508 |
save the memory to a persistent datastore, then enqueues the task, |
509 |
so that it can be directed to the next available download worker. |
510 |
|
511 |
Newly started tasks have a status of "active". |
512 |
|
513 |
=cut |
514 |
|
515 |
sub start_task { |
516 |
my ($self, $session,$kernel,$heap,$uuid) = @_[OBJECT, SESSION,KERNEL,HEAP,ARG0]; |
517 |
my $task = $heap->{tasks}->{$uuid}; |
518 |
if ($task){ |
519 |
if ( $task->{status} ne "active" ){ |
520 |
|
521 |
#Clear any pre-existing error states |
522 |
delete $task->{error} if $task->{error}; |
523 |
|
524 |
#Step One: mark task as active |
525 |
$task->{status} = "active"; |
526 |
|
527 |
#Step Two: save state |
528 |
$kernel->call("harvester","save_state"); |
529 |
|
530 |
#Step Three: enqueue task |
531 |
$kernel->post("oai-downloader",'enqueue','download_postback', $task); |
532 |
|
533 |
return 1; |
534 |
} |
535 |
} |
536 |
return 0; |
537 |
} |
538 |
|
539 |
=head3 repeat_task |
540 |
|
541 |
|
542 |
|
543 |
=cut |
544 |
|
545 |
sub repeat_task { |
546 |
my ($self, $session,$kernel,$heap,$uuid) = @_[OBJECT, SESSION,KERNEL,HEAP,ARG0]; |
547 |
my $task = $heap->{tasks}->{$uuid}; |
548 |
if ($task){ |
549 |
my $interval = $task->{interval}; |
550 |
if ($task->{downloading} && $interval){ |
551 |
$kernel->post("oai-downloader",'enqueue','download_postback', $task); |
552 |
} |
553 |
} |
554 |
} |
555 |
|
556 |
=head3 stop_task |
557 |
|
558 |
This event handler prevents new workers from spawning, kills |
559 |
existing workers, and stops pending imports from being imported. |
560 |
|
561 |
Newly stopped tasks have a status of "stopped". |
562 |
|
563 |
=cut |
564 |
|
565 |
sub stop_task { |
566 |
my ($self, $kernel, $heap, $session, $sender, $task_uuid) = @_[OBJECT, KERNEL,HEAP,SESSION,SENDER,ARG0]; |
567 |
|
568 |
my $task = $heap->{tasks}->{$task_uuid}; |
569 |
|
570 |
if ($task && $task->{status} && $task->{status} ne "stopped" ){ |
571 |
|
572 |
#Step One: deactivate task, so no new workers can be started |
573 |
$task->{status} = "stopped"; |
574 |
#NOTE: You could also clear timers for new downloads, but that's probably unnecessary because of this step. |
575 |
|
576 |
#Step Two: kill workers |
577 |
my $scoreboard = $heap->{scoreboard}; |
578 |
my $session_types = $scoreboard->{task}->{$task_uuid}; |
579 |
if ($session_types){ |
580 |
foreach my $type ( keys %$session_types ){ |
581 |
my $sessions = $session_types->{$type}; |
582 |
if ($sessions){ |
583 |
foreach my $session (keys %$sessions){ |
584 |
if ($session){ |
585 |
$kernel->signal($session, "cancel"); |
586 |
} |
587 |
} |
588 |
} |
589 |
} |
590 |
#Remove the task uuid from the task key of the scoreboard |
591 |
delete $scoreboard->{task}->{$task_uuid}; |
592 |
#NOTE: The task uuid will still exist under the session key, |
593 |
#but the sessions will deregister themselves and clean that up for you. |
594 |
} |
595 |
|
596 |
#Step Three: stop pending imports for this task |
597 |
my $schema = Koha::Database->new()->schema(); |
598 |
my $items = $schema->resultset('OaiHarvesterImportQueue')->search({ |
599 |
uuid => $task_uuid, |
600 |
}); |
601 |
my $rows_updated = $items->update({ |
602 |
status => "stopped", |
603 |
}); |
604 |
|
605 |
#Step Four: save state |
606 |
$kernel->call("harvester","save_state"); |
607 |
return 1; |
608 |
} |
609 |
return 0; |
610 |
} |
611 |
|
612 |
=head3 delete_task |
613 |
|
614 |
Deleted tasks are stopped, pending imports are deleted from the |
615 |
database and file system, and then the task is removed from the harvester. |
616 |
|
617 |
=cut |
618 |
|
619 |
sub delete_task { |
620 |
my ($self, $kernel, $heap, $session, $task_uuid) = @_[OBJECT, KERNEL,HEAP,SESSION,ARG0]; |
621 |
|
622 |
my $task = $heap->{tasks}->{$task_uuid}; |
623 |
if ($task){ |
624 |
#Step One: stop task |
625 |
$kernel->call($session,"stop_task",$task_uuid); |
626 |
|
627 |
#Step Two: delete pending imports in database |
628 |
my $schema = Koha::Database->new()->schema(); |
629 |
my $items = $schema->resultset('OaiHarvesterImportQueue')->search({ |
630 |
uuid => $task_uuid, |
631 |
}); |
632 |
if ($items){ |
633 |
my $rows_deleted = $items->delete; |
634 |
#NOTE: shows 0E0 instead of 0 |
635 |
} |
636 |
|
637 |
#Step Three: remove task specific spool directory and files within it |
638 |
my $spooldir = $task->{spooldir}; |
639 |
if ($spooldir){ |
640 |
my $files_deleted = remove_tree($spooldir, { safe => 1 }); |
641 |
} |
642 |
|
643 |
delete $heap->{tasks}->{$task_uuid}; |
644 |
|
645 |
#Step Four: save state |
646 |
$kernel->call("harvester","save_state"); |
647 |
return 1; |
648 |
} |
649 |
return 0; |
650 |
} |
651 |
|
652 |
1; |