12192891de1bba6939b8ade706e1feed6ca97940
[spider.git] / perl / DXCron.pm
1 #
2 # module to timed tasks
3 #
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
5 #
6 #
7 #
8
9 package DXCron;
10
11 use DXVars;
12 use DXUtil;
13 use DXM;
14 use DXDebug;
15 use IO::File;
16 use DXLog;
17 use Time::HiRes qw(gettimeofday tv_interval);
18 use DXSubprocess;
19
20 use strict;
21
22 use vars qw{@crontab @lcrontab @scrontab $mtime $lasttime $lastmin};
23
24 $mtime = 0;
25 $lasttime = 0;
26 $lastmin = 0;
27
28
29 my $fn = "$main::cmd/crontab";
30 my $localfn = "$main::localcmd/crontab";
31
32 # cron initialisation / reading in cronjobs
33 sub init
34 {
35         if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
36                 my $t;
37                 
38                 # first read in the standard one
39                 if (-e $fn) {
40                         $t = -M $fn;
41                         
42                         @scrontab = cread($fn);
43                         $mtime = $t if  !$mtime || $t <= $mtime;
44                 }
45
46                 # then read in any local ones
47                 if (-e $localfn) {
48                         $t = -M $localfn;
49                         
50                         @lcrontab = cread($localfn);
51                         $mtime = $t if $t <= $mtime;
52                 }
53                 @crontab = (@scrontab, @lcrontab);
54         }
55 }
56
57 # read in a cron file
58 sub cread
59 {
60         my $fn = shift;
61         my $fh = new IO::File;
62         my $line = 0;
63         my @out;
64
65         dbg("DXCron::cread reading $fn\n") if isdbg('cron');
66         open($fh, $fn) or confess("cron: can't open $fn $!");
67         while (<$fh>) {
68                 $line++;
69                 chomp;
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;
73                 my $ref = bless {};
74                 my $err = '';
75
76                 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
77                         $err .= parse($ref, 'min', $min, 0, 60);
78                         $err .= parse($ref, 'hour', $hour, 0, 23);
79                         $err .= parse($ref, 'mday', $mday, 1, 31);
80                         $err .= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
81                         $err .= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
82                         if (!$err) {
83                                 $ref->{cmd} = $cmd;
84                                 push @out, $ref;
85                                 dbg("DXCron::cread: adding $_\n") if isdbg('cron');
86                         } else {
87                                 $err =~ s/^, //;
88                                 LogDbg('cron', "DXCron::cread: error $err on line $line '$_'");
89                         }
90                 } else {
91                         LogDbg('cron', "DXCron::cread error on line $line '$_'");
92                         my @s = ($min, $hour, $mday, $month, $wday, $cmd);
93                         my $s = "line $line splits as " . join(', ', (map {defined $_ ? qq{$_} : q{'undef'}} @s));
94                         LogDbg('cron', $s);
95                 }
96                 
97         }
98         close($fh);
99         return @out;
100 }
101
102 sub parse
103 {
104         my $ref = shift;
105         my $sort = shift;
106         my $val = shift;
107         my $low = shift;
108         my $high = shift;
109         my @req;
110
111         # handle '*' values
112         if ($val eq '*') {
113                 $ref->{$sort} = 0;
114                 return;
115         }
116
117         # handle comma delimited values
118         my @comma = split /,/o, $val;
119         for (@comma) {
120                 my @minus = split /-/o;
121                 if (@minus == 2) {
122                         return  ", $sort should be $low >= $minus[0] <= $high" if $minus[0] < $low || $minus[0] > $high;
123                         return  ", $sort should be $low >= $minus[1] <= $high" if $minus[1] < $low || $minus[1] > $high;
124                         my $i;
125                         for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
126                                 push @req, 0 + $i; 
127                         }
128                 } else {
129                         return ", $sort should be $low >= $val <= $high" if $_ < $low || $_ > $high;
130                         push @req, 0 + $_;
131                 }
132         }
133         $ref->{$sort} = \@req;
134         
135         return;
136 }
137
138 # process the cronjobs
139 sub process
140 {
141         my $now = $main::systime;
142         return if $now-$lasttime < 1;
143         
144         my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
145
146         # are we at a minute boundary?
147         if ($min != $lastmin) {
148                 
149                 # read in any changes if the modification time has changed
150                 init();
151
152                 $mon += 1;       # months otherwise go 0-11
153                 my $cron;
154                 foreach $cron (@crontab) {
155                         if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
156                                 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
157                                 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
158                                 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
159                                 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}})  ){
160                                 
161                                 if ($cron->{cmd}) {
162                                         dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
163                                         eval "$cron->{cmd}";
164                                         dbg("cron: cmd error $@") if $@ && isdbg('cron');
165                                 }
166                         }
167                 }
168         }
169
170         # remember when we are now
171         $lasttime = $now;
172         $lastmin = $min;
173 }
174
175
176 # these are simple stub functions to make connecting easy in DXCron contexts
177 #
178
179 # is it locally connected?
180 sub connected
181 {
182         my $call = uc shift;
183         return DXChannel::get($call);
184 }
185
186 # is it remotely connected anywhere (with exact callsign)?
187 sub present
188 {
189         my $call = uc shift;
190         return Route::get($call);
191 }
192
193 # is it remotely connected anywhere (ignoring SSIDS)?
194 sub presentish
195 {
196         my $call = uc shift;
197         my $c = Route::get($call);
198         unless ($c) {
199                 for (1..15) {
200                         $c = Route::get("$call-$_");
201                         last if $c;
202                 }
203         }
204         return $c;
205 }
206
207 # is it remotely connected anywhere (with exact callsign) and on node?
208 sub present_on
209 {
210         my $call = uc shift;
211         my $ncall = uc shift;
212         my $node = Route::Node::get($ncall);
213         return ($node) ? grep $call eq $_, $node->users : undef;
214 }
215
216 # is it remotely connected (ignoring SSIDS) and on node?
217 sub presentish_on
218 {
219         my $call = uc shift;
220         my $ncall = uc shift;
221         my $node = Route::Node::get($ncall);
222         my $present;
223         if ($node) {
224                 $present = grep {/^$call/ } $node->users;
225         }
226         return $present;
227 }
228
229 # last time this thing was connected
230 sub last_connect
231 {
232         my $call = uc shift;
233         return $main::systime if DXChannel::get($call);
234         my $user = DXUser::get($call);
235         return $user ? $user->lastin : 0;
236 }
237
238 # disconnect a locally connected thing
239 sub disconnect
240 {
241         my $call =  shift;
242         run_cmd("disconnect $call");
243 }
244
245 # start a connect process off
246 sub start_connect
247 {
248         my $call = shift;
249         # connecting is now done in one place - Yeah!
250         run_cmd("connect $call");
251 }
252
253 # spawn any old job off
254 sub spawn
255 {
256         my $line = shift;
257         my $t0 = [gettimeofday];
258
259         dbg("DXCron::spawn: $line") if isdbg("cron");
260         my $fc = DXSubprocess->new();
261         $fc->run(
262                          sub {
263                                  my @res = `$line`;
264 #                                diffms("DXCron spawn 1", $line, $t0, scalar @res) if isdbg('chan');
265                                  return @res
266                          },
267                          sub {
268                                  my ($fc, $err, @res) = @_; 
269                                  if ($err) {
270                                          my $s = "DXCron::spawn: error $err";
271                                          dbg($s);
272                                          return;
273                                  }
274                                  for (@res) {
275                                          chomp;
276                                          dbg("DXCron::spawn: $_") if isdbg("cron");
277                                  }
278                                  diffms("by DXCron::spawn", $line, $t0, scalar @res) if isdbg('progress');
279                          }
280                         );
281 }
282
283 sub spawn_cmd
284 {
285         my $line = shift;
286         my $t0 = [gettimeofday];
287
288         dbg("DXCron::spawn_cmd run: $line") if isdbg('cron');
289         my $fc = DXSubprocess->new();
290         $fc->run(
291                          sub {
292                                  $main::me->{_nospawn} = 1;
293                                  my @res = $main::me->run_cmd($line);
294                                  delete $main::me->{_nospawn};
295 #                                diffms("DXCron spawn_cmd 1", $line, $t0, scalar @res) if isdbg('chan');
296                                  return @res;
297                          },
298                          sub {
299                                  my ($fc, $err, @res) = @_; 
300                                  if ($err) {
301                                          my $s = "DXCron::spawn_cmd: error $err";
302                                          dbg($s);
303                                  }
304                                  for (@res) {
305                                          chomp;
306                                          dbg("DXCron::spawn_cmd: $_") if isdbg("cron");
307                                  }
308                                  diffms("by DXCron::spawn_cmd", $line, $t0, scalar @res) if isdbg('progress');
309                          }
310                         );
311 }
312
313 # do an rcmd to another cluster from the crontab
314 sub rcmd
315 {
316         my $call = uc shift;
317         my $line = shift;
318
319         # can we see it? Is it a node?
320         my $noderef = Route::Node::get($call);
321         return  unless $noderef && $noderef->version;
322
323         # send it 
324         DXProt::addrcmd($main::me, $call, $line);
325 }
326
327 sub run_cmd
328 {
329         my $line = shift;
330         my @in = $main::me->run_cmd($line);
331         dbg("DXCron::run_cmd: $line") if isdbg('cron');
332         for (@in) {
333                 s/\s*$//og;
334                 dbg("DXCron::cmd out: $_") if isdbg('cron');
335         }
336 }
337
338 1;
339 __END__