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