Convert time module to webmincron
authorJamie Cameron <jcameron@webmin.com>
Sat, 29 May 2010 21:42:05 +0000 (14:42 -0700)
committerJamie Cameron <jcameron@webmin.com>
Sat, 29 May 2010 21:42:05 +0000 (14:42 -0700)
miniserv.pl
time/postinstall.pl

index a307be7..44efa6f 100755 (executable)
@@ -4840,8 +4840,17 @@ foreach my $cron (@webmincrons) {
                $run = 1;
                }
        elsif ($cron->{'mins'}) {
-               # Check if current time matches spec
+               # Check if current time matches spec, and we haven't run in the
+               # last minute
                my @tm = localtime($now);
+               if (&matches_cron($cron->{'mins'}, $tm[1]) &&
+                   &matches_cron($cron->{'hours'}, $tm[2]) &&
+                   &matches_cron($cron->{'days'}, $tm[3]) &&
+                   &matches_cron($cron->{'months'}, $tm[4]+1) &&
+                   &matches_cron($cron->{'weekdays'}, $tm[6]) &&
+                   $now - $webmincron_last{$cron->{'id'}} > 60) {
+                       $run = 1;
+                       }
                }
 
        if ($run) {
@@ -4926,6 +4935,26 @@ if ($changed) {
        }
 }
 
+# matches_cron(cron-spec, time)
+# Checks if some minute or hour matches some cron spec, which can be * or a list
+# of numbers.
+sub matches_cron
+{
+my ($spec, $tm) = @_;
+if ($spec eq '*') {
+       return 1;
+       }
+else {
+       foreach my $s (split(/,/, $spec)) {
+               if ($s == $tm ||
+                   $s =~ /^(\d+)\-(\d+)$/ && $tm >= $1 && $tm <= $2) {
+                       return 1;
+                       }
+               }
+       return 0;
+       }
+}
+
 # read_webmin_crons()
 # Read all scheduled webmin cron functions and store them in the @webmincrons
 # global list
@@ -4991,7 +5020,7 @@ foreach my $f (readdir(CRONS)) {
                        $cron{'months'} = '1';
                        $cron{'weekdays'} = '*';
                        }
-               else {
+               elsif ($cron{'special'}) {
                        print STDERR "Cron $1 invalid special time $cron{'special'}\n";
                        $broken = 1;
                        }
index 9877b92..fd96afa 100755 (executable)
@@ -1,18 +1,23 @@
 
 require 'time-lib.pl';
 
-# Change time sync jobs running at midnight to a random time, to stop
-# overloading public NTP servers
+# Convert existing cron job to webmin cron
 sub module_install
 {
 &foreign_require("cron", "cron-lib.pl");
 local $job = &find_cron_job();
-if ($job && $job->{'mins'} eq '0' && $job->{'hours'} eq '0') {
-       # Midnight .. fix it
-       &seed_random();
-       $job->{'mins'} = int(rand()*60);
-       $job->{'hours'} = int(rand()*24);
-       &cron::change_cron_job($job);
+if ($job) {
+       &foreign_require("webmincron");
+       $wcron = { 'module' => $module_name,
+                  'func' => 'sync_time_cron',
+                  'special' => $job->{'special'},
+                  'mins' => $job->{'mins'},
+                  'hours' => $job->{'hours'},
+                  'days' => $job->{'days'},
+                  'months' => $job->{'months'},
+                  'weekdays' => $job->{'weekdays'},
+               };
+       &webmincron::create_webmin_cron($wcron, $job->{'command'});
        }
 }