From 825b50b586c911daf73a007387c2153b5f6a5f4f Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Thu, 20 Jun 2002 04:28:13 +0000 Subject: Change some things so that signal handlers may queue tasks more easily --- Echolot/Scheduler.pm | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'Echolot') diff --git a/Echolot/Scheduler.pm b/Echolot/Scheduler.pm index e54eef1..711b9e0 100644 --- a/Echolot/Scheduler.pm +++ b/Echolot/Scheduler.pm @@ -1,7 +1,7 @@ package Echolot::Scheduler; # (c) 2002 Peter Palfrader -# $Id: Scheduler.pm,v 1.4 2002/06/13 15:27:25 weasel Exp $ +# $Id: Scheduler.pm,v 1.5 2002/06/20 04:28:13 weasel Exp $ # =pod @@ -67,8 +67,6 @@ sub add($$$$$) { =item B (I, I) -Internal function. - Schedule execution of I for I. If I is not given it is calculated from I and I passed to B. @@ -85,6 +83,8 @@ sub schedule($$;$) { unless (defined $for) { + ($interval < 0) and + return 1; my $now = time(); $for = $now - $now % $interval + $offset; ($for <= $now) and $for += $interval; @@ -113,13 +113,13 @@ It will run forever or until a task with I == 'exit' is executed. sub run($) { my ($self) = @_; - my $task = shift @{ $self->{'schedule'} }; - (defined $task) or + (defined $self->{'schedule'}->[0]) or croak("Scheduler is empty"), return 0; while(1) { my $now = time(); + my $task = $self->{'schedule'}->[0]; if ($task->{'start'} < $now) { warn("Task $task->{'name'} could not be started on time\n"); } else { @@ -127,23 +127,27 @@ sub run($) { sleep ($task->{'start'} - $now); }; + (time() < $task->{'start'}) and + next; + $now = $task->{'start'}; do { + $task = shift @{ $self->{'schedule'} }; my $name = $task->{'name'}; (defined $self->{'tasks'}->{$name}) or warn("Task $task->{'name'} is not defined\n"); my $what = $self->{'tasks'}->{$name}->{'what'}; + print "Running $name at ".(time())." (scheduled for $now)\n"; last if ($what eq 'exit'); - print "Running $name at $now\n"; &$what(); - $self->schedule($name, $now + $self->{'tasks'}->{$name}->{'interval'}); + $self->schedule($name, $now + $self->{'tasks'}->{$name}->{'interval'}) if + ($self->{'tasks'}->{$name}->{'interval'} > 0); - $task = shift @{ $self->{'schedule'} }; - (defined $task) or + (defined $self->{'schedule'}->[0]) or croak("Scheduler is empty"), return 0; - } while ($now == $task->{'start'}); + } while ($now >= $self->{'schedule'}->[0]->{'start'}); }; return 1; -- cgit v1.2.3