2 # module to timed tasks
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
17 use Time::HiRes qw(gettimeofday tv_interval);
22 use vars qw{@crontab @lcrontab @scrontab $mtime $lasttime $lastmin};
29 my $fn = "$main::cmd/crontab";
30 my $localfn = "$main::localcmd/crontab";
32 # cron initialisation / reading in cronjobs
35 if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
38 # first read in the standard one
42 @scrontab = cread($fn);
43 $mtime = $t if !$mtime || $t <= $mtime;
46 # then read in any local ones
50 @lcrontab = cread($localfn);
51 $mtime = $t if $t <= $mtime;
53 @crontab = (@scrontab, @lcrontab);
61 my $fh = new IO::File;
65 dbg("DXCron::cread reading $fn\n") if isdbg('cron');
66 open($fh, $fn) or confess("cron: can't open $fn $!");
70 next if /^\s*#/o or /^\s*$/o;
71 my ($min, $hour, $mday, $month, $wday, $cmd) = /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/o;
72 next unless defined $min;
76 if (defined $min && defined $hour && defined $cmd) { # it isn't all of them, but should be enough to tell if this is a real line
77 $err .= parse($ref, 'min', $min, 0, 60);
78 $err .= parse($ref, 'hour', $hour, 0, 23);
79 $err .= parse($ref, 'mday', $mday, 1, 31);
80 $err .= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
81 $err .= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
85 dbg("DXCron::cread: adding $_\n") if isdbg('cron');
88 LogDbg('cron', "DXCron::cread: error $err on line $line '$_'");
91 LogDbg('cron', "DXCron::cread error on line $line '$_'");
92 my @s = ($min, $hour, $mday, $month, $wday, $cmd);
93 my $s = "line $line splits as " . join(', ', (map {defined $_ ? qq{$_} : q{'undef'}} @s));
117 # handle comma delimited values
118 my @comma = split /,/o, $val;
120 my @minus = split /-/o;
122 return ", $sort should be $low >= $minus[0] <= $high" if $minus[0] < $low || $minus[0] > $high;
123 return ", $sort should be $low >= $minus[1] <= $high" if $minus[1] < $low || $minus[1] > $high;
125 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
129 return ", $sort should be $low >= $val <= $high" if $_ < $low || $_ > $high;
133 $ref->{$sort} = \@req;
138 # process the cronjobs
141 my $now = $main::systime;
142 return if $now-$lasttime < 1;
144 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
146 # are we at a minute boundary?
147 if ($min != $lastmin) {
149 # read in any changes if the modification time has changed
152 $mon += 1; # months otherwise go 0-11
154 foreach $cron (@crontab) {
155 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
156 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
157 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
158 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
159 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
162 dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
164 dbg("cron: cmd error $@") if $@ && isdbg('cron');
170 # remember when we are now
176 # these are simple stub functions to make connecting easy in DXCron contexts
179 # is it locally connected?
183 return DXChannel::get($call);
186 # is it remotely connected anywhere (with exact callsign)?
190 return Route::get($call);
193 # is it remotely connected anywhere (ignoring SSIDS)?
197 my $c = Route::get($call);
200 $c = Route::get("$call-$_");
207 # is it remotely connected anywhere (with exact callsign) and on node?
211 my $ncall = uc shift;
212 my $node = Route::Node::get($ncall);
213 return ($node) ? grep $call eq $_, $node->users : undef;
216 # is it remotely connected (ignoring SSIDS) and on node?
220 my $ncall = uc shift;
221 my $node = Route::Node::get($ncall);
224 $present = grep {/^$call/ } $node->users;
229 # last time this thing was connected
233 return $main::systime if DXChannel::get($call);
234 my $user = DXUser::get($call);
235 return $user ? $user->lastin : 0;
238 # disconnect a locally connected thing
242 run_cmd("disconnect $call");
245 # start a connect process off
249 # connecting is now done in one place - Yeah!
250 run_cmd("connect $call");
253 # spawn any old job off
257 my $t0 = [gettimeofday];
259 dbg("DXCron::spawn: $line") if isdbg("cron");
260 my $fc = DXSubprocess->new();
264 # diffms("DXCron spawn 1", $line, $t0, scalar @res) if isdbg('chan');
268 my ($fc, $err, @res) = @_;
270 my $s = "DXCron::spawn: error $err";
276 dbg("DXCron::spawn: $_") if isdbg("cron");
278 diffms("by DXCron::spawn", $line, $t0, scalar @res) if isdbg('progress');
286 my $t0 = [gettimeofday];
288 dbg("DXCron::spawn_cmd run: $line") if isdbg('cron');
289 my $fc = DXSubprocess->new();
292 $main::me->{_nospawn} = 1;
293 my @res = $main::me->run_cmd($line);
294 delete $main::me->{_nospawn};
295 # diffms("DXCron spawn_cmd 1", $line, $t0, scalar @res) if isdbg('chan');
299 my ($fc, $err, @res) = @_;
301 my $s = "DXCron::spawn_cmd: error $err";
306 dbg("DXCron::spawn_cmd: $_") if isdbg("cron");
308 diffms("by DXCron::spawn_cmd", $line, $t0, scalar @res) if isdbg('progress');
313 # do an rcmd to another cluster from the crontab
319 # can we see it? Is it a node?
320 my $noderef = Route::Node::get($call);
321 return unless $noderef && $noderef->version;
324 DXProt::addrcmd($main::me, $call, $line);
330 my @in = $main::me->run_cmd($line);
331 dbg("DXCron::run_cmd: $line") if isdbg('cron');
334 dbg("DXCron::cmd out: $_") if isdbg('cron');