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 $use_localtime};
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 $!");
67 while (my $l = <$fh>) {
70 next if $l =~ /^\s*#/ or $l =~ /^\s*$/;
71 if (my ($ts) = $l =~/^\s*LOCALE\s*=\s*(UTC|LOCAL)/i) {
75 } elsif ($ts eq 'LOCAL') {
78 dbg("DXCron: LOCALE set to $ts") if isdbg('cron');
80 my ($min, $hour, $mday, $month, $wday, $cmd) = $l =~ /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/;
81 next unless defined $min;
85 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
86 $err .= parse($ref, 'min', $min, 0, 60);
87 $err .= parse($ref, 'hour', $hour, 0, 23);
88 $err .= parse($ref, 'mday', $mday, 1, 31);
89 $err .= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
90 $err .= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
94 dbg("DXCron::cread: adding $l\n") if isdbg('cron');
97 LogDbg('cron', "DXCron::cread: error $err on line $line '$l'");
100 LogDbg('cron', "DXCron::cread error on line $line '$l'");
101 my @s = ($min, $hour, $mday, $month, $wday, $cmd);
102 my $s = "line $line splits as " . join(', ', (map {defined $_ ? qq{$_} : q{'undef'}} @s));
125 # handle comma delimited values
126 my @comma = split /,/o, $val;
128 my @minus = split /-/o;
130 return ", $sort should be $low >= $minus[0] <= $high" if $minus[0] < $low || $minus[0] > $high;
131 return ", $sort should be $low >= $minus[1] <= $high" if $minus[1] < $low || $minus[1] > $high;
133 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
137 return ", $sort should be $low >= $val <= $high" if $_ < $low || $_ > $high;
141 $ref->{$sort} = \@req;
146 # process the cronjobs
149 my $now = $main::systime;
150 return if $now-$lasttime < 1;
152 my ($sec, $min, $hour, $mday, $mon, $wday);
153 if ($use_localtime) {
154 ($sec, $min, $hour, $mday, $mon, $wday) = (localtime($now))[0,1,2,3,4,6];
156 ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
159 # are we at a minute boundary?
160 if ($min != $lastmin) {
162 # read in any changes if the modification time has changed
165 $mon += 1; # months otherwise go 0-11
167 foreach $cron (@crontab) {
168 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
169 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
170 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
171 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
172 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
175 my $s = $use_localtime ? "LOCALTIME" : "UTC";
176 dbg("cron: $s $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
178 dbg("cron: cmd error $@") if $@ && isdbg('cron');
184 # remember when we are now
190 # these are simple stub functions to make connecting easy in DXCron contexts
193 # is it locally connected?
197 return DXChannel::get($call);
200 # is it remotely connected anywhere (with exact callsign)?
204 return Route::get($call);
207 # is it remotely connected anywhere (ignoring SSIDS)?
211 my $c = Route::get($call);
214 $c = Route::get("$call-$_");
221 # is it remotely connected anywhere (with exact callsign) and on node?
225 my $ncall = uc shift;
226 my $node = Route::Node::get($ncall);
227 return ($node) ? grep $call eq $_, $node->users : undef;
230 # is it remotely connected (ignoring SSIDS) and on node?
234 my $ncall = uc shift;
235 my $node = Route::Node::get($ncall);
238 $present = grep {/^$call/ } $node->users;
243 # last time this thing was connected
247 return $main::systime if DXChannel::get($call);
248 my $user = DXUser::get($call);
249 return $user ? $user->lastin : 0;
252 # disconnect a locally connected thing
256 run_cmd("disconnect $call");
259 # start a connect process off
263 # connecting is now done in one place - Yeah!
264 run_cmd("connect $call");
267 # spawn any old job off
271 my $t0 = [gettimeofday];
273 dbg("DXCron::spawn: $line") if isdbg("cron");
274 my $fc = DXSubprocess->new();
278 # diffms("DXCron spawn 1", $line, $t0, scalar @res) if isdbg('chan');
282 my ($fc, $err, @res) = @_;
284 my $s = "DXCron::spawn: error $err";
290 dbg("DXCron::spawn: $_") if isdbg("cron");
292 diffms("by DXCron::spawn", $line, $t0, scalar @res) if isdbg('progress');
300 my $t0 = [gettimeofday];
302 dbg("DXCron::spawn_cmd run: $line") if isdbg('cron');
303 my $fc = DXSubprocess->new();
306 ++$main::me->{_nospawn};
307 my @res = $main::me->run_cmd($line);
308 # diffms("DXCron spawn_cmd 1", $line, $t0, scalar @res) if isdbg('chan');
312 my ($fc, $err, @res) = @_;
313 --$main::me->{_nospawn};
314 $main::me->{_nospawn} = 0 if exists $main::me->{_nospawn} && $main::me->{_nospawn} <= 0;
316 my $s = "DXCron::spawn_cmd: error $err";
321 dbg("DXCron::spawn_cmd: $_") if isdbg("cron");
323 diffms("by DXCron::spawn_cmd", $line, $t0, scalar @res) if isdbg('progress');
328 # do an rcmd to another cluster from the crontab
334 # can we see it? Is it a node?
335 my $noderef = Route::Node::get($call);
336 return unless $noderef && $noderef->version;
339 DXProt::addrcmd($main::me, $call, $line);
345 dbg("DXCron::run_cmd: $line") if isdbg('cron');
346 my @in = $main::me->run_cmd($line);
349 dbg("DXCron::cmd out: $_") if isdbg('cron');