hydra-notify: operate on cached_build_finished events

This commit is contained in:
Graham Christensen 2022-01-10 20:19:28 -05:00
parent 96b2200895
commit 6b7f1da11e
6 changed files with 185 additions and 0 deletions

View file

@ -8,6 +8,12 @@ Notifications are passed from `hydra-queue-runner` to `hydra-notify` through Pos
Note that the notification format is subject to change and should not be considered an API. Integrate with `hydra-notify` instead of listening directly.
### `cached_build_finished`
* **Payload:** Exactly two values, tab separated: The ID of the evaluation which contains the finished build, followed by the ID of the finished build.
* **When:** Issued directly after an evaluation completes, when that evaluation includes this finished build.
* **Delivery Semantics:** At most once per evaluation.
### `build_queued`
* **Payload:** Exactly one value, the ID of the build.

View file

@ -2,6 +2,7 @@ package Hydra::Event;
use strict;
use warnings;
use Hydra::Event::CachedBuildFinished;
use Hydra::Event::BuildFinished;
use Hydra::Event::BuildQueued;
use Hydra::Event::BuildStarted;
@ -12,6 +13,7 @@ my %channels_to_events = (
build_started => \&Hydra::Event::BuildStarted::parse,
step_finished => \&Hydra::Event::StepFinished::parse,
build_finished => \&Hydra::Event::BuildFinished::parse,
cached_build_finished => \&Hydra::Event::CachedBuildFinished::parse,
);

View file

@ -0,0 +1,59 @@
package Hydra::Event::CachedBuildFinished;
use strict;
use warnings;
sub parse :prototype(@) {
if (@_ != 2) {
die "cached_build_finished: payload takes two arguments, but ", scalar(@_), " were given";
}
my @failures = grep(!/^\d+$/, @_);
if (@failures > 0) {
die "cached_build_finished: payload arguments should be integers, but we received the following non-integers:", @failures;
}
my ($evaluation_id, $build_id) = map int, @_;
return Hydra::Event::CachedBuildFinished->new($evaluation_id, $build_id);
}
sub new {
my ($self, $evaluation_id, $build_id) = @_;
return bless {
"evaluation_id" => $evaluation_id,
"build_id" => $build_id,
"evaluation" => undef,
"build" => undef,
}, $self;
}
sub interestedIn {
my ($self, $plugin) = @_;
return int(defined($plugin->can('cachedBuildFinished')));
}
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";
}
if (!defined($self->{"evaluation"})) {
$self->{"evaluation"} = $db->resultset('JobsetEvals')->find($self->{"evaluation_id"})
or die "evaluation $self->{'evaluation_id'} does not exist\n";
}
}
sub execute {
my ($self, $db, $plugin) = @_;
$self->load($db);
$plugin->cachedBuildFinished($self->{"evaluation"}, $self->{"build"});
return 1;
}
1;

View file

@ -36,6 +36,12 @@ sub instantiate {
# my ($self, $build) = @_;
# }
# # Called when build $build is a finished build, and is
# part evaluation $evaluation
# sub cachedBuildFinished {
# my ($self, $evaluation, $build) = @_;
# }
# # Called when build $build has started.
# sub buildStarted {
# my ($self, $build) = @_;

View file

@ -96,6 +96,7 @@ my $listener = Hydra::PostgresListener->new($dbh);
$listener->subscribe("build_finished");
$listener->subscribe("build_queued");
$listener->subscribe("build_started");
$listener->subscribe("cached_build_finished");
$listener->subscribe("hydra_notify_dump_metrics");
$listener->subscribe("step_finished");

View file

@ -0,0 +1,111 @@
use strict;
use warnings;
use Setup;
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
use Hydra::Event;
use Hydra::Event::CachedBuildFinished;
use Test2::V0;
use Test2::Tools::Exception;
use Test2::Tools::Mock qw(mock_obj);
my $db = Hydra::Model::DB->new;
hydra_setup($db);
subtest "Parsing" => sub {
like(
dies { Hydra::Event::parse_payload("cached_build_finished", "") },
qr/takes two arguments/,
"empty payload"
);
like(
dies { Hydra::Event::parse_payload("cached_build_finished", "abc123") },
qr/takes two arguments/,
"missing the build ID"
);
like(
dies { Hydra::Event::parse_payload("cached_build_finished", "123\t456\t789\t012\t345") },
qr/takes two arguments/,
"too many arguments"
);
like(
dies { Hydra::Event::parse_payload("cached_build_finished", "abc123\tdef456") },
qr/should be integers/,
"evaluation ID should be an integer"
);
like(
dies { Hydra::Event::parse_payload("cached_build_finished", "123\tabc123") },
qr/should be integers/,
"build ID should be an integer"
);
is(
Hydra::Event::parse_payload("cached_build_finished", "123\t456"),
Hydra::Event::CachedBuildFinished->new(123, 456),
"one dependent build"
);
};
my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"});
my $jobset = createBaseJobset("basic", "basic.nix", $ctx{jobsdir});
ok(evalSucceeds($jobset), "Evaluating jobs/basic.nix should exit with return code 0");
is(nrQueuedBuildsForJobset($jobset), 3, "Evaluating jobs/basic.nix should result in 3 builds");
subtest "interested" => sub {
my $event = Hydra::Event::CachedBuildFinished->new(123, 456);
subtest "A plugin which does not implement the API" => sub {
my $plugin = {};
my $mock = mock_obj $plugin => ();
is($event->interestedIn($plugin), 0, "The plugin is not interesting.");
};
subtest "A plugin which does implement the API" => sub {
my $plugin = {};
my $mock = mock_obj $plugin => (
add => [
"cachedBuildFinished" => sub {}
]
);
is($event->interestedIn($plugin), 1, "The plugin is interesting.");
};
};
subtest "load" => sub {
my ($build) = $db->resultset('Builds')->search({ }, { limit => 1 })->single;
my $evaluation = $build->jobsetevals->search({}, { limit => 1 })->single;
my $event = Hydra::Event::CachedBuildFinished->new($evaluation->id, $build->id);
$event->load($db);
is($event->{"evaluation"}->id, $evaluation->id, "The evaluation record matches.");
is($event->{"build"}->id, $build->id, "The build record matches.");
# Create a fake "plugin" with a cachedBuildFinished sub, the sub sets this
# global passedEvaluation and passedBuild variables for verifying.
my $passedEvaluation;
my $passedBuild;
my $plugin = {};
my $mock = mock_obj $plugin => (
add => [
"cachedBuildFinished" => sub {
my ($self, $evaluation, $build) = @_;
$passedEvaluation = $evaluation;
$passedBuild = $build;
}
]
);
$event->execute($db, $plugin);
is($passedEvaluation->id, $evaluation->id, "The plugin's cachedBuildFinished hook is called with a matching evaluation");
is($passedBuild->id, $build->id, "The plugin's cachedBuildFinished hook is called with a matching build");
};
done_testing;