2 # module to timed tasks
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
20 use vars qw{@crontab @lcrontab @scrontab $mtime $lasttime $lastmin};
27 my $fn = "$main::cmd/crontab";
28 my $localfn = "$main::localcmd/crontab";
30 # cron initialisation / reading in cronjobs
33 if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
36 # first read in the standard one
40 @scrontab = cread($fn);
41 $mtime = $t if !$mtime || $t <= $mtime;
44 # then read in any local ones
48 @lcrontab = cread($localfn);
49 $mtime = $t if $t <= $mtime;
51 @crontab = (@scrontab, @lcrontab);
59 my $fh = new IO::File;
63 dbg("cron: reading $fn\n") if isdbg('cron');
64 open($fh, $fn) or confess("cron: can't open $fn $!");
68 next if /^\s*#/o or /^\s*$/o;
69 my ($min, $hour, $mday, $month, $wday, $cmd) = /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/o;
70 next unless defined $min;
74 $err |= parse($ref, 'min', $min, 0, 60);
75 $err |= parse($ref, 'hour', $hour, 0, 23);
76 $err |= parse($ref, 'mday', $mday, 1, 31);
77 $err |= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
78 $err |= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
82 dbg("cron: adding $_\n") if isdbg('cron');
84 dbg("cron: error on line $line '$_'\n") if isdbg('cron');
106 # handle comma delimited values
107 my @comma = split /,/o, $val;
109 my @minus = split /-/o;
111 return 1 if $minus[0] < $low || $minus[0] > $high;
112 return 1 if $minus[1] < $low || $minus[1] > $high;
114 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
118 return 1 if $_ < $low || $_ > $high;
122 $ref->{$sort} = \@req;
127 # process the cronjobs
130 my $now = $main::systime;
131 return if $now-$lasttime < 1;
133 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
135 # are we at a minute boundary?
136 if ($min != $lastmin) {
138 # read in any changes if the modification time has changed
141 $mon += 1; # months otherwise go 0-11
143 foreach $cron (@crontab) {
144 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
145 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
146 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
147 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
148 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
151 dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
153 dbg("cron: cmd error $@") if $@ && isdbg('cron');
159 # remember when we are now
165 # these are simple stub functions to make connecting easy in DXCron contexts
168 # is it locally connected?
172 return DXChannel::get($call);
175 # is it remotely connected anywhere (with exact callsign)?
179 return Route::get($call);
182 # is it remotely connected anywhere (ignoring SSIDS)?
186 my $c = Route::get($call);
189 $c = Route::get("$call-$_");
196 # is it remotely connected anywhere (with exact callsign) and on node?
200 my $ncall = uc shift;
201 my $node = Route::Node::get($ncall);
202 return ($node) ? grep $call eq $_, $node->users : undef;
205 # is it remotely connected (ignoring SSIDS) and on node?
209 my $ncall = uc shift;
210 my $node = Route::Node::get($ncall);
213 $present = grep {/^$call/ } $node->users;
218 # last time this thing was connected
222 return $main::systime if DXChannel::get($call);
223 my $user = DXUser::get($call);
224 return $user ? $user->lastin : 0;
227 # disconnect a locally connected thing
231 run_cmd("disconnect $call");
234 # start a connect process off
238 # connecting is now done in one place - Yeah!
239 run_cmd("connect $call");
242 # spawn any old job off
247 my $fc = Mojo::IOLoop::ForkCall->new;
249 sub {my @res = `$line`; return @res},
252 my ($fc, $err, @res) = @_;
254 my $s = "DXCron::spawn: error $err";
263 # do an rcmd to another cluster from the crontab
269 # can we see it? Is it a node?
270 my $noderef = Route::Node::get($call);
271 return unless $noderef && $noderef->version;
274 DXProt::addrcmd($main::me, $call, $line);
280 my @in = DXCommandmode::run_cmd($main::me, $line);
281 dbg("cmd run: $line") if isdbg('cron');
284 dbg("cmd out: $_") if isdbg('cron');