From e862306597f7a9859a126b4406f64c421759a8f8 Mon Sep 17 00:00:00 2001 From: Guillaume Bougard Date: Thu, 23 Jan 2025 16:04:53 +0100 Subject: [PATCH] fix: Manage to reschedule target tasks after they had been all manually triggered via httpd interface --- lib/GLPI/Agent/Daemon.pm | 14 ++++++++++++++ lib/GLPI/Agent/Event.pm | 6 +++++- lib/GLPI/Agent/Target.pm | 9 ++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/GLPI/Agent/Daemon.pm b/lib/GLPI/Agent/Daemon.pm index fa6a33951..2af59d0fd 100644 --- a/lib/GLPI/Agent/Daemon.pm +++ b/lib/GLPI/Agent/Daemon.pm @@ -164,6 +164,20 @@ sub run { # Leave immediately if we passed in terminate method last if $self->{_terminate}; + # Reschedule if required + if ($event->taskrun && $event->get('reschedule')) { + # First set rundate to now, than reset next run date + $target->setNextRunDateFromNow(); + $target->resetNextRunDate(); + + if ($logger) { + my $date = $target->getFormatedNextRunDate(); + my $id = $target->id(); + my $name = $target->getName(); + $logger->info("target $id: next run: $date - $name"); + } + } + # We should run service optimization after all targets can be run $self->{_run_optimization} = scalar($self->getTargets()); diff --git a/lib/GLPI/Agent/Event.pm b/lib/GLPI/Agent/Event.pm index 9034ba4c9..68d4491a9 100644 --- a/lib/GLPI/Agent/Event.pm +++ b/lib/GLPI/Agent/Event.pm @@ -65,11 +65,15 @@ sub new { } elsif ($params{taskrun} && $params{taskrun} =~ /^yes|1$/i) { # Run task request eventually following a runnow event $self = { - _taskrun => 1, + _taskrun => 1, _name => "run", _task => $params{task} // '', _delay => $params{delay} // 0, _httpd => 1, + _params => { + # Support parameter to reset target next rundate on task run + reschedule => $params{reschedule} // 0, + } }; # Store any other supported params if ($params{task} && $params{task} eq "inventory") { diff --git a/lib/GLPI/Agent/Target.pm b/lib/GLPI/Agent/Target.pm index c003b282f..615fada99 100644 --- a/lib/GLPI/Agent/Target.pm +++ b/lib/GLPI/Agent/Target.pm @@ -182,8 +182,11 @@ sub triggerRunTasksNow { my %plannedTasks = map { lc($_) => 1 } @{$self->{tasks}}; my $task = $event->task; - my @tasks = $task && $task eq "all" ? @{$self->{tasks}} : split(/,+/, $task); + my $all = $task && $task eq "all" ? 1 : 0; + my @tasks = $all ? @{$self->{tasks}} : split(/,+/, $task); + my $reschedule_index = $all ? scalar(@tasks) : 0; foreach my $runtask (map { lc($_) } @tasks) { + $reschedule_index--; next unless $plannedTasks{$runtask}; my %event = ( @@ -193,6 +196,10 @@ sub triggerRunTasksNow { delay => 0, ); + # permit to reschedule on last task run + $event{reschedule} = 1 + if $all && $reschedule_index == 0; + # Add any supported params if ($runtask eq "inventory") { my $full = $event->get("full");