Webmin cron fixes
authorJamie Cameron <jcameron@webmin.com>
Tue, 18 May 2010 06:56:58 +0000 (23:56 -0700)
committerJamie Cameron <jcameron@webmin.com>
Tue, 18 May 2010 06:56:58 +0000 (23:56 -0700)
miniserv.pl
system-status/system-status-lib.pl
webmincron/webmincron-lib.pl

index 7622968..8da3d7b 100755 (executable)
@@ -4833,7 +4833,7 @@ foreach my $cron (@webmincrons) {
                $webmincron_last{$cron->{'id'}} = $now;
                $changed = 1;
                }
-       elsif ($webmincron_last{$cron->{'id'}} < $now - $cron{'interval'}) {
+       elsif ($now - $webmincron_last{$cron->{'id'}} > $cron->{'interval'}) {
                # Older than interval .. time to run
                print DEBUG "Running cron id=$cron->{'id'} ".
                            "module=$cron->{'module'} func=$cron->{'func'}\n";
index 737f627..b4b110a 100755 (executable)
@@ -348,50 +348,18 @@ return @rv;
 }
 
 # setup_collectinfo_job()
-# Creates or updates the systeminfo.pl cron job, based on the schedule
-# set in the module config.
+# Creates or updates the Webmin function cron job, based on the interval
+# set in the module config
 sub setup_collectinfo_job
 {
-&foreign_require("cron");
-
-# Work out correct steps
+&foreign_require("webmincron");
 my $step = $config{'collect_interval'};
 $step = 5 if (!$step || $step eq 'none');
-my $offset = int(rand()*$step);
-my @mins;
-for(my $i=$offset; $i<60; $i+= $step) {
-       push(@mins, $i);
-       }
-my $job = &cron::find_cron_job($systeminfo_cron_cmd);
-
-if (!$job && $config{'collect_interval'} ne 'none') {
-       # Create, and run for the first time
-       $job = { 'mins' => join(',', @mins),
-                'hours' => '*',
-                'days' => '*',
-                'months' => '*',
-                'weekdays' => '*',
-                'user' => 'root',
-                'active' => 1,
-                'command' => $systeminfo_cron_cmd };
-       &cron::create_cron_job($job);
-       }
-elsif ($job && $config{'collect_interval'} ne 'none') {
-       # Update existing job, if step has changed
-       my @oldmins = split(/,/, $job->{'mins'});
-       my $oldstep = $oldmins[0] eq '*' ? 1 :
-                        @oldmins == 1 ? 60 :
-                        $oldmins[1]-$oldmins[0];
-       if ($step != $oldstep) {
-               $job->{'mins'} = join(',', @mins);
-               &cron::change_cron_job($job);
-               }
-       }
-elsif ($job && $config{'collect_interval'} eq 'none') {
-       # No longer wanted, so delete
-       &cron::delete_cron_job($job);
-       }
-&cron::create_wrapper($systeminfo_cron_cmd, $module_name, "systeminfo.pl");
+my $cron = { 'module' => $module_name,
+            'func' => 'scheduled_collect_system_info',
+            'interval' => $step * 60,
+          };
+&webmincron::create_webmin_cron($cron, $systeminfo_cron_cmd);
 }
 
 # get_current_drive_temps()
index 403edf4..cbfff48 100644 (file)
@@ -8,6 +8,7 @@ Functions for creating and listing Webmin scheduled functions.
 # XXX support cron-style time specs
 # XXX make sure temp files are cleaned up
 # XXX switch all cron jobs at install time
+# XXX delete jobs when un-installing modules
 
 BEGIN { push(@INC, ".."); };
 use WebminCore;
@@ -81,11 +82,48 @@ my $file = "$webmin_crons_directory/$cron->{'id'}.cron";
 &unlock_file($file);
 }
 
-=head2 create_webmin_cron(module, function, interval, &args)
+=head2 create_webmin_cron(&cron, [old-cron-command])
+
+Create or update a webmin cron job that calls some function.
+If the old-cron parameter is given, find and replace the regular cron job
+of that name.
 
 =cut
 sub create_webmin_cron
 {
+my ($cron, $old_cmd) = @_;
+
+# Find and replace existing cron with same module, function and args
+my @crons = &list_webmin_crons();
+my $already;
+foreach my $oc (@crons) {
+       next if ($oc->{'module'} ne $cron->{'module'});
+       next if ($oc->{'func'} ne $cron->{'func'});
+       for(my $i=0; defined($oc->{'arg'.$i}) ||
+                    defined($cron->{'arg'.$i}); $i++) { }
+       $already = $c;
+       last;
+       }
+if ($already) {
+       # Update existing, possibly with new interval
+       $cron->{'id'} = $already->{'id'};
+       }
+&save_webmin_cron($cron);
+
+# Find and delete any Unix cron job that this is replacing
+if ($old_cmd && &foreign_installed("cron")) {
+       &foreign_require("cron");
+       my @jobs = &cron::list_cron_jobs();
+       my ($job) = grep {
+            $_->{'user'} eq 'root' &&
+            $_->{'command'} =~ /(^|[ \|\&;\/])\Q$old_cmd\E($|[ \|\&><;])/
+            } @jobs;
+       if ($job) {
+               &lock_file(&cron::cron_file($job));
+               &cron::delete_cron_job($job);
+               &unlock_file(&cron::cron_file($job));
+               }
+       }
 }
 
 1;