hydra-notify: listen for build_queued events

This commit is contained in:
Graham Christensen 2021-12-20 13:18:32 -05:00
parent eb5f6c8490
commit d195e545f5
4 changed files with 119 additions and 0 deletions

View file

@ -3,10 +3,12 @@ package Hydra::Event;
use strict; use strict;
use warnings; use warnings;
use Hydra::Event::BuildFinished; use Hydra::Event::BuildFinished;
use Hydra::Event::BuildQueued;
use Hydra::Event::BuildStarted; use Hydra::Event::BuildStarted;
use Hydra::Event::StepFinished; use Hydra::Event::StepFinished;
my %channels_to_events = ( my %channels_to_events = (
build_queued => \&Hydra::Event::BuildQueued::parse,
build_started => \&Hydra::Event::BuildStarted::parse, build_started => \&Hydra::Event::BuildStarted::parse,
step_finished => \&Hydra::Event::StepFinished::parse, step_finished => \&Hydra::Event::StepFinished::parse,
build_finished => \&Hydra::Event::BuildFinished::parse, build_finished => \&Hydra::Event::BuildFinished::parse,

View file

@ -0,0 +1,47 @@
package Hydra::Event::BuildQueued;
use strict;
use warnings;
sub parse :prototype(@) {
unless (@_ == 1) {
die "build_queued: payload takes only one argument, but ", scalar(@_), " were given";
}
my ($build_id) = @_;
unless ($build_id =~ /^\d+$/) {
die "build_queued: payload argument should be an integer, but '", $build_id, "' was given"
}
return Hydra::Event::BuildQueued->new(int($build_id));
}
sub new {
my ($self, $id) = @_;
return bless {
"build_id" => $id,
"build" => undef
}, $self;
}
sub load {
my ($self, $db) = @_;
if (!defined($self->{"build"})) {
$self->{"build"} = $db->resultset('Builds')->find($self->{"build_id"})
or die "build $self->{'build_id'} does not exist\n";
}
}
sub execute {
my ($self, $db, $plugin) = @_;
$self->load($db);
$plugin->buildQueued($self->{"build"});
return 1;
}
1;

View file

@ -93,6 +93,7 @@ my $task_dispatcher = Hydra::TaskDispatcher->new(
my $dbh = $db->storage->dbh; my $dbh = $db->storage->dbh;
my $listener = Hydra::PostgresListener->new($dbh); my $listener = Hydra::PostgresListener->new($dbh);
$listener->subscribe("build_queued");
$listener->subscribe("build_started"); $listener->subscribe("build_started");
$listener->subscribe("build_finished"); $listener->subscribe("build_finished");
$listener->subscribe("step_finished"); $listener->subscribe("step_finished");

69
t/Event/BuildQueued.t Normal file
View file

@ -0,0 +1,69 @@
use strict;
use warnings;
use Setup;
use Hydra::Event;
use Hydra::Event::BuildQueued;
use Test2::V0;
use Test2::Tools::Exception;
use Test2::Tools::Mock qw(mock_obj);
my $ctx = test_context();
my $db = $ctx->db();
my $builds = $ctx->makeAndEvaluateJobset(
expression => "basic.nix"
);
subtest "Parsing build_queued" => sub {
like(
dies { Hydra::Event::parse_payload("build_queued", "") },
qr/one argument/,
"empty payload"
);
like(
dies { Hydra::Event::parse_payload("build_queued", "abc123\tabc123") },
qr/only one argument/,
"two arguments"
);
like(
dies { Hydra::Event::parse_payload("build_queued", "abc123") },
qr/should be an integer/,
"not an integer"
);
is(
Hydra::Event::parse_payload("build_queued", "19"),
Hydra::Event::BuildQueued->new(19),
"Valid parse"
);
};
subtest "load" => sub {
my $build = $builds->{"empty_dir"};
my $event = Hydra::Event::BuildQueued->new($build->id);
$event->load($db);
is($event->{"build"}->id, $build->id, "The build record matches.");
# Create a fake "plugin" with a buildQueued sub, the sub sets this
# global passedBuild variable.
my $passedBuild;
my $plugin = {};
my $mock = mock_obj $plugin => (
add => [
"buildQueued" => sub {
my ($self, $build) = @_;
$passedBuild = $build;
}
]
);
$event->execute($db, $plugin);
is($passedBuild->id, $build->id, "The plugin's buildQueued hook is called with the proper build");
};
done_testing;