Skip to content

Commit

Permalink
fix: Fix support of jobs managed by ToolBox
Browse files Browse the repository at this point in the history
  • Loading branch information
g-bougard committed Dec 17, 2024
1 parent 26c880b commit 21f9863
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion lib/GLPI/Agent/Daemon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,19 @@ 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;

# 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) {
Expand Down
51 changes: 26 additions & 25 deletions lib/GLPI/Agent/HTTP/Server/ToolBox/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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})) {
Expand Down
20 changes: 6 additions & 14 deletions lib/GLPI/Agent/Target.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 21f9863

Please sign in to comment.