| 1 | package Hopkins::Queue; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Hopkins::Queue - hopkins queue states and methods | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Hopkins::Queue contains all of the POE event handlers and | ||
| 12 | supporting methods for the initialization and management of | ||
| 13 | each configured hopkins queue. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | use POE; | ||
| 18 | use Class::Accessor::Fast; | ||
| 19 | |||
| 20 | use Cache::FileCache; | ||
| 21 | use DateTime::Format::ISO8601; | ||
| 22 | use Tie::IxHash; | ||
| 23 | |||
| 24 | use Hopkins::Constants; | ||
| 25 | use Hopkins::Worker; | ||
| 26 | use Hopkins::Work; | ||
| 27 | |||
| 28 | use base 'Class::Accessor::Fast'; | ||
| 29 | |||
| 30 | __PACKAGE__->mk_accessors(qw(kernel config cache name alias onerror onfatal concurrency works halted frozen error)); | ||
| 31 | |||
| 32 | =head1 STATES | ||
| 33 | |||
| 34 | =over 4 | ||
| 35 | |||
| 36 | =item new | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub new | ||
| 41 | { | ||
| 42 | my $self = shift->SUPER::new(@_); | ||
| 43 | |||
| 44 | Hopkins->log_debug('creating queue ' . $self->name); | ||
| 45 | |||
| 46 | $self->alias('queue.' . $self->name); | ||
| 47 | $self->works(new Tie::IxHash); | ||
| 48 | |||
| 49 | $self->onerror(undef) | ||
| 50 | unless $self->onerror | ||
| 51 | and grep { $self->onerror eq $_ } qw(halt freeze shutdown flush); | ||
| 52 | |||
| 53 | $self->cache(new Cache::FileCache { | ||
| 54 | cache_root => $self->config->fetch('state/root')->stringify, | ||
| 55 | namespace => 'queue/' . $self->name, | ||
| 56 | directory_umask => 0077 | ||
| 57 | }); | ||
| 58 | |||
| 59 | $self->read_state; | ||
| 60 | $self->write_state; | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub read_state | ||
| 66 | { | ||
| 67 | my $self = shift; | ||
| 68 | |||
| 69 | $self->frozen($self->cache->get('frozen') ? 1 : 0); | ||
| 70 | $self->halted($self->cache->get('halted') ? 1 : 0); | ||
| 71 | $self->error($self->cache->get('error')); | ||
| 72 | |||
| 73 | my $aref = $self->cache->get('works'); | ||
| 74 | |||
| 75 | return if not ref $aref eq 'ARRAY'; | ||
| 76 | |||
| 77 | foreach my $href (@$aref) { | ||
| 78 | my $work = new Hopkins::Work; | ||
| 79 | my $date = undef; | ||
| 80 | |||
| 81 | $work->queue($self); | ||
| 82 | $work->options($href->{options}); | ||
| 83 | |||
| 84 | if (my $id = $href->{id}) { | ||
| 85 | $work->id($id); | ||
| 86 | } else { | ||
| 87 | Hopkins->log_error('unable to determine task ID when reading state'); | ||
| 88 | next; | ||
| 89 | } | ||
| 90 | |||
| 91 | if (my $date = Hopkins->parse_datetime($href->{date_enqueued})) { | ||
| 92 | $work->date_enqueued($date); | ||
| 93 | } else { | ||
| 94 | Hopkins->log_error('unable to parse date/time information when reading state'); | ||
| 95 | } | ||
| 96 | |||
| 97 | # FIXME: date_to_execute not yet implemented | ||
| 98 | |||
| 99 | if (my $date = Hopkins->parse_datetime($href->{date_to_execute})) { | ||
| 100 | $work->date_to_execute($date); | ||
| 101 | } else { | ||
| 102 | #Hopkins->log_error('unable to parse date/time information when reading state'); | ||
| 103 | $work->date_to_execute(DateTime->now(time_zone => 'local')); | ||
| 104 | } | ||
| 105 | |||
| 106 | if (my $val = $href->{date_started}) { | ||
| 107 | if (my $date = Hopkins->parse_datetime($href->{date_started})) { | ||
| 108 | $work->date_started($date); | ||
| 109 | } else { | ||
| 110 | Hopkins->log_error('unable to parse date/time information when reading state'); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | # attempt to locate the referenced task. if the | ||
| 115 | # configuration has changed and we can't locate the | ||
| 116 | # referenced task, we'll want to halt the queue | ||
| 117 | # until an operator can take a look at it. | ||
| 118 | |||
| 119 | if (my $task = $self->config->get_task_info($href->{task})) { | ||
| 120 | $work->task($task); | ||
| 121 | } else { | ||
| 122 | Hopkins->log_error("unable to locate task '$href->{task}' when reading state"); | ||
| 123 | |||
| 124 | $self->kernel->post(store => notify => task_aborted => $work->serialize); | ||
| 125 | } | ||
| 126 | |||
| 127 | # if the task was already started or has an invalid | ||
| 128 | # vconfiguration, mark it as orphaned. otherwise, | ||
| 129 | # go ahead and queue it up for execution. | ||
| 130 | |||
| 131 | if ($work->date_started or not defined $work->task) { | ||
| 132 | $self->kernel->post(store => notify => task_orphaned => $work->serialize); | ||
| 133 | } else { | ||
| 134 | $self->enqueue($work, write => 0); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | sub spawn | ||
| 140 | { | ||
| 141 | my $self = shift; | ||
| 142 | |||
| 143 | Hopkins->log_debug('spawning queue ' . $self->name); | ||
| 144 | |||
| 145 | # this passive queue will act as an on-demand task | ||
| 146 | # execution queue, waiting for enqueue events to be | ||
| 147 | # posted to the kernel. | ||
| 148 | |||
| 149 | #POE::Component::JobQueue->spawn | ||
| 150 | #( | ||
| 151 | # Alias => $self->alias, | ||
| 152 | # WorkerLimit => $self->concurrency, | ||
| 153 | # Worker => sub { $self->spawn_worker(@_) }, | ||
| 154 | # Passive => { Prioritizer => \&Hopkins::Queue::prioritize }, | ||
| 155 | #); | ||
| 156 | |||
| 157 | #foreach my $work ($self->tasks->Values) { | ||
| 158 | # $self->kernel->post($self->alias => enqueue => dequeue => $work); | ||
| 159 | #} | ||
| 160 | |||
| 161 | # this active queue will poll hopkins periodically, | ||
| 162 | # checking the parent Hopkins::Queue object for new | ||
| 163 | # tasks to spawn. | ||
| 164 | |||
| 165 | POE::Component::JobQueue->spawn | ||
| 166 | ( | ||
| 167 | Alias => $self->alias, | ||
| 168 | WorkerLimit => $self->concurrency, | ||
| 169 | Worker => sub { $self->fetch_and_spawn_worker(@_) }, | ||
| 170 | Active => | ||
| 171 | { | ||
| 172 | #PollInterval => $global->{poll}, | ||
| 173 | PollInterval => 15, | ||
| 174 | AckAlias => 'manager', | ||
| 175 | AckState => 'complete' | ||
| 176 | } | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | |||
| 180 | =item contents | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | sub contents | ||
| 185 | { | ||
| 186 | my $self = shift; | ||
| 187 | my $args = ref $_[0] eq 'HASH' ? shift : { @_ }; | ||
| 188 | |||
| 189 | my $date = $args->{executing}; | ||
| 190 | my @works = $self->works->Values; | ||
| 191 | |||
| 192 | return $date ? grep { $_->date_to_execute < $date } @works : @works; | ||
| 193 | } | ||
| 194 | |||
| 195 | =item find | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | sub find | ||
| 200 | { | ||
| 201 | my $self = shift; | ||
| 202 | my $id = shift; | ||
| 203 | |||
| 204 | return $self->works->FETCH($id); | ||
| 205 | } | ||
| 206 | |||
| 207 | =item enqueue | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub enqueue | ||
| 212 | { | ||
| 213 | my $self = shift; | ||
| 214 | my $work = shift; | ||
| 215 | my $args = { @_ }; | ||
| 216 | |||
| 217 | $self->works->Push($work->id => $work); | ||
| 218 | $self->write_state unless $args->{write} and $args->{write} == 0; | ||
| 219 | } | ||
| 220 | |||
| 221 | =item dequeue | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub dequeue | ||
| 226 | { | ||
| 227 | my $self = shift; | ||
| 228 | my $work = shift; | ||
| 229 | my $args = { @_ }; | ||
| 230 | |||
| 231 | my $id = UNIVERSAL::isa($work, 'Hopkins::Work') ? $work->id : $work; | ||
| 232 | |||
| 233 | $self->works->Delete($id); | ||
| 234 | $self->write_state unless $args->{write} and $args->{write} == 0; | ||
| 235 | } | ||
| 236 | |||
| 237 | =item write_state | ||
| 238 | |||
| 239 | write the queue's state to disk. | ||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | sub write_state | ||
| 244 | { | ||
| 245 | my $self = shift; | ||
| 246 | |||
| 247 | $self->cache->set(frozen => $self->frozen); | ||
| 248 | $self->cache->set(halted => $self->halted); | ||
| 249 | $self->cache->set(error => $self->error); | ||
| 250 | $self->cache->set(works => [ map { $_->serialize } $self->works->Values ]); | ||
| 251 | } | ||
| 252 | |||
| 253 | =item stop | ||
| 254 | |||
| 255 | stops the queue, shutting down the PoCo::JobQueue session | ||
| 256 | if running by sending a stop event to it. | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub stop | ||
| 261 | { | ||
| 262 | my $self = shift; | ||
| 263 | |||
| 264 | $self->kernel->post($self->alias => 'stop') if $self->kernel; | ||
| 265 | } | ||
| 266 | |||
| 267 | =item spawn_worker | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | sub spawn_worker | ||
| 272 | { | ||
| 273 | my $self = shift; | ||
| 274 | |||
| 275 | my $args = | ||
| 276 | { | ||
| 277 | postback => shift, | ||
| 278 | work => shift, | ||
| 279 | queue => $self | ||
| 280 | }; | ||
| 281 | |||
| 282 | new Hopkins::Worker $args; | ||
| 283 | } | ||
| 284 | |||
| 285 | =item fetch_and_spawn_worker | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub fetch_and_spawn_worker | ||
| 290 | { | ||
| 291 | my $self = shift; | ||
| 292 | my $postback = shift; | ||
| 293 | |||
| 294 | return if $self->halted; | ||
| 295 | |||
| 296 | #Hopkins->log_debug('polling ' . $self->name . ' queue for tasks to execute'); | ||
| 297 | |||
| 298 | my $now = DateTime->now(time_zone => 'local'); | ||
| 299 | my @work = | ||
| 300 | sort Hopkins::Queue::prioritize | ||
| 301 | grep { not defined $_->worker and $_->date_to_execute < $now } | ||
| 302 | $self->works->Values; | ||
| 303 | |||
| 304 | if (my $work = shift @work) { | ||
| 305 | my $args = | ||
| 306 | { | ||
| 307 | postback => $postback->($work), | ||
| 308 | work => $work, | ||
| 309 | queue => $self | ||
| 310 | }; | ||
| 311 | |||
| 312 | new Hopkins::Worker $args; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | =item prioritize | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | sub prioritize | ||
| 321 | { | ||
| 322 | my $apri = $a->priority; | ||
| 323 | my $bpri = $b->priority; | ||
| 324 | |||
| 325 | $apri = 1 if $apri < 1; | ||
| 326 | $apri = 9 if $apri > 9; | ||
| 327 | $bpri = 1 if $bpri < 1; | ||
| 328 | $bpri = 9 if $bpri > 9; | ||
| 329 | |||
| 330 | return $apri <=> $bpri; | ||
| 331 | } | ||
| 332 | |||
| 333 | =item is_running | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | sub is_running | ||
| 338 | { | ||
| 339 | my $self = shift; | ||
| 340 | my $name = shift; | ||
| 341 | |||
| 342 | return Hopkins->is_session_active($self->alias); | ||
| 343 | } | ||
| 344 | |||
| 345 | =item status | ||
| 346 | |||
| 347 | =cut | ||
| 348 | |||
| 349 | sub status | ||
| 350 | { | ||
| 351 | my $self = shift; | ||
| 352 | |||
| 353 | return HOPKINS_QUEUE_STATUS_HALTED if $self->halted; | ||
| 354 | return HOPKINS_QUEUE_STATUS_RUNNING if $self->works->Length > 0; | ||
| 355 | |||
| 356 | return HOPKINS_QUEUE_STATUS_IDLE; | ||
| 357 | } | ||
| 358 | |||
| 359 | =item status_string | ||
| 360 | |||
| 361 | =cut | ||
| 362 | |||
| 363 | sub status_string | ||
| 364 | { | ||
| 365 | my $self = shift; | ||
| 366 | |||
| 367 | for ($self->status) { | ||
| 368 | $_ == HOPKINS_QUEUE_STATUS_IDLE && return 'idle'; | ||
| 369 | |||
| 370 | #$_ == HOPKINS_QUEUE_STATUS_RUNNING && $self->frozen | ||
| 371 | # && return 'running (frozen)'; | ||
| 372 | |||
| 373 | #$_ == HOPKINS_QUEUE_STATUS_HALTED && $self->frozen | ||
| 374 | # && return 'halted (frozen)'; | ||
| 375 | |||
| 376 | $_ == HOPKINS_QUEUE_STATUS_RUNNING && return 'running'; | ||
| 377 | $_ == HOPKINS_QUEUE_STATUS_HALTED && return 'halted'; | ||
| 378 | } | ||
| 379 | } | ||
| 380 | |||
| 381 | =item num_queued | ||
| 382 | |||
| 383 | =cut | ||
| 384 | |||
| 385 | sub num_queued | ||
| 386 | { | ||
| 387 | my $self = shift; | ||
| 388 | my $task = shift; | ||
| 389 | |||
| 390 | return $self->works->Length if not defined $task; | ||
| 391 | |||
| 392 | if (not ref $task eq 'Hopkins::Task') { | ||
| 393 | Hopkins->log_warn('Hopkins::Queue->num_queued called with argument that is not a Hopkins::Task object'); | ||
| 394 | return 0; | ||
| 395 | } | ||
| 396 | |||
| 397 | return scalar grep { $_->task->name eq $task->name } $self->works->Values | ||
| 398 | } | ||
| 399 | |||
| 400 | =item start | ||
| 401 | |||
| 402 | =cut | ||
| 403 | |||
| 404 | sub start | ||
| 405 | { | ||
| 406 | my $self = shift; | ||
| 407 | |||
| 408 | $self->error(undef); | ||
| 409 | $self->halted(0); | ||
| 410 | |||
| 411 | return HOPKINS_QUEUE_ALREADY_RUNNING if $self->is_running; | ||
| 412 | |||
| 413 | $self->spawn; | ||
| 414 | |||
| 415 | return HOPKINS_QUEUE_STARTED; | ||
| 416 | } | ||
| 417 | |||
| 418 | =item halt | ||
| 419 | |||
| 420 | halts the queue. no tasks will be executed, although tasks | ||
| 421 | may still be enqueued. | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | sub halt | ||
| 426 | { | ||
| 427 | my $self = shift; | ||
| 428 | |||
| 429 | $self->halted(1); | ||
| 430 | } | ||
| 431 | |||
| 432 | =item continue | ||
| 433 | |||
| 434 | reverses the action of halt by starting the queue back up. | ||
| 435 | the existing state of the frozen flag will be preserved. | ||
| 436 | |||
| 437 | =cut | ||
| 438 | |||
| 439 | sub continue | ||
| 440 | { | ||
| 441 | my $self = shift; | ||
| 442 | |||
| 443 | $self->halted(0); | ||
| 444 | } | ||
| 445 | |||
| 446 | =item freeze | ||
| 447 | |||
| 448 | freezes the queue. no more tasks will be enqueued, but | ||
| 449 | currently queued tasks will be allowed to execute. | ||
| 450 | |||
| 451 | =cut | ||
| 452 | |||
| 453 | sub freeze | ||
| 454 | { | ||
| 455 | my $self = shift; | ||
| 456 | |||
| 457 | $self->frozen(1); | ||
| 458 | } | ||
| 459 | |||
| 460 | =item thaw | ||
| 461 | |||
| 462 | reverses the action of freeze by unsetting the frozen flag. | ||
| 463 | tasks will not be queable. the existing halt state will be | ||
| 464 | preserved. | ||
| 465 | |||
| 466 | =cut | ||
| 467 | |||
| 468 | sub thaw | ||
| 469 | { | ||
| 470 | my $self = shift; | ||
| 471 | |||
| 472 | $self->frozen(0); | ||
| 473 | } | ||
| 474 | |||
| 475 | =item shutdown | ||
| 476 | |||
| 477 | shuts the queue down. this is basically a shortcut for the | ||
| 478 | freeze and halt actions. no more tasks will be executed and | ||
| 479 | no further tasks may be enqueud. | ||
| 480 | |||
| 481 | =cut | ||
| 482 | |||
| 483 | sub shutdown | ||
| 484 | { | ||
| 485 | my $self = shift; | ||
| 486 | |||
| 487 | $self->freeze; | ||
| 488 | $self->halt; | ||
| 489 | } | ||
| 490 | |||
| 491 | =item flush | ||
| 492 | |||
| 493 | flush the queue of any tasks waiting to execute. stops the | ||
| 494 | PoCo::JobQueue session (if running) and clears the internal | ||
| 495 | list of tasks. if the queue was running prior to the flush, | ||
| 496 | the PoCo::JobQueue session is spun back up. | ||
| 497 | |||
| 498 | =cut | ||
| 499 | |||
| 500 | sub flush | ||
| 501 | { | ||
| 502 | my $self = shift; | ||
| 503 | |||
| 504 | $self->stop; | ||
| 505 | $self->works->Delete($self->works->Keys); | ||
| 506 | $self->start if not $self->halted; | ||
| 507 | } | ||
| 508 | |||
| 509 | =item DESTROY | ||
| 510 | |||
| 511 | =cut | ||
| 512 | |||
| 513 | sub DESTROY { shift->shutdown } | ||
| 514 | |||
| 515 | =back | ||
| 516 | |||
| 517 | =head1 AUTHOR | ||
| 518 | |||
| 519 | Mike Eldridge <diz@cpan.org> | ||
| 520 | |||
| 521 | =head1 LICENSE | ||
| 522 | |||
| 523 | This program is free software; you may redistribute it | ||
| 524 | and/or modify it under the same terms as Perl itself. | ||
| 525 | |||
| 526 | =cut | ||
| 527 | |||
| 528 | 1; |