| 1 | package Hopkins::Manager; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Hopkins::Manager - hopkins manager session states | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Hopkins::Manager encapsulates the manager session, which is | ||
| 12 | responsible for configuration parsing and change scanning, | ||
| 13 | queue and plugin management, and last, but certainly not | ||
| 14 | least, task scheduling. | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use POE; | ||
| 19 | use Class::Accessor::Fast; | ||
| 20 | |||
| 21 | use Data::UUID; | ||
| 22 | use Path::Class::Dir; | ||
| 23 | |||
| 24 | use Hopkins::Store; | ||
| 25 | use Hopkins::Config; | ||
| 26 | use Hopkins::Queue; | ||
| 27 | use Hopkins::Task; | ||
| 28 | use Hopkins::Work; | ||
| 29 | |||
| 30 | use Hopkins::Constants; | ||
| 31 | |||
| 32 | use base 'Class::Accessor::Fast'; | ||
| 33 | |||
| 34 | __PACKAGE__->mk_accessors(qw(kernel hopkins config plugins queues)); | ||
| 35 | |||
| 36 | my $ug = new Data::UUID; | ||
| 37 | |||
| 38 | =head1 STATES | ||
| 39 | |||
| 40 | =over 4 | ||
| 41 | |||
| 42 | =item new | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new | ||
| 47 | { | ||
| 48 | my $self = shift->SUPER::new(@_); | ||
| 49 | |||
| 50 | $self->plugins({}); | ||
| 51 | $self->queues({}); | ||
| 52 | $self->kernel($poe_kernel); | ||
| 53 | |||
| 54 | # create the POE Session that will be the bread and butter | ||
| 55 | # of the job daemon's normal function. the manager session | ||
| 56 | # will read the configuration upon execution and will begin | ||
| 57 | # the rest of the startup process in the following order: | ||
| 58 | # | ||
| 59 | # - storage initialization via DBIx::Class | ||
| 60 | # - queue creation via POE::Component::JobQueue | ||
| 61 | # - RPC session creation via POE::Component::Server::SOAP | ||
| 62 | |||
| 63 | # create manager session | ||
| 64 | POE::Session->create | ||
| 65 | ( | ||
| 66 | object_states => | ||
| 67 | [ | ||
| 68 | $self => | ||
| 69 | { | ||
| 70 | _start => 'start', | ||
| 71 | _stop => 'stop', | ||
| 72 | |||
| 73 | config_scan => 'config_scan', | ||
| 74 | config_load => 'config_load', | ||
| 75 | |||
| 76 | init_config => 'init_config', | ||
| 77 | init_queues => 'init_queues', | ||
| 78 | init_plugins => 'init_plugins', | ||
| 79 | init_store => 'init_store', | ||
| 80 | |||
| 81 | queue_check_all => 'queue_check_all', | ||
| 82 | queue_check => 'queue_check', | ||
| 83 | queue_failure => 'queue_failure', | ||
| 84 | queue_start => 'queue_start', | ||
| 85 | queue_halt => 'queue_halt', | ||
| 86 | queue_continue => 'queue_continue', | ||
| 87 | queue_freeze => 'queue_freeze', | ||
| 88 | queue_thaw => 'queue_thaw', | ||
| 89 | queue_shutdown => 'queue_shutdown', | ||
| 90 | queue_flush => 'queue_flush', | ||
| 91 | |||
| 92 | scheduler => 'scheduler', | ||
| 93 | executor => 'executor', | ||
| 94 | enqueue => 'enqueue', | ||
| 95 | complete => 'complete', | ||
| 96 | abort => 'abort', | ||
| 97 | |||
| 98 | shutdown => 'shutdown' | ||
| 99 | } | ||
| 100 | ] | ||
| 101 | ); | ||
| 102 | |||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | =item start | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub start | ||
| 111 | { | ||
| 112 | my $self = $_[OBJECT]; | ||
| 113 | my $kernel = $_[KERNEL]; | ||
| 114 | |||
| 115 | # set the alias for the current session | ||
| 116 | $kernel->alias_set('manager'); | ||
| 117 | |||
| 118 | # log that we are starting up | ||
| 119 | Hopkins->log_info("starting hopkins version $Hopkins::VERSION"); | ||
| 120 | |||
| 121 | # post events for initial setup | ||
| 122 | $kernel->call(manager => 'init_config'); # configuration file | ||
| 123 | $kernel->post(manager => 'init_store'); # database storage sessage | ||
| 124 | $kernel->post(manager => 'init_queues'); # worker queue sessions | ||
| 125 | } | ||
| 126 | |||
| 127 | =item stop | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub stop | ||
| 132 | { | ||
| 133 | my $self = $_[OBJECT]; | ||
| 134 | |||
| 135 | Hopkins->log_debug('manager exiting'); | ||
| 136 | } | ||
| 137 | |||
| 138 | =item init_queues | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub init_queues | ||
| 143 | { | ||
| 144 | my $self = $_[OBJECT]; | ||
| 145 | my $kernel = $_[KERNEL]; | ||
| 146 | |||
| 147 | Hopkins->log_info('creating queues'); | ||
| 148 | |||
| 149 | # create a queue object for each configured queue. we | ||
| 150 | # use an active POE::Component::JobQueue and leave the | ||
| 151 | # scheduling up to the manager session. | ||
| 152 | |||
| 153 | foreach my $name ($self->config->get_queue_names) { | ||
| 154 | my $opts = $self->config->get_queue_info($name); | ||
| 155 | my $queue = new Hopkins::Queue { kernel => $kernel, config => $self->config, %$opts }; | ||
| 156 | |||
| 157 | $self->queues->{$name} = $queue; | ||
| 158 | |||
| 159 | $kernel->post(manager => queue_start => $name) unless $queue->halted; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub queue_start | ||
| 164 | { | ||
| 165 | my $self = $_[OBJECT]; | ||
| 166 | my $name = $_[ARG0]; | ||
| 167 | my $queue = $self->queue($name); | ||
| 168 | |||
| 169 | return HOPKINS_QUEUE_NOT_FOUND if not defined $queue; | ||
| 170 | |||
| 171 | return $queue->start; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub queue_halt | ||
| 175 | { | ||
| 176 | my $self = $_[OBJECT]; | ||
| 177 | my $name = $_[ARG0]; | ||
| 178 | my $queue = $self->queue($name); | ||
| 179 | |||
| 180 | return HOPKINS_QUEUE_NOT_FOUND if not defined $queue; | ||
| 181 | |||
| 182 | return $queue->halt; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub queue_continue | ||
| 186 | { | ||
| 187 | my $self = $_[OBJECT]; | ||
| 188 | my $name = $_[ARG0]; | ||
| 189 | my $queue = $self->queue($name); | ||
| 190 | |||
| 191 | return HOPKINS_QUEUE_NOT_FOUND if not defined $queue; | ||
| 192 | |||
| 193 | return $queue->continue; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub queue_freeze | ||
| 197 | { | ||
| 198 | my $self = $_[OBJECT]; | ||
| 199 | my $name = $_[ARG0]; | ||
| 200 | my $queue = $self->queue($name); | ||
| 201 | |||
| 202 | return HOPKINS_QUEUE_NOT_FOUND if not defined $queue; | ||
| 203 | |||
| 204 | return $queue->freeze; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub queue_thaw | ||
| 208 | { | ||
| 209 | my $self = $_[OBJECT]; | ||
| 210 | my $name = $_[ARG0]; | ||
| 211 | my $queue = $self->queue($name); | ||
| 212 | |||
| 213 | return HOPKINS_QUEUE_NOT_FOUND if not defined $queue; | ||
| 214 | |||
| 215 | return $queue->thaw; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub queue_flush | ||
| 219 | { | ||
| 220 | my $self = $_[OBJECT]; | ||
| 221 | my $name = $_[ARG0]; | ||
| 222 | my $queue = $self->queue($name); | ||
| 223 | |||
| 224 | return HOPKINS_QUEUE_NOT_FOUND if not defined $queue; | ||
| 225 | |||
| 226 | return $queue->flush; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub queue_shutdown | ||
| 230 | { | ||
| 231 | my $self = $_[OBJECT]; | ||
| 232 | my $name = $_[ARG0]; | ||
| 233 | my $queue = $self->queue($name); | ||
| 234 | |||
| 235 | return HOPKINS_QUEUE_NOT_FOUND if not defined $queue; | ||
| 236 | |||
| 237 | return $queue->shutdown; | ||
| 238 | } | ||
| 239 | |||
| 240 | sub queue_check | ||
| 241 | { | ||
| 242 | my $self = $_[OBJECT]; | ||
| 243 | my $name = $_[ARG0]; | ||
| 244 | |||
| 245 | return $self->queues->{$name}; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub queue_check_all | ||
| 249 | { | ||
| 250 | my $self = $_[OBJECT]; | ||
| 251 | |||
| 252 | return values %{ $self->queues }; | ||
| 253 | } | ||
| 254 | |||
| 255 | =item queue_failure | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | sub queue_failure | ||
| 260 | { | ||
| 261 | my $self = $_[OBJECT]; | ||
| 262 | my $kernel = $_[KERNEL]; | ||
| 263 | my $queue = $_[ARG0]; | ||
| 264 | my $error = $_[ARG1]; | ||
| 265 | |||
| 266 | my $msg = 'task failure in ' . $queue->name . ' queue'; | ||
| 267 | |||
| 268 | if (my $action = $queue->onerror) { | ||
| 269 | $queue->$action($error); | ||
| 270 | } | ||
| 271 | |||
| 272 | $queue->error($error); | ||
| 273 | |||
| 274 | Hopkins->log_warn($msg); | ||
| 275 | } | ||
| 276 | |||
| 277 | =item init_store | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | sub init_store | ||
| 282 | { | ||
| 283 | my $self = shift; | ||
| 284 | |||
| 285 | new Hopkins::Store { config => $self->config }; | ||
| 286 | } | ||
| 287 | |||
| 288 | =item init_config | ||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | sub init_config | ||
| 293 | { | ||
| 294 | my $self = $_[OBJECT]; | ||
| 295 | my $kernel = $_[KERNEL]; | ||
| 296 | |||
| 297 | my $class = 'Hopkins::Config::' . $self->hopkins->conf->[0]; | ||
| 298 | |||
| 299 | eval "use $class"; | ||
| 300 | |||
| 301 | if (my $err = $@) { | ||
| 302 | Hopkins->log_error("unable to load config class: $err"); | ||
| 303 | return $kernel->post(manager => 'shutdown'); | ||
| 304 | } | ||
| 305 | |||
| 306 | eval { | ||
| 307 | $self->config($class->new($self->hopkins->conf->[1])); | ||
| 308 | die 'constructor did not return a Hopkins::Config object' | ||
| 309 | if not UNIVERSAL::isa($self->config, 'Hopkins::Config'); | ||
| 310 | }; | ||
| 311 | |||
| 312 | if (my $err = $@) { | ||
| 313 | Hopkins->log_error("unable to create config object: $err"); | ||
| 314 | return $kernel->post(manager => 'shutdown'); | ||
| 315 | } | ||
| 316 | |||
| 317 | $kernel->call(manager => 'config_load'); | ||
| 318 | $kernel->alarm(config_scan => time + $self->hopkins->scan); | ||
| 319 | } | ||
| 320 | |||
| 321 | =item init_plugins | ||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | sub init_plugins | ||
| 326 | { | ||
| 327 | my $self = $_[OBJECT]; | ||
| 328 | my $kernel = $_[KERNEL]; | ||
| 329 | |||
| 330 | Hopkins->log_info('loading plugins'); | ||
| 331 | |||
| 332 | my $config = $self->config; | ||
| 333 | my $plugins = $self->plugins; | ||
| 334 | |||
| 335 | delete $plugins->{$_} | ||
| 336 | foreach grep { not $config->has_plugin($_) } keys %$plugins; | ||
| 337 | |||
| 338 | foreach my $name ($config->get_plugin_names) { | ||
| 339 | if (not exists $plugins->{$name}) { | ||
| 340 | my $options = $config->get_plugin_info($name); | ||
| 341 | my $package = $name =~ /^\+/ ? $name : "Hopkins::Plugin::$name"; | ||
| 342 | my $path = $package; | ||
| 343 | |||
| 344 | $path =~ s{::}{/}g; | ||
| 345 | |||
| 346 | eval { | ||
| 347 | require "$path.pm"; | ||
| 348 | |||
| 349 | my $plugin = $package->new({ manager => $self, config => $options }); | ||
| 350 | |||
| 351 | $plugins->{$name} = $plugin; | ||
| 352 | }; | ||
| 353 | |||
| 354 | Hopkins->log_error("failed to load plugin $name: $@") if $@; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | =item config_load | ||
| 360 | |||
| 361 | =cut | ||
| 362 | |||
| 363 | sub config_load | ||
| 364 | { | ||
| 365 | my $self = $_[OBJECT]; | ||
| 366 | my $kernel = $_[KERNEL]; | ||
| 367 | |||
| 368 | my $status = $self->config->load; | ||
| 369 | |||
| 370 | $kernel->post(manager => 'shutdown') unless $status->ok; | ||
| 371 | |||
| 372 | if ($status->failed) { | ||
| 373 | my $err = $status->parsed | ||
| 374 | ? 'errors in configuration, discarding new version' | ||
| 375 | : 'unable to load configuration: ' . $status->errmsg; | ||
| 376 | |||
| 377 | Hopkins->log_error($err); | ||
| 378 | } | ||
| 379 | |||
| 380 | $kernel->post(manager => 'shutdown') if not $self->config->loaded; | ||
| 381 | |||
| 382 | return unless $status->updated; | ||
| 383 | |||
| 384 | Hopkins->log_info('configuration loaded'); | ||
| 385 | |||
| 386 | if ($status->store_modified) { | ||
| 387 | Hopkins->log_info('store information changed'); | ||
| 388 | $kernel->post(store => 'init'); | ||
| 389 | } | ||
| 390 | |||
| 391 | $kernel->alarm('executor'); | ||
| 392 | |||
| 393 | $kernel->post(manager => 'init_plugins'); | ||
| 394 | $kernel->post(manager => 'scheduler'); | ||
| 395 | } | ||
| 396 | |||
| 397 | =item config_scan | ||
| 398 | |||
| 399 | =cut | ||
| 400 | |||
| 401 | sub config_scan | ||
| 402 | { | ||
| 403 | my $self = $_[OBJECT]; | ||
| 404 | my $kernel = $_[KERNEL]; | ||
| 405 | |||
| 406 | if ($self->config->scan) { | ||
| 407 | Hopkins->log_info('configuration file changed'); | ||
| 408 | $kernel->post(manager => 'config_load') | ||
| 409 | } | ||
| 410 | |||
| 411 | $kernel->alarm(config_scan => time + $self->hopkins->scan); | ||
| 412 | } | ||
| 413 | |||
| 414 | =item shutdown | ||
| 415 | |||
| 416 | =cut | ||
| 417 | |||
| 418 | sub shutdown | ||
| 419 | { | ||
| 420 | my $self = $_[OBJECT]; | ||
| 421 | my $kernel = $_[KERNEL]; | ||
| 422 | |||
| 423 | Hopkins->log_info('received shutdown request'); | ||
| 424 | |||
| 425 | $kernel->alarm('executor'); | ||
| 426 | $kernel->alarm('config_scan'); | ||
| 427 | |||
| 428 | foreach my $name ($self->config->get_queue_names) { | ||
| 429 | Hopkins->log_debug("posting stop event for $name queue"); | ||
| 430 | $kernel->post(manager => queue_halt => $name); | ||
| 431 | } | ||
| 432 | |||
| 433 | $kernel->post(store => 'shutdown'); | ||
| 434 | $kernel->alias_remove('manager'); | ||
| 435 | } | ||
| 436 | |||
| 437 | =item scheduler | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | sub scheduler | ||
| 442 | { | ||
| 443 | my $self = $_[OBJECT]; | ||
| 444 | my $kernel = $_[KERNEL]; | ||
| 445 | |||
| 446 | my $now = DateTime->now(time_zone => 'local'); | ||
| 447 | |||
| 448 | $now->truncate(to => 'seconds'); | ||
| 449 | |||
| 450 | foreach my $name ($self->config->get_task_names) { | ||
| 451 | my $task = $self->config->get_task_info($name); | ||
| 452 | |||
| 453 | next if not defined $task->schedule; | ||
| 454 | next if not $task->enabled; | ||
| 455 | |||
| 456 | my $next = $task->schedule->next($now); | ||
| 457 | |||
| 458 | Hopkins->log_debug('scheduling ' . $task->name . ' for ' . $next->iso8601); | ||
| 459 | |||
| 460 | $kernel->alarm_add(executor => $next->epoch => $task); | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | =item executor | ||
| 465 | |||
| 466 | =cut | ||
| 467 | |||
| 468 | sub executor | ||
| 469 | { | ||
| 470 | my $self = $_[OBJECT]; | ||
| 471 | my $kernel = $_[KERNEL]; | ||
| 472 | my $task = $_[ARG0]; | ||
| 473 | |||
| 474 | Hopkins->log_debug('executor alarm for ' . $task->name); | ||
| 475 | |||
| 476 | my $now = DateTime->now(time_zone => 'local'); | ||
| 477 | |||
| 478 | Hopkins->log_debug('current time: ' . $now->iso8601); | ||
| 479 | |||
| 480 | my $state = $kernel->call(manager => enqueue => $task->name => $task->params); | ||
| 481 | my $next = $task->schedule->next($now); | ||
| 482 | |||
| 483 | Hopkins->log_error('failed to enqueue ' . $task->name) if not $state; | ||
| 484 | |||
| 485 | Hopkins->log_debug('scheduling ' . $task->name . ' for ' . $next->iso8601); | ||
| 486 | |||
| 487 | $kernel->alarm_add(executor => $next->epoch => $task); | ||
| 488 | } | ||
| 489 | |||
| 490 | =item enqueue | ||
| 491 | |||
| 492 | enqueue a task by creating a Work object and adding it to | ||
| 493 | the destination Queue object's list. if the destination | ||
| 494 | queue is not running, no Work will be created and an error | ||
| 495 | 0 will be returned to the caller. | ||
| 496 | |||
| 497 | this state may be posted to by any session, but is primarily | ||
| 498 | utilized by the manager session's executor alarms. the RPC | ||
| 499 | plugin exposes an enqueue method via SOAP that posts to this | ||
| 500 | event. the HMI plugin also exposes an enqueuing mechanism | ||
| 501 | via a web interface. | ||
| 502 | |||
| 503 | =cut | ||
| 504 | |||
| 505 | sub enqueue | ||
| 506 | { | ||
| 507 | my $self = $_[OBJECT]; | ||
| 508 | my $kernel = $_[KERNEL]; | ||
| 509 | my $name = $_[ARG0]; | ||
| 510 | my $topts = $_[ARG1]; | ||
| 511 | my $qopts = $_[ARG2] || {}; | ||
| 512 | |||
| 513 | my $task = $self->config->get_task_info($name); | ||
| 514 | my $now = DateTime->now(time_zone => 'local'); | ||
| 515 | my $when = $qopts->{when} || $now; | ||
| 516 | my $pri = $qopts->{priority} || 5; | ||
| 517 | |||
| 518 | # make sure the Task exists | ||
| 519 | |||
| 520 | if (not defined $task) { | ||
| 521 | Hopkins->log_warn("unable to enqueue $name; task not found"); | ||
| 522 | return HOPKINS_ENQUEUE_TASK_NOT_FOUND; | ||
| 523 | } | ||
| 524 | |||
| 525 | # ensure that the Task's queue exists | ||
| 526 | |||
| 527 | my $queue = $self->queue($task->queue); | ||
| 528 | |||
| 529 | if (not defined $queue) { | ||
| 530 | Hopkins->log_warn("unable to enqueue $name; queue " . $task->queue . ' not found'); | ||
| 531 | return HOPKINS_ENQUEUE_QUEUE_UNAVAILABLE; | ||
| 532 | } | ||
| 533 | |||
| 534 | # ensure that the queue is not frozen | ||
| 535 | |||
| 536 | if ($queue->frozen) { | ||
| 537 | Hopkins->log_warn("unable to enqueue $name; queue " . $task->queue . ' frozen'); | ||
| 538 | return HOPKINS_ENQUEUE_QUEUE_FROZEN; | ||
| 539 | } | ||
| 540 | |||
| 541 | # ensure that the requested start time is sane | ||
| 542 | |||
| 543 | if ($when and not UNIVERSAL::isa($when, 'DateTime')) { | ||
| 544 | eval { $when = DateTime::Format::ISO8601->parse_datetime($when) }; | ||
| 545 | if ($@) { | ||
| 546 | Hopkins->log_warn("unable to enqueue $name; invalid date/time specified"); | ||
| 547 | return HOPKINS_ENQUEUE_DATETIME_INVALID; | ||
| 548 | } | ||
| 549 | } | ||
| 550 | |||
| 551 | # ensure that we don't stack tasks if requested | ||
| 552 | |||
| 553 | if ($task->stack && $when <= $now && $task->stack <= $queue->num_queued($task)) { | ||
| 554 | Hopkins->log_warn("unable to enqueue $name; stack limit reached"); | ||
| 555 | return HOPKINS_ENQUEUE_TASK_STACK_LIMIT; | ||
| 556 | } | ||
| 557 | |||
| 558 | # all of our sanity checks have passed. looks like we | ||
| 559 | # are good to queue up some work for the queue! create | ||
| 560 | # and populate a new Work object. assigning a new UUID | ||
| 561 | # to the work so that we can reference it later. | ||
| 562 | |||
| 563 | my $work = new Hopkins::Work; | ||
| 564 | |||
| 565 | $work->id($ug->create_str); | ||
| 566 | $work->task($task); | ||
| 567 | $work->queue($queue); | ||
| 568 | $work->options($topts); | ||
| 569 | $work->priority($pri); | ||
| 570 | $work->date_enqueued($now); | ||
| 571 | $work->date_to_execute($when); | ||
| 572 | |||
| 573 | # pass the Work to the Queue for enqueuing | ||
| 574 | |||
| 575 | $queue->enqueue($work); | ||
| 576 | |||
| 577 | Hopkins->log_debug("enqueued task $name (" . $work->id . ')'); | ||
| 578 | |||
| 579 | # notify the Store that we've enqueued a task | ||
| 580 | |||
| 581 | $kernel->post(store => notify => task_enqueued => $work->serialize); | ||
| 582 | |||
| 583 | return HOPKINS_ENQUEUE_OK; | ||
| 584 | } | ||
| 585 | |||
| 586 | sub complete | ||
| 587 | { | ||
| 588 | my $self = $_[OBJECT]; | ||
| 589 | my $kernel = $_[KERNEL]; | ||
| 590 | my $work = $_[ARG0]->[0]; | ||
| 591 | |||
| 592 | Hopkins->log_debug('completed task ' . $work->task->name . ' (' . $work->id . ')'); | ||
| 593 | |||
| 594 | $work->date_completed(DateTime->now(time_zone => 'local')); | ||
| 595 | |||
| 596 | $work->queue->dequeue($work); | ||
| 597 | |||
| 598 | $kernel->post(store => notify => task_completed => $work->serialize); | ||
| 599 | } | ||
| 600 | |||
| 601 | sub abort | ||
| 602 | { | ||
| 603 | my $self = $_[OBJECT]; | ||
| 604 | my $kernel = $_[KERNEL]; | ||
| 605 | my $name = $_[ARG0]; | ||
| 606 | my $id = $_[ARG1]; | ||
| 607 | |||
| 608 | my $queue = $self->queue($name) or return; | ||
| 609 | my $work = $queue->find($id) or return; | ||
| 610 | |||
| 611 | Hopkins->log_debug('aborting task ' . $work->task->name . ' (' . $work->id . ')'); | ||
| 612 | |||
| 613 | if ($work->worker) { | ||
| 614 | $kernel->post($work->worker->alias => 'terminate'); | ||
| 615 | } else { | ||
| 616 | $queue->dequeue($work); | ||
| 617 | $work->aborted(1); | ||
| 618 | |||
| 619 | $kernel->post(store => notify => task_completed => $work->serialize); | ||
| 620 | } | ||
| 621 | } | ||
| 622 | |||
| 623 | sub queue | ||
| 624 | { | ||
| 625 | my $self = shift; | ||
| 626 | my $name = shift; | ||
| 627 | |||
| 628 | return $self->queues->{$name}; | ||
| 629 | } | ||
| 630 | |||
| 631 | =back | ||
| 632 | |||
| 633 | =head1 AUTHOR | ||
| 634 | |||
| 635 | Mike Eldridge <diz@cpan.org> | ||
| 636 | |||
| 637 | =head1 LICENSE | ||
| 638 | |||
| 639 | =cut | ||
| 640 | |||
| 641 | 1; |