Skip to content

Commit

Permalink
Fix handling of job array in enqueue_git_clones
Browse files Browse the repository at this point in the history
The job array should not be emptied because there are other
functions called after that that work with the job ids.

Issue: https://progress.opensuse.org/issues/164898
  • Loading branch information
perlpunk committed Oct 16, 2024
1 parent 7c2b6c3 commit 0f3b1d0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
16 changes: 7 additions & 9 deletions lib/OpenQA/Shared/Plugin/Gru.pm
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,13 @@ sub _find_existing_minion_job ($self, $task, $args, $job_ids) {
$dtf->format_datetime(DateTime->now()->subtract(minutes => 1)),
'git_clone', OpenQA::Schema::Result::GruTasks->encode_json_to_db($args));
$sth->execute(@args);
return unless my $job = $sth->fetchrow_hashref;
return 0 unless my $job = $sth->fetchrow_hashref;
# same task was run less than 1 minute ago and finished, nothing to do
return 1 if $job->{state} eq 'finished';

my $notes = decode_json $job->{notes};
if ($job->{state} eq 'finished') {
# same task was run less than 1 minute ago and finished, nothing to do
@$job_ids = ();
return;
}
$self->_add_jobs_to_gru_task($notes->{gru_id}, $job_ids);
return 1;
}

sub _add_jobs_to_gru_task ($self, $gru_id, $job_ids) {
Expand All @@ -159,7 +158,6 @@ sub _add_jobs_to_gru_task ($self, $gru_id, $job_ids) {
last;
}
}
@$job_ids = ();
}

sub enqueue ($self, $task, $args = [], $options = {}, $jobs = []) {
Expand Down Expand Up @@ -246,8 +244,8 @@ sub enqueue_git_clones ($self, $clones, $job_ids) {
return unless OpenQA::App->singleton->config->{'scm git'}->{git_auto_clone} eq 'yes';
# $clones is a hashref with paths as keys and git urls as values
# $job_id is used to create entries in a related table (gru_dependencies)
$self->_find_existing_minion_job('git_clone', $clones, $job_ids);
$self->enqueue('git_clone', $clones, {priority => 10}, $job_ids) if @$job_ids;
my $found = $self->_find_existing_minion_job('git_clone', $clones, $job_ids);
$self->enqueue('git_clone', $clones, {priority => 10}, $job_ids) unless $found;
}

sub enqueue_and_keep_track {
Expand Down
3 changes: 0 additions & 3 deletions t/14-grutasks-git.t
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ subtest 'enqueue_git_clones' => sub {
$mocked_gru->redefine(enqueue => sub (@) { $enq++; });
my $jobs = [$j[2]->id];
my $result = $t->app->gru->enqueue_git_clones($clones, $jobs);
is_deeply $jobs, [], 'job array was emptied';
my @deps = $task->jobs;
is scalar @deps, 3, 'job was added to GruTask';
is $deps[2]->job_id, $j[2]->id, 'third job was added to existing GruTask';
Expand All @@ -266,7 +265,6 @@ subtest 'enqueue_git_clones' => sub {
$mocked_gru->redefine(enqueue => sub (@) { $enq++; });
my $jobs = [$j[3]->id];
my $result = $t->app->gru->enqueue_git_clones($clones, $jobs);
is_deeply $jobs, [], 'job array was emptied';
my @deps = $task->jobs;
is scalar @deps, 3, 'no job was added to GruTask';
is $enq, 0, 'enqueue was not called';
Expand All @@ -278,7 +276,6 @@ subtest 'enqueue_git_clones' => sub {
stderr_like { $t->app->gru->_add_jobs_to_gru_task(999, $jobs) }
qr{GruTask 999 already gone.*insert or update on table "gru_dependencies" violates foreign key constraint "gru_dependencies_fk_gru_task_id"},
'expected log output if GruTask deleted in between';
is_deeply $jobs, [], 'job array was emptied';
my @deps = $task->jobs;
is scalar @deps, 3, 'no job was added to GruTask';
};
Expand Down

0 comments on commit 0f3b1d0

Please sign in to comment.