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