Skip to content

Commit c8fe5b4

Browse files
committed
Emit restart events when a job is enqueued
Use the `auto_duplicate` to emit an event as it is invoked by other functions which try to handle individual job restarts. That should make sure that the event is send to AMQP for jobs with `RETRY` once it is actually triggered. issue: https://progress.opensuse.org/issues/190557 Signed-off-by: Ioannis Bonatakis <[email protected]>
1 parent ad78558 commit c8fe5b4

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/OpenQA/Schema/Result/Jobs.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use OpenQA::Utils (
1616
use OpenQA::App;
1717
use OpenQA::Jobs::Constants;
1818
use OpenQA::JobDependencies::Constants;
19+
use OpenQA::Events;
1920
use OpenQA::Markdown 'markdown_to_html';
2021
use OpenQA::Setup;
2122
use OpenQA::ScreenshotDeletion;
@@ -1001,6 +1002,7 @@ sub auto_duplicate ($self, $args = {}) {
10011002
$dup->{cluster_cloned} = {map { $_ => $clones->{$_}->{clone} } keys %$clones};
10021003
$dup->{comments_created} = $args->{comments};
10031004
log_debug("Job $job_id duplicated as $clone_id");
1005+
OpenQA::Events->singleton->emit_event('job_restart', data => {id => $clone_id});
10041006
return $dup;
10051007
}
10061008

t/10-jobs.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use OpenQA::Jobs::Constants;
1919
use OpenQA::Test::Case;
2020
use Test::MockModule 'strict';
2121
use Test::Mojo;
22+
use Test::Output;
2223
use Test::Warnings qw(:report_warnings warning);
2324
use Mojo::File 'path';
2425
use Mojo::JSON qw(decode_json encode_json);
@@ -899,7 +900,8 @@ subtest 'job setting based retriggering' => sub {
899900
my $finalize_job_count_before = @{$get_jobs->('finalize_job_results')};
900901
$job->update({state => SCHEDULED, result => NONE});
901902
$job->done(result => FAILED);
902-
perform_minion_jobs($minion);
903+
stdout_like { perform_minion_jobs($minion) } qr/Job \d+ duplicated as \d+/,
904+
'check debug message from auto_duplicate';
903905
is $jobs->count, $jobs_nr + 2, 'job retriggered as it FAILED (with retry)';
904906
$job->update;
905907
$job->discard_changes;

t/20-stale-job-detection.t

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use Test::Output qw(combined_like stderr_like);
1313
use Test::MockModule;
1414
use OpenQA::App;
1515
use OpenQA::Constants qw(DEFAULT_WORKER_TIMEOUT DB_TIMESTAMP_ACCURACY);
16+
use OpenQA::Events;
1617
use OpenQA::Jobs::Constants;
1718
use OpenQA::WebSockets;
1819
use OpenQA::Scheduler;
@@ -43,9 +44,24 @@ subtest 'worker with job and not updated in last 120s is considered dead' => sub
4344
$workers->update_all({t_seen => undef});
4445
is($jobs->stale_ones->count, 3, 'jobs considered stale if t_seen is not set');
4546

47+
my @emitted_events;
48+
my $mock_events = Test::MockModule->new('OpenQA::Events');
49+
$mock_events->redefine(
50+
emit_event => sub {
51+
my ($self, $type, %args) = @_;
52+
push @emitted_events, {type => $type, %args};
53+
});
54+
4655
stderr_like { OpenQA::Scheduler::Model::Jobs->singleton->incomplete_and_duplicate_stale_jobs }
4756
qr/Dead job 99961 aborted and duplicated 99982\n.*Dead job 99963 aborted as incomplete/, 'dead jobs logged';
4857

58+
is(scalar @emitted_events, 1, 'one event emitted for the duplicated job');
59+
is_deeply(
60+
$emitted_events[0],
61+
{type => 'job_restart', data => {id => 99982}},
62+
'job_restart event with correct type and data'
63+
);
64+
4965
for my $job_id (99961, 99963) {
5066
my $job = $jobs->find(99963);
5167
is($job->state, DONE, "running job $job_id is now done");

0 commit comments

Comments
 (0)