}
# 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()
# 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;
&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;