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;
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 $err .= parse($ref, 'min', $min, 0, 60);
77 $err .= parse($ref, 'hour', $hour, 0, 23);
78 $err .= parse($ref, 'mday', $mday, 1, 31);
79 $err .= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
80 $err .= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
84 dbg("DXCron::cread: adding $_\n") if isdbg('cron');
87 dbg("DXCron::cread: error $err on line $line '$_'\n") if isdbg('cron');
109 # handle comma delimited values
110 my @comma = split /,/o, $val;
112 my @minus = split /-/o;
114 return ", $sort should be $low >= $minus[0] <= $high" if $minus[0] < $low || $minus[0] > $high;
115 return ", $sort should be $low >= $minus[1] <= $high" if $minus[1] < $low || $minus[1] > $high;
117 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
121 return ", $sort should be $low >= $val <= $high" if $_ < $low || $_ > $high;
125 $ref->{$sort} = \@req;
130 # process the cronjobs
133 my $now = $main::systime;
134 return if $now-$lasttime < 1;
136 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
138 # are we at a minute boundary?
139 if ($min != $lastmin) {
141 # read in any changes if the modification time has changed
144 $mon += 1; # months otherwise go 0-11
146 foreach $cron (@crontab) {
147 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
148 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
149 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
150 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
151 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
154 dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
156 dbg("cron: cmd error $@") if $@ && isdbg('cron');
162 # remember when we are now
168 # these are simple stub functions to make connecting easy in DXCron contexts
171 # is it locally connected?
175 return DXChannel::get($call);
178 # is it remotely connected anywhere (with exact callsign)?
182 return Route::get($call);
185 # is it remotely connected anywhere (ignoring SSIDS)?
189 my $c = Route::get($call);
192 $c = Route::get("$call-$_");
199 # is it remotely connected anywhere (with exact callsign) and on node?
203 my $ncall = uc shift;
204 my $node = Route::Node::get($ncall);
205 return ($node) ? grep $call eq $_, $node->users : undef;
208 # is it remotely connected (ignoring SSIDS) and on node?
212 my $ncall = uc shift;
213 my $node = Route::Node::get($ncall);
216 $present = grep {/^$call/ } $node->users;
221 # last time this thing was connected
225 return $main::systime if DXChannel::get($call);
226 my $user = DXUser::get($call);
227 return $user ? $user->lastin : 0;
230 # disconnect a locally connected thing
234 run_cmd("disconnect $call");
237 # start a connect process off
241 # connecting is now done in one place - Yeah!
242 run_cmd("connect $call");
245 # spawn any old job off
249 my $t0 = [gettimeofday];
251 dbg("DXCron::spawn: $line") if isdbg("cron");
252 my $fc = Mojo::IOLoop::Subprocess->new();
256 # diffms("DXCron spawn 1", $line, $t0, scalar @res) if isdbg('chan');
260 my ($fc, $err, @res) = @_;
262 my $s = "DXCron::spawn: error $err";
268 dbg("DXCron::spawn: $_") if isdbg("cron");
270 diffms("by DXCron::spawn", $line, $t0, scalar @res) if isdbg('progress');
278 my $t0 = [gettimeofday];
280 dbg("DXCron::spawn_cmd run: $line") if isdbg('cron');
281 my $fc = Mojo::IOLoop::Subprocess->new();
284 $main::me->{_nospawn} = 1;
285 my @res = $main::me->run_cmd($line);
286 delete $main::me->{_nospawn};
287 # diffms("DXCron spawn_cmd 1", $line, $t0, scalar @res) if isdbg('chan');
291 my ($fc, $err, @res) = @_;
293 my $s = "DXCron::spawn_cmd: error $err";
298 dbg("DXCron::spawn_cmd: $_") if isdbg("cron");
300 diffms("by DXCron::spawn_cmd", $line, $t0, scalar @res) if isdbg('progress');
305 # do an rcmd to another cluster from the crontab
311 # can we see it? Is it a node?
312 my $noderef = Route::Node::get($call);
313 return unless $noderef && $noderef->version;
316 DXProt::addrcmd($main::me, $call, $line);
322 my @in = $main::me->run_cmd($line);
323 dbg("DXCron::run_cmd: $line") if isdbg('cron');
326 dbg("DXCron::cmd out: $_") if isdbg('cron');