1337919790b910096944a800333339af2ce994a6
[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 Mojo::IOLoop::Subprocess;
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                 $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");
81                 if (!$err) {
82                         $ref->{cmd} = $cmd;
83                         push @out, $ref;
84                         dbg("DXCron::cread: adding $_\n") if isdbg('cron');
85                 } else {
86                         $err =~ s/^, //;
87                         dbg("DXCron::cread: error $err on line $line '$_'\n") if isdbg('cron');
88                 }
89         }
90         close($fh);
91         return @out;
92 }
93
94 sub parse
95 {
96         my $ref = shift;
97         my $sort = shift;
98         my $val = shift;
99         my $low = shift;
100         my $high = shift;
101         my @req;
102
103         # handle '*' values
104         if ($val eq '*') {
105                 $ref->{$sort} = 0;
106                 return;
107         }
108
109         # handle comma delimited values
110         my @comma = split /,/o, $val;
111         for (@comma) {
112                 my @minus = split /-/o;
113                 if (@minus == 2) {
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;
116                         my $i;
117                         for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
118                                 push @req, 0 + $i; 
119                         }
120                 } else {
121                         return ", $sort should be $low >= $val <= $high" if $_ < $low || $_ > $high;
122                         push @req, 0 + $_;
123                 }
124         }
125         $ref->{$sort} = \@req;
126         
127         return;
128 }
129
130 # process the cronjobs
131 sub process
132 {
133         my $now = $main::systime;
134         return if $now-$lasttime < 1;
135         
136         my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
137
138         # are we at a minute boundary?
139         if ($min != $lastmin) {
140                 
141                 # read in any changes if the modification time has changed
142                 init();
143
144                 $mon += 1;       # months otherwise go 0-11
145                 my $cron;
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}})  ){
152                                 
153                                 if ($cron->{cmd}) {
154                                         dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
155                                         eval "$cron->{cmd}";
156                                         dbg("cron: cmd error $@") if $@ && isdbg('cron');
157                                 }
158                         }
159                 }
160         }
161
162         # remember when we are now
163         $lasttime = $now;
164         $lastmin = $min;
165 }
166
167
168 # these are simple stub functions to make connecting easy in DXCron contexts
169 #
170
171 # is it locally connected?
172 sub connected
173 {
174         my $call = uc shift;
175         return DXChannel::get($call);
176 }
177
178 # is it remotely connected anywhere (with exact callsign)?
179 sub present
180 {
181         my $call = uc shift;
182         return Route::get($call);
183 }
184
185 # is it remotely connected anywhere (ignoring SSIDS)?
186 sub presentish
187 {
188         my $call = uc shift;
189         my $c = Route::get($call);
190         unless ($c) {
191                 for (1..15) {
192                         $c = Route::get("$call-$_");
193                         last if $c;
194                 }
195         }
196         return $c;
197 }
198
199 # is it remotely connected anywhere (with exact callsign) and on node?
200 sub present_on
201 {
202         my $call = uc shift;
203         my $ncall = uc shift;
204         my $node = Route::Node::get($ncall);
205         return ($node) ? grep $call eq $_, $node->users : undef;
206 }
207
208 # is it remotely connected (ignoring SSIDS) and on node?
209 sub presentish_on
210 {
211         my $call = uc shift;
212         my $ncall = uc shift;
213         my $node = Route::Node::get($ncall);
214         my $present;
215         if ($node) {
216                 $present = grep {/^$call/ } $node->users;
217         }
218         return $present;
219 }
220
221 # last time this thing was connected
222 sub last_connect
223 {
224         my $call = uc shift;
225         return $main::systime if DXChannel::get($call);
226         my $user = DXUser::get($call);
227         return $user ? $user->lastin : 0;
228 }
229
230 # disconnect a locally connected thing
231 sub disconnect
232 {
233         my $call =  shift;
234         run_cmd("disconnect $call");
235 }
236
237 # start a connect process off
238 sub start_connect
239 {
240         my $call = shift;
241         # connecting is now done in one place - Yeah!
242         run_cmd("connect $call");
243 }
244
245 # spawn any old job off
246 sub spawn
247 {
248         my $line = shift;
249         my $t0 = [gettimeofday];
250
251         dbg("DXCron::spawn: $line") if isdbg("cron");
252         my $fc = Mojo::IOLoop::Subprocess->new();
253         $fc->run(
254                          sub {
255                                  my @res = `$line`;
256 #                                diffms("DXCron spawn 1", $line, $t0, scalar @res) if isdbg('chan');
257                                  return @res
258                          },
259                          sub {
260                                  my ($fc, $err, @res) = @_; 
261                                  if ($err) {
262                                          my $s = "DXCron::spawn: error $err";
263                                          dbg($s);
264                                          return;
265                                  }
266                                  for (@res) {
267                                          chomp;
268                                          dbg("DXCron::spawn: $_") if isdbg("cron");
269                                  }
270                                  diffms("by DXCron::spawn", $line, $t0, scalar @res) if isdbg('progress');
271                          }
272                         );
273 }
274
275 sub spawn_cmd
276 {
277         my $line = shift;
278         my $t0 = [gettimeofday];
279
280         dbg("DXCron::spawn_cmd run: $line") if isdbg('cron');
281         my $fc = Mojo::IOLoop::Subprocess->new();
282         $fc->run(
283                          sub {
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');
288                                  return @res;
289                          },
290                          sub {
291                                  my ($fc, $err, @res) = @_; 
292                                  if ($err) {
293                                          my $s = "DXCron::spawn_cmd: error $err";
294                                          dbg($s);
295                                  }
296                                  for (@res) {
297                                          chomp;
298                                          dbg("DXCron::spawn_cmd: $_") if isdbg("cron");
299                                  }
300                                  diffms("by DXCron::spawn_cmd", $line, $t0, scalar @res) if isdbg('progress');
301                          }
302                         );
303 }
304
305 # do an rcmd to another cluster from the crontab
306 sub rcmd
307 {
308         my $call = uc shift;
309         my $line = shift;
310
311         # can we see it? Is it a node?
312         my $noderef = Route::Node::get($call);
313         return  unless $noderef && $noderef->version;
314
315         # send it 
316         DXProt::addrcmd($main::me, $call, $line);
317 }
318
319 sub run_cmd
320 {
321         my $line = shift;
322         my @in = $main::me->run_cmd($line);
323         dbg("DXCron::run_cmd: $line") if isdbg('cron');
324         for (@in) {
325                 s/\s*$//og;
326                 dbg("DXCron::cmd out: $_") if isdbg('cron');
327         }
328 }
329
330 1;
331 __END__