eval_failed event: change interface to traceID\tjobsetID

I was not going to break the interface until I noticed the other eval_* events used literal \ts
This commit is contained in:
Graham Christensen 2022-02-07 16:14:18 -05:00
parent 2597fa8c11
commit d512e6220f
8 changed files with 171 additions and 5 deletions

View file

@ -75,7 +75,7 @@ It is possible for subsequent deliveries of the same `build_finished` data to im
### `eval_failed`
* **Payload:** Exactly one value: an opaque trace ID representing this evaluation.
* **Payload:** Exactly two values: an opaque trace ID representing this evaluation, and the ID of the jobset.
* **When:** After any fetching any input fails, or any other evaluation error occurs.
* **Delivery Semantics:** Ephemeral. `hydra-notify` must be running to react to this event. No record of this event is stored.

View file

@ -8,6 +8,7 @@ use Hydra::Event::BuildStarted;
use Hydra::Event::CachedBuildFinished;
use Hydra::Event::CachedBuildQueued;
use Hydra::Event::EvalCached;
use Hydra::Event::EvalFailed;
use Hydra::Event::EvalStarted;
use Hydra::Event::StepFinished;
@ -18,6 +19,7 @@ my %channels_to_events = (
cached_build_finished => \&Hydra::Event::CachedBuildFinished::parse,
cached_build_queued => \&Hydra::Event::CachedBuildQueued::parse,
eval_cached => \&Hydra::Event::EvalCached::parse,
eval_failed => \&Hydra::Event::EvalFailed::parse,
eval_started => \&Hydra::Event::EvalStarted::parse,
step_finished => \&Hydra::Event::StepFinished::parse,
);

View file

@ -0,0 +1,53 @@
package Hydra::Event::EvalFailed;
use strict;
use warnings;
sub parse :prototype(@) {
unless (@_ == 2) {
die "eval_failed: payload takes two arguments, but ", scalar(@_), " were given";
}
my ($trace_id, $jobset_id) = @_;
unless ($jobset_id =~ /^\d+$/) {
die "eval_failed: payload argument should be an integer, but '", $jobset_id, "' was given"
}
return Hydra::Event::EvalFailed->new($trace_id, int($jobset_id));
}
sub new {
my ($self, $trace_id, $jobset_id) = @_;
return bless {
"trace_id" => $trace_id,
"jobset_id" => $jobset_id,
"jobset" => undef
}, $self;
}
sub interestedIn {
my ($self, $plugin) = @_;
return int(defined($plugin->can('evalFailed')));
}
sub load {
my ($self, $db) = @_;
if (!defined($self->{"jobset"})) {
$self->{"jobset"} = $db->resultset('Jobsets')->find({ id => $self->{"jobset_id"}})
or die "Jobset $self->{'jobset_id'} does not exist\n";
}
}
sub execute {
my ($self, $db, $plugin) = @_;
$self->load($db);
$plugin->evalFailed($self->{"trace_id"}, $self->{"jobset"});
return 1;
}
1;

View file

