2 # module to timed tasks
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
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("cron: 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("cron: adding $_\n") if isdbg('cron');
86 dbg("cron: error on line $line '$_'\n") if isdbg('cron');
108 # handle comma delimited values
109 my @comma = split /,/o, $val;
111 my @minus = split /-/o;
113 return 1 if $minus[0] < $low || $minus[0] > $high;
114 return 1 if $minus[1] < $low || $minus[1] > $high;
116 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
120 return 1 if $_ < $low || $_ > $high;
124 $ref->{$sort} = \@req;
129 # process the cronjobs
132 my $now = $main::systime;
133 return if $now-$lasttime < 1;
135 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
137 # are we at a minute boundary?
138 if ($min != $lastmin) {
140 # read in any changes if the modification time has changed
143 $mon += 1; # months otherwise go 0-11
145 foreach $cron (@crontab) {
146 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
147 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
148 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
149 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
150 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
153 dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
155 dbg("cron: cmd error $@") if $@ && isdbg('cron');
161 # remember when we are now
167 # these are simple stub functions to make connecting easy in DXCron contexts
170 # is it locally connected?
174 return DXChannel::get($call);
177 # is it remotely connected anywhere (with exact callsign)?
181 return Route::get($call);
184 # is it remotely connected anywhere (ignoring SSIDS)?
188 my $c = Route::get($call);
191 $c = Route::get("$call-$_");
198 # is it remotely connected anywhere (with exact callsign) and on node?
202 my $ncall = uc shift;
203 my $node = Route::Node::get($ncall);
204 return ($node) ? grep $call eq $_, $node->users : undef;
207 # is it remotely connected (ignoring SSIDS) and on node?
211 my $ncall = uc shift;
212 my $node = Route::Node::get($ncall);
215 $present = grep {/^$call/ } $node->users;
220 # last time this thing was connected
224 return $main::systime if DXChannel::get($call);
225 my $user = DXUser::get($call);
226 return $user ? $user->lastin : 0;
229 # disconnect a locally connected thing
233 run_cmd("disconnect $call");
236 # start a connect process off
240 # connecting is now done in one place - Yeah!
241 run_cmd("connect $call");
244 # spawn any old job off
249 my $fc = Mojo::IOLoop::Subprocess->new();
251 sub {my @res = `$line`; return @res},
253 my ($fc, $err, @res) = @_;
255 my $s = "DXCron::spawn: error $err";
261 dbg("DXCron::spawn: $_") if isdbg("cron");
271 dbg("spawn_cmd run: $line") if isdbg('cron');
272 my $fc = Mojo::IOLoop::Subprocess->new();
275 $main::me->{_nospawn} = 1;
276 my @res = $main::me->run_cmd($line);
277 delete $main::me->{_nospawn};
281 my ($fc, $err, @res) = @_;
283 my $s = "spawn_cmd: error $err";
288 dbg("spawn_cmd: $_") if isdbg("cron");
294 # do an rcmd to another cluster from the crontab
300 # can we see it? Is it a node?
301 my $noderef = Route::Node::get($call);
302 return unless $noderef && $noderef->version;
305 DXProt::addrcmd($main::me, $call, $line);
311 my @in = $main::me->run_cmd($line);
312 dbg("cmd run: $line") if isdbg('cron');
315 dbg("cmd out: $_") if isdbg('cron');