From 21f986350d67eee9a73374046ff44e48337fa9ea Mon Sep 17 00:00:00 2001 From: Guillaume Bougard Date: Tue, 17 Dec 2024 20:12:35 +0100 Subject: [PATCH] fix: Fix support of jobs managed by ToolBox --- Changes | 3 ++ lib/GLPI/Agent/Daemon.pm | 7 ++- .../Agent/HTTP/Server/ToolBox/Inventory.pm | 51 ++++++++++--------- lib/GLPI/Agent/Target.pm | 20 +++----- 4 files changed, 41 insertions(+), 40 deletions(-) diff --git a/Changes b/Changes index 811c96b51..3d2954817 100644 --- a/Changes +++ b/Changes @@ -22,6 +22,7 @@ core: - Added a "Force an inventory" to only trigger inventory task for all targets - Targets list also show target planned tasks for trusted clients * Refacto: Add Logger api to register a callback permitting to collect journaling events +* Fix ToolBox job events support inventory: * Fix rare windows perl error during drives, ipv6 network or videos inventory @@ -67,6 +68,8 @@ esx: toolbox: * Fix task log reset for enabled tasks +* Fix enabled jobs start up +* Fix event logger support to update counters even when debug is not enabled packaging: * Update Windows MSI packing building process to use: diff --git a/lib/GLPI/Agent/Daemon.pm b/lib/GLPI/Agent/Daemon.pm index 016a80460..fa6a33951 100644 --- a/lib/GLPI/Agent/Daemon.pm +++ b/lib/GLPI/Agent/Daemon.pm @@ -126,6 +126,9 @@ sub run { $self->_reloadConfIfNeeded(); + # Still get next event, as we don't want to handle job events here but in events_cb called from sleep + my $event = $target->nextEvent(); + if ($target->paused()) { undef $responses; @@ -133,7 +136,9 @@ sub run { # Leave immediately if we passed in terminate method last if $self->{_terminate}; - } elsif (my $event = $target->getEvent()) { + } elsif ($event && !$event->job) { + # Always remove event from list + $target->delEvent($event); # Contact server if required and cache responses if ($event->taskrun) { diff --git a/lib/GLPI/Agent/HTTP/Server/ToolBox/Inventory.pm b/lib/GLPI/Agent/HTTP/Server/ToolBox/Inventory.pm index 0f73228a2..1da28f12d 100644 --- a/lib/GLPI/Agent/HTTP/Server/ToolBox/Inventory.pm +++ b/lib/GLPI/Agent/HTTP/Server/ToolBox/Inventory.pm @@ -531,9 +531,10 @@ sub _submit_update { my $rundate = $self->_get_next_run_date($edit, $job, $job->{last_run_date}); $event->rundate($rundate); $job->{next_run_date} = $rundate; - # Re-schedule event + # Re-schedule event if enabled $self->{toolbox}->{target}->delEvent($event); - $self->{toolbox}->{target}->addEvent($event); + $self->{toolbox}->{target}->addEvent($event) + if $self->isyes($job->{enabled}); # Reset edited entry $edit = $newname; @@ -670,7 +671,12 @@ sub _submit_runnow { # We need to reschedule event if enabled if ($self->isyes($job->{enabled})) { - my $event = $self->{toolbox}->{target}->getEvent($name); + my %event = ( + job => 1, + name => $name, + task => $job->{type} eq 'local' ? "inventory" : "netscan", + ); + my $event = GLPI::Agent::Event->new(%event); # To find next run date, we need to reset not_before time by setting it to now/last_run_date my $rundate = $self->_get_next_run_date($name, $job, $job->{last_run_date}); $event->rundate($rundate); @@ -685,32 +691,26 @@ sub _submit_runnow { sub event_logger { my ($self) = @_; - # We always set verbosity higher to debug2 so we can analyse any debug level - # messages. - my $logger = GLPI::Agent::Logger->new( verbosity => 2 ); - - # Hack logger to add ourself as backend so our addMessage callback is always - # called on logging message in any thread - push @{$logger->{backends}}, $self; + my $logger = GLPI::Agent::Logger->new(); - return $logger; -} + # Setup logger with callback to collect logger messages at all level + my $agent = $self->{toolbox}->{server}->{agent}; + my $taskid = $self->{taskid}; + my $messages = $self->{tasks}->{$taskid}->{messages}; -# To use ourself as a logger backend in a multi-threaded process -sub addMessage { - my ($self, %params) = @_; + $logger->register_event_cb(sub { + my (%params) = @_; - return unless $params{level} && $params{message}; + return unless $params{level} && $params{message}; - my $agent = $self->{toolbox}->{server}->{agent}; + if ($agent->forked()) { + $agent->forked_process_event("LOGGER,$taskid,[$params{level}] $params{message}"); + } else { + push @{$messages}, "[$params{level}] $params{message}"; + } + }); - my $taskid = $self->{taskid}; - if ($agent->forked()) { - $agent->forked_process_event("LOGGER,$taskid,[$params{level}] $params{message}"); - } else { - my $messages = $self->{tasks}->{$taskid}->{messages}; - push @{$messages}, "[$params{level}] $params{message}"; - } + return $logger; } sub netscan { @@ -1051,8 +1051,9 @@ sub events_cb { $jobs = $self->yaml(jobs); } # Time to check if we need to run a job - my $event = $self->{toolbox}->{target}->getEvent(); + my $event = $self->{toolbox}->{target}->nextEvent(); return 0 unless $event && $event->job; + $self->{toolbox}->{target}->delEvent($event); my $name = $event->name; my $job = $jobs->{$name}; if ($job && $job->{type} && $self->isyes($job->{enabled})) { diff --git a/lib/GLPI/Agent/Target.pm b/lib/GLPI/Agent/Target.pm index 9ecf7a479..5853f8d0e 100644 --- a/lib/GLPI/Agent/Target.pm +++ b/lib/GLPI/Agent/Target.pm @@ -219,7 +219,7 @@ sub addEvent { my $logprefix = $self->{_logprefix}; # Check for supported events - if ($event->runnow || $event->taskrun) { + if (!$event->job && ($event->runnow || $event->taskrun)) { unless ($event->task) { $logger->debug("$logprefix Not supported ".$event->name." event without task"); return 0; @@ -306,23 +306,15 @@ sub delEvent { if $self->{_next_event}; # Cleanup event list - $self->{_events} = [ grep { $_->name ne $event->name } @{$self->{_events}} ] + $self->{_events} = [ grep { $_->name ne $event->name || (($event->init || $event->maintenance) && $_->task ne $event->task) } @{$self->{_events}} ] } -sub getEvent { - my ($self, $name) = @_; - if ($name) { - my ($event) = grep { $_->name eq $name } @{$self->{_events}}; - return $event; - } - return unless @{$self->{_events}} && time >= $self->{_events}->[0]->rundate; - my $event = shift @{$self->{_events}}; +sub nextEvent { + my ($self) = @_; - # Always accept new event for this name - delete $self->{_next_event}->{$event->name} - if $self->{_next_event}; + return unless @{$self->{_events}} && time >= $self->{_events}->[0]->rundate; - return $event; + return $self->{_events}->[0]; } sub paused {