@ -42,6 +42,11 @@ sub instantiate {
# my ($self, $traceID, $jobset, $evaluation) = @_;
# }
# # Called when an evaluation of $jobset failed.
# sub evalFailed {
# my ($self, $traceID, $jobset) = @_;
# }
# # Called when build $build has been queued.
# sub buildQueued {
# my ($self, $build) = @_;

View file

@ -654,7 +654,7 @@ sub checkJobsetWrapped {
print STDERR $fetchError;
$db->txn_do(sub {
$jobset->update({ lastcheckedtime => time, fetcherrormsg => $fetchError }) if !$dryRun;
$db->storage->dbh->do("notify eval_failed, ?", undef, join('\t', $tmpId));
$db->storage->dbh->do("notify eval_failed, ?", undef, join("\t", $tmpId, $jobset->get_column('id')));
});
return;
}
@ -882,7 +882,7 @@ sub checkJobset {
$db->txn_do(sub {
$jobset->update({lastcheckedtime => $eventTime});
setJobsetError($jobset, $checkError, $eventTime);
$db->storage->dbh->do("notify eval_failed, ?", undef, join('\t', $tmpId));
$db->storage->dbh->do("notify eval_failed, ?", undef, join("\t", $tmpId, $jobset->get_column('id')));
}) if !$dryRun;
$failed = 1;
}

View file

@ -99,6 +99,7 @@ $listener->subscribe("build_started");
$listener->subscribe("cached_build_finished");
$listener->subscribe("cached_build_queued");
$listener->subscribe("eval_cached");
$listener->subscribe("eval_failed");
$listener->subscribe("eval_started");
$listener->subscribe("hydra_notify_dump_metrics");
$listener->subscribe("step_finished");

View file

@ -0,0 +1,94 @@
use strict;
use warnings;
use Setup;
use Hydra::Event;
use Hydra::Event::EvalFailed;
use Test2::V0;
use Test2::Tools::Exception;
use Test2::Tools::Mock qw(mock_obj);
my $ctx = test_context();
my $builds = $ctx->makeAndEvaluateJobset(
expression => "basic.nix",
build => 1
);
subtest "Parsing eval_failed" => sub {
like(
dies { Hydra::Event::parse_payload("eval_failed", "") },
qr/two arguments/,
"empty payload"
);
like(
dies { Hydra::Event::parse_payload("eval_failed", "abc123") },
qr/two arguments/,
"one argument"
);
like(
dies { Hydra::Event::parse_payload("eval_failed", "abc123\tabc123\tabc123") },
qr/two arguments/,
"three arguments"
);
like(
dies { Hydra::Event::parse_payload("eval_failed", "abc123\tabc123") },
qr/should be an integer/,
"not an integer: second argument"
);
is(
Hydra::Event::parse_payload("eval_failed", "abc123\t456"),
Hydra::Event::EvalFailed->new("abc123", 456)
);
};
subtest "interested" => sub {
my $event = Hydra::Event::EvalFailed->new(123, []);
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 => [
"evalFailed" => sub {}
]
);
is($event->interestedIn($plugin), 1, "The plugin is interesting.");
};
};
subtest "load" => sub {
my $jobset = $builds->{"empty_dir"}->jobset;
my $event = Hydra::Event::EvalFailed->new("traceID", $jobset->id);
$event->load($ctx->db());
is($event->{"jobset"}->get_column("id"), $jobset->id, "The jobset record matches.");
# Create a fake "plugin" with a evalFailed sub, the sub sets this
# "global" passedTraceID, passedJobset
my $passedTraceID;
my $passedJobset;
my $plugin = {};
my $mock = mock_obj $plugin => (
add => [
"evalFailed" => sub {
my ($self, $traceID, $jobset) = @_;
$passedTraceID = $traceID;
$passedJobset = $jobset;
}
]
);
$event->execute($ctx->db(), $plugin);
is($passedTraceID, "traceID", "The plugin is told what the trace ID was");
is($passedJobset->get_column("id"), $jobset->id, "The plugin's evalFailed hook is called with the right jobset");
};
done_testing;

View file

@ -142,8 +142,19 @@ subtest "on a fresh evaluation with corrupted sources" => sub {
close $fh;
ok(evalFails($builds->{"variable-job"}->jobset), "evaluating the corrupted job");
is($listener->block_for_messages(0)->()->{"channel"}, "eval_started", "the evaluation started");
is($listener->block_for_messages(0)->()->{"channel"}, "eval_failed", "the evaluation failed");
my $traceID;
expectEvent($listener, "eval_started", sub {
isnt($_->{"trace_id"}, "", "We got a trace ID");
$traceID = $_->{"trace_id"};
is($_->{"jobset_id"}, $jobset->get_column('id'), "the jobset ID matches");
});
expectEvent($listener, "eval_failed", sub {
is($_->{"trace_id"}, $traceID, "Trace ID matches");
is($_->{"jobset_id"}, $jobset->get_column('id'), "the jobset ID matches");
});
is($listener->block_for_messages(0)->(), undef, "there are no more messages from the evaluator");
};