-
Notifications
You must be signed in to change notification settings - Fork 4
/
scheduler.pl
executable file
·325 lines (264 loc) · 8.4 KB
/
scheduler.pl
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/perl -w
#
# (C) 2000 - 2005 Wieger Opmeer, Casper Joost Eyckelhof, Yvo Brevoort
#
# This package is free software; you can redistribute it and/or modify it
# under the terms of the "Artistic License".
#
# TODO: scalability issues, like not keeping whole list in memory
#
use strict;
use lib './lib';
use Multigate::Debug;
use IO::Select;
use DateTime;
use Time::ParseDate;
Multigate::Debug::setdebug('scheduler');
#Multigate::Debug::setdebug('scheduler_debug');
$| = 1; # autoflush!
#
# Globals and such
#
my $multiroot = $ENV{MULTI_ROOT} or die "\$ENV{MULTI_ROOT} undefined!";
my @events = ();
my $schedulefile = "$multiroot/logs/events.txt";
my $tempfile = "$multiroot/logs/events.txt.tmp";
#
# return formatted time for printing in preffered format
#
sub formattedtime {
my $timestamp = shift;
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime($timestamp);
return sprintf(
'%04d-%02d-%02dT%02d:%02d:%02d',
$year + 1900,
$mon + 1, $mday, $hour, $min, $sec
);
}
#
# Writes current @events to file
#
sub synchronize {
debug( 'scheduler_debug',
'------Current list of scheduled events:-------' );
my $event;
open( NEW, "> $tempfile" );
foreach $event (@events) {
print NEW $event, "\n";
debug( 'scheduler_debug', $event );
}
close NEW;
rename( $tempfile, $schedulefile )
or debug( 'scheduler',
"WARNING: Couldn't rename $tempfile to $schedulefile: $!" );
debug( 'scheduler_debug', '------End of list-------' );
}
#
# Add an entry to the list of scheduled events if it has an understandable format
# pre: list is sorted, length = n
# post: list is sorted, length = n+1
# addentry returns success(1) or failure(0)
#
sub addentry {
my $line = shift;
my ( $snip_incoming, $protocol, $sender, $org_time, $msg ) = split " ",
$line, 5;
# Check if we have a time specification
unless ( defined $org_time ) {
print "TOPROTOCOL $protocol $sender No time specification!\n";
debug( 'scheduler', "User gave empty time specification" );
return;
}
# Check if we have a message part
unless ( defined $msg ) {
print "TOPROTOCOL $protocol $sender Nothing to do!\n";
debug( 'scheduler', "User gave empty message" );
return;
}
# Try to parse the time specification
my $time = $org_time;
$time =~ s/\./:/g; # allow use of '.' instead of ':'
$time =~ s/-/\//g; # allow use of '-' instead of '/'
$time =~ s/_/ /g; # gross hack to introduce spaces
$time =~ s/T/ /g; # another gross hack to introduce spaces
my $timestamp = time;
# calculate the timestamp for this event
# assume 2 possibilities:
# +[[days:]hours:]minutes -> current_time + days:hours:minutes
# !^+ -> first occurence of that time from now
# teatime -> first occurence of 16:00
debug( 'scheduler_debug', "Trying to parse: $time" );
if( $time =~ /^teatime$/ ) { # its teatime
$time = "16:00";
}
if ( $time =~ /^\s*\+([0-9:]+)\s*$/ ) { # relative time
my ( $mn, $hr, $dy, $mo, $yr ) = reverse split /:/, $1;
my $datetime =
DateTime->now->set_time_zone('local')->truncate( to => 'minute' );
$datetime->add(
years => ( defined $yr ) ? $yr : 0,
months => ( defined $mo ) ? $mo : 0,
days => ( defined $dy ) ? $dy : 0,
hours => ( defined $hr ) ? $hr : 0,
minutes => ( defined $mn ) ? $mn : 0,
);
$timestamp = $datetime->epoch;
}
else {
$timestamp = parsedate( $time, UK => 1, PREFER_FUTURE => 1 );
if ( defined $timestamp ) {
my $datetime =
DateTime->from_epoch( epoch => $timestamp )
->set_time_zone('local');
if ( $timestamp <= time() ) {
$datetime->add( days => 1 );
}
$timestamp = $datetime->epoch;
}
}
unless ( defined $timestamp ) {
print
"TOPROTOCOL $protocol $sender Unparsable time specification $org_time\n";
debug( 'scheduler', "Could not parse input $time ($org_time)" );
return;
}
if ( $timestamp < time ) {
print "TOPROTOCOL $protocol $sender $org_time is in the past!\n";
debug( 'scheduler', "User gave time in the past: $time ($org_time)" );
return;
}
# Fixup $msg if it does not begin with a !command
unless ( $msg =~ /^!/ ) {
$msg = '!echo ' . $msg;
}
# Prepare scheduled_string to add to the list..
my $scheduled_string = "$timestamp $protocol $sender $msg";
# And notify the user..
print "TOPROTOCOL $protocol $sender Scheduled event for ",
formattedtime($timestamp), "\n";
# And us..
debug(
'scheduler_debug',
'Scheduled event for ',
formattedtime($timestamp),
' (',
$timestamp,
'), now is ',
formattedtime(time),
' (',
time,
'). Event in ',
$timestamp - time,
' seconds.'
);
# Insert scheduled_string into the list of scheduled events, keeping it sorted!
# First find insertion point
my $listcounter = 0;
my $timestamp_list
; # temporary variable to store timestamps from the scheduled events list
my $rest;
foreach $line (@events) {
( $timestamp_list, $rest ) = split ' ', $line, 2;
if ( $timestamp_list <= $timestamp ) {
$listcounter++; # Keep on searching
}
else {
last; # Found insertion point
}
}
# Now we know where to insert our new entry:
splice @events, $listcounter, 0, $scheduled_string;
# Write updated @events to file:
synchronize();
# All done!
}
#
# Execute all events from the list that have a scheduled timestamp <= now
# Remove those events from the list
# Return the timeout untill the next event
#
sub checkschedule {
#only check if there are events
if (@events) {
my $changes = 0;
my $now = time;
my $timestamp;
my $rest;
my $line;
my $timeout;
# We know that @events is sorted, find all items that have a timestamp < $now
foreach $line (@events) {
( $timestamp, $rest ) = split ' ', $line, 2;
$timeout = $timestamp - $now;
debug( 'scheduler_debug',
"Checking event: ($rest) scheduled in $timestamp - $now = $timeout seconds"
);
if ( $timeout <= 0 ) {
print 'TODISPATCHER ', $rest, "\n";
debug( 'scheduler_debug', 'Sent to dispatcher: ', $rest );
$changes++;
}
else {
last; # exit loop if we're past $now
}
}
# kick first n entries from @events
splice @events, 0, $changes;
# write all changes to file if necesarry
synchronize() if $changes > 0;
return $timeout;
}
else {
return undef;
}
}
#
# Main program:
# restore @events from file
#
if ( ( -e $schedulefile ) && ( !-z $schedulefile ) ) {
open( EVENTS, "< $schedulefile" );
@events = <EVENTS>;
close EVENTS;
chomp @events;
}
debug( 'scheduler', 'Restored ', scalar @events, ' entries from file.' );
#
# for select on STDIN, build a readset with only STDIN
#
my $readset = IO::Select->new();
$readset->add( fileno(STDIN) );
my $r_ready; # to store read_ready filehandles
my $timeout = checkschedule();
#
# mainloop
#
while (1) {
debug( 'scheduler_debug', 'Timeout: ',
( defined $timeout ) ? $timeout : 'undef' );
($r_ready) = IO::Select->select( $readset, undef, undef, $timeout );
if ($r_ready) {
# We have someone knocking on STDIN, lets read what it is
my $line = <STDIN>;
unless ( defined $line ) {
debug( 'scheduler', 'Going down. (parent died?)' );
exit 0;
}
chomp($line);
debug( 'scheduler_debug', 'Reading: ', formattedtime( time() ),
' ', $line );
exit if ( $line eq 'DIEDIEDIE' ); # die without a fuzz
# add the line to the scheduled list
addentry($line);
}
else {
# timeout occured
#debug('scheduler_debug', 'Timeout: ', scalar(localtime) );
}
# Now that we are awake, check if something has to be done:
$timeout = checkschedule();
}
#
# The end
#