summaryrefslogtreecommitdiff
path: root/Echolot/Scheduler.pm
blob: df245c8c7a580d23ac01dbe30094c2e9bdb5af88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package Echolot::Scheduler;

# (c) 2002 Peter Palfrader <peter@palfrader.org>
# $Id: Scheduler.pm,v 1.11 2002/07/22 01:28:21 weasel Exp $
#

=pod

=head1 Name

Echolot::Scheduler - Task selector/scheduler for echolot

=head1 DESCRIPTION

This package provides several functions for scheduling tasks within
the ping daemon.

=over

=cut

use strict;
use Carp qw{cluck confess};

my $ORDER = 1;

=item B<new> ()

Creates a new scheduler object.

=cut
sub new {
	my ($class, %params) = @_;
	my $self = {};
	bless $self, $class;
	return $self;
};

=item B<add> (I<name>, I<interval>, I<offset>, I<what>)

Adds a task with I<name> to the list of tasks. Every I<interval> seconds
I<what> is called. If for example I<interval> is 3600 - meaning I<what>
should be executed hourly - setting I<offset> to 600 would mean that
it get's called 10 minutes after the hour.

=cut
sub add($$$$$) {
	my ($self, $name, $interval, $offset, $what) = @_;

	confess("Must not add zero intervall for job $name")
		unless $interval;

	if (defined $self->{'tasks'}->{$name}) {
		@{ $self->{'schedule'} } = grep { $_->{'name'} ne $name } @{ $self->{'schedule'} };
	};

	$self->{'tasks'}->{$name} =
		{
			interval  => $interval,
			offset    => $offset,
			what      => $what,
			order     => $ORDER++
		};

	$self->schedule($name);
	
	return 1;
};

=item B<schedule> (I<name>, I<for>)

Schedule execution of I<name> for I<for>. If I<for> is not given it is calculated
from I<interval> and I<offset> passed to B<new>.

=cut
sub schedule($$;$$) {
	my ($self, $name, $for, $arguments) = @_;
	
	(defined $self->{'tasks'}->{$name}) or
		cluck("Task $name is not defined"),
		return 0;

	my $interval = $self->{'tasks'}->{$name}->{'interval'};
	my $offset = $self->{'tasks'}->{$name}->{'offset'};


	unless (defined $for) {
		($interval < 0) and
			return 1;
		my $now = time();
		$for = $now - $now % $interval + $offset;
		($for <= $now) and $for += $interval;
	};

	$arguments = [] unless defined $arguments;

	push @{ $self->{'schedule'} },
		{
			start => $for,
			order => $self->{'tasks'}->{$name}->{'order'},
			name => $name,
			arguments => $arguments
		};

	@{ $self->{'schedule'} } = sort { $a->{'start'} <=> $b->{'start'} or $a->{'order'} <=> $b->{'order'} }
		@{ $self->{'schedule'} };

	return 1;
};

=item B<run> ()

Start the scheduling run.

It will run forever or until a task with I<what> == 'exit' is executed.

=cut
sub run($) {
	my ($self) = @_;

	(defined $self->{'schedule'}->[0]) or
		cluck("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 {
			print "zZzZZzz at $now\n" if Echolot::Config::get()->{'verbose'};
			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" if Echolot::Config::get()->{'verbose'};
			last if ($what eq 'exit');
			&$what( $now, @{ $task->{'arguments'} } );
			$self->schedule($name, $now + $self->{'tasks'}->{$name}->{'interval'}) if
				($self->{'tasks'}->{$name}->{'interval'} > 0);

			(defined $self->{'schedule'}->[0]) or
				cluck("Scheduler is empty"),
				return 0;
		} while ($now >= $self->{'schedule'}->[0]->{'start'});
	};

	return 1;
};

# vim: set ts=4 shiftwidth=4: