2 # module to timed tasks
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
17 use Time::HiRes qw(gettimeofday tv_interval);
18 use Mojo::IOLoop::Subprocess;
23 use vars qw{@crontab @lcrontab @scrontab $mtime $lasttime $lastmin};
30 my $fn = "$main::cmd/crontab";
31 my $localfn = "$main::localcmd/crontab";
33 # cron initialisation / reading in cronjobs
36 if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
39 # first read in the standard one
43 @scrontab = cread($fn);
44 $mtime = $t if !$mtime || $t <= $mtime;
47 # then read in any local ones
51 @lcrontab = cread($localfn);
52 $mtime = $t if $t <= $mtime;
54 @crontab = (@scrontab, @lcrontab);
62 my $fh = new IO::File;
66 dbg("DXCron::cread reading $fn\n") if isdbg('cron');
67 open($fh, $fn) or confess("cron: can't open $fn $!");
71 next if /^\s*#/o or /^\s*$/o;
72 my ($min, $hour, $mday, $month, $wday, $cmd) = /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/o;
73 next unless defined $min;
77 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
78 $err .= parse($ref, 'min', $min, 0, 60);
79 $err .= parse($ref, 'hour', $hour, 0, 23);
80 $err .= parse($ref, 'mday', $mday, 1, 31);
81 $err .= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
82 $err .= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
86 dbg("DXCron::cread: adding $_\n") if isdbg('cron');
89 LogDbg('cron', "DXCron::cread: error $err on line $line '$_'");
92 LogDbg('cron', "DXCron::cread error on line $line '$_'");
93 my @s = ($min, $hour, $mday, $month, $wday, $cmd);
94 my $s = "line $line splits as " . join(', ', (map {defined $_ ? qq{$_} : q{'undef'}} @s));
118 # handle comma delimited values
119 my @comma = split /,/o, $val;
121 my @minus = split /-/o;
123 return ", $sort should be $low >= $minus[0] <= $high" if $minus[0] < $low || $minus[0] > $high;
124 return ", $sort should be $low >= $minus[1] <= $high" if $minus[1] < $low || $minus[1] > $high;
126 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
130 return ", $sort should be $low >= $val <= $high" if $_ < $low || $_ > $high;
134 $ref->{$sort} = \@req;
139 # process the cronjobs
142 my $now = $main::systime;
143 return if $now-$lasttime < 1;
145 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
147 # are we at a minute boundary?
148 if ($min != $lastmin) {
150 # read in any changes if the modification time has changed
153 $mon += 1; # months otherwise go 0-11
155 foreach $cron (@crontab) {
156 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
157 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
158 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
159 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
160 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
163 dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
165 dbg("cron: cmd error $@") if $@ && isdbg('cron');
171 # remember when we are now
177 # these are simple stub functions to make connecting easy in DXCron contexts
180 # is it locally connected?
184 return DXChannel::get($call);
187 # is it remotely connected anywhere (with exact callsign)?
191 return Route::get($call);
194 # is it remotely connected anywhere (ignoring SSIDS)?
198 my $c = Route::get($call);
201 $c = Route::get("$call-$_");
208 # is it remotely connected anywhere (with exact callsign) and on node?
212 my $ncall = uc shift;
213 my $node = Route::Node::get($ncall);
214 return ($node) ? grep $call eq $_, $node->users : undef;
217 # is it remotely connected (ignoring SSIDS) and on node?
221 my $ncall = uc shift;
222 my $node = Route::Node::get($ncall);
225 $present = grep {/^$call/ } $node->users;
230 # last time this thing was connected
234 return $main::systime if DXChannel::get($call);
235 my $user = DXUser::get($call);
236 return $user ? $user->lastin : 0;
239 # disconnect a locally connected thing
243 run_cmd("disconnect $call");
246 # start a connect process off
250 # connecting is now done in one place - Yeah!
251 run_cmd("connect $call");
254 # spawn any old job off
258 my $t0 = [gettimeofday];
260 dbg("DXCron::spawn: $line") if isdbg("cron");
261 my $fc = DXSubprocess->new();
268 my ($fc, $err, @res) = @_;
270 my $s = "DXCron::spawn: error $err";
276 dbg("DXCron::spawn: $_") if isdbg("cron");
278 diffms(__PACKAGE__, "::spawn", $line, $t0, scalar @res) if isdbg('progress');
286 my $chan = shift || $main::me;
287 my $pkg = ref $chan || __PACKAGE__;
288 my $t0 = [gettimeofday];
290 dbg("$pkg::spawn_cmd run: $line") if isdbg('cron');
291 my $fc = DXSubprocess->new;
294 $chan->{_nospawn} = 1;
295 my @res = $chan->run_cmd($line);
296 delete $chan->{_nospawn};
300 my ($fc, $err, @res) = @_;
303 my $s = "$pkg::spawn_cmd: error $err";
310 } elsif (isdbg('cron')) {
311 dbg("$pkg::spawn_cmd: $_");
316 diffms($pkg, "::spawn_cmd", $line, $t0, scalar @res) if isdbg('progress');
321 # do an rcmd to another cluster from the crontab
327 # can we see it? Is it a node?
328 my $noderef = Route::Node::get($call);
329 return unless $noderef && $noderef->version;
332 DXProt::addrcmd($main::me, $call, $line);
338 my @in = $main::me->run_cmd($line);
339 dbg("DXCron::run_cmd: $line") if isdbg('cron');
342 dbg("DXCron::cmd out: $_") if isdbg('cron');