| 1 | |||
| 2 | package Proc::JobQueue::EventQueue; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp qw(confess); | ||
| 7 | require Proc::JobQueue; | ||
| 8 | use Time::HiRes qw(time); | ||
| 9 | use Object::Dependency; | ||
| 10 | require POSIX; | ||
| 11 | |||
| 12 | our @ISA = qw(Proc::JobQueue); | ||
| 13 | |||
| 14 | our $timer_interval = 6; | ||
| 15 | my $debug = 0; | ||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | my ($pkg, %params) = @_; | ||
| 20 | |||
| 21 | $params{dependency_graph} ||= Object::Dependency->new(); | ||
| 22 | |||
| 23 | my $queue = $pkg->SUPER::new( | ||
| 24 | unloop => undef, | ||
| 25 | startmore_in_progress => 0, | ||
| 26 | on_failure => \&on_failure, | ||
| 27 | %params | ||
| 28 | ); | ||
| 29 | |||
| 30 | my $last_dump = time; | ||
| 31 | |||
| 32 | my $timer = IO::Event->timer( | ||
| 33 | interval => $params{timer_interval} || $timer_interval, | ||
| 34 | cb => sub { | ||
| 35 | print STDERR "beep!\n" if $debug; | ||
| 36 | eval { | ||
| 37 | $queue->startmore; | ||
| 38 | }; | ||
| 39 | if ($@) { | ||
| 40 | print STDERR "DIE DIE DIE DIE DIE (DT1): $@"; | ||
| 41 | # exit 1; hangs | ||
| 42 | POSIX::_exit(1); | ||
| 43 | }; | ||
| 44 | if ($debug && time > $last_dump + $timer_interval) { | ||
| 45 | $params{dependency_graph}->dump_graph(); | ||
| 46 | $last_dump = time; | ||
| 47 | } | ||
| 48 | use POSIX ":sys_wait_h"; | ||
| 49 | my $k; | ||
| 50 | do { $k = waitpid(-1, WNOHANG) } while $k > 0; | ||
| 51 | }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | $Event::DIED = sub { | ||
| 55 | Event::verbose_exception_handler(@_); | ||
| 56 | $queue->unloop(); | ||
| 57 | IO::Event::unloop_all(); | ||
| 58 | }; | ||
| 59 | |||
| 60 | return $queue; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub unloop | ||
| 64 | { | ||
| 65 | my ($queue) = @_; | ||
| 66 | if ($queue->{unloop}) { | ||
| 67 | $queue->unloop($queue->alldone); | ||
| 68 | } else { | ||
| 69 | IO::Event::unloop_all(); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub on_failure | ||
| 74 | { | ||
| 75 | my ($queue, $job, @exit_code) = @_; | ||
| 76 | if ($job->{on_failure}) { | ||
| 77 | $job->{on_failure}->(@exit_code); | ||
| 78 | } elsif ($job->{errors}) { | ||
| 79 | $job->{errors}->("FAILED: $job->{desc}", @exit_code); | ||
| 80 | } else { | ||
| 81 | print STDERR "JOB $job->{desc} FAILED\nexit @exit_code\n"; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | 1; | ||
| 87 | |||
| 88 | __END__ |