hydra-notify: move BuildFinished processing to an Event

This commit is contained in:
Graham Christensen
2021-08-12 12:03:25 -04:00
committed by Your Name
parent 4a1389e36e
commit fa6d7abc13
4 changed files with 145 additions and 84 deletions

View File

@ -18,8 +18,48 @@ sub parse :prototype(@) {
}
sub new {
my ($self, $build_id, $dependencies) = @_;
return bless { "build_id" => $build_id, "dependencies" => $dependencies }, $self;
my ($self, $build_id, $dependent_ids) = @_;
return bless {
"build_id" => $build_id,
"dependent_ids" => $dependent_ids,
"build" => undef,
"dependents" => [],
}, $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";
foreach my $id (@{$self->{"dependent_ids"}}) {
my $dep = $db->resultset('Builds')->find($id)
or die "dependent build $id does not exist\n";
push @{$self->{"dependents"}}, $dep;
}
}
}
sub execute {
my ($self, $db, $plugin) = @_;
$self->load($db);
$plugin->buildFinished($self->{"build"}, $self->{"dependents"});
# Mark the build and all dependents as having their notifications "finished".
#
# Otherwise, the dependent builds will remain with notificationpendingsince set
# until hydra-notify is started, as buildFinished is never emitted for them.
foreach my $b ($self->{"build"}, @{$self->{"dependents"}}) {
if ($b->finished && defined($b->notificationpendingsince)) {
$b->update({ notificationpendingsince => undef })
}
}
return 1;
}
1;

View File

@ -2,10 +2,11 @@
use strict;
use utf8;
use Hydra::Event::BuildFinished;
use Hydra::Helper::AddBuilds;
use Hydra::Helper::Nix;
use Hydra::Plugin;
use Hydra::PostgresListener;
use Hydra::Helper::Nix;
use Hydra::Helper::AddBuilds;
use Getopt::Long;
STDERR->autoflush(1);
@ -44,44 +45,15 @@ sub runPluginsForEvent {
}
}
sub buildFinished {
my ($buildId, @deps) = @_;
my $build = $db->resultset('Builds')->find($buildId)
or die "build $buildId does not exist\n";
my @dependents;
foreach my $id (@deps) {
my $dep = $db->resultset('Builds')->find($id)
or die "build $id does not exist\n";
push @dependents, $dep;
}
foreach my $plugin (@plugins) {
eval {
$plugin->buildFinished($build, [@dependents]);
1;
} or do {
print STDERR "error with $plugin->buildFinished: $@\n";
}
}
# We have to iterate through all dependents as well, and if they are finished
# to mark their notificationpendingsince.
# Otherwise, the dependent builds will remain with notificationpendingsince set
# until hydra-notify is started, as buildFinished is never emitted for them.
foreach my $b ($build, @dependents) {
$b->update({ notificationpendingsince => undef }) if $b->finished;
}
}
# Process builds that finished while hydra-notify wasn't running.
for my $build ($db->resultset('Builds')->search(
{ notificationpendingsince => { '!=', undef } }))
{
my $buildId = $build->id;
print STDERR "sending notifications for build ${\$buildId}...\n";
buildFinished($build->id);
print STDERR "sending notifications for build $build->id...\n";
my $event = Hydra::Event::BuildFinished->new($build->id);
runPluginsForEvent($event);
}
@ -93,18 +65,11 @@ while (!$queued_only) {
my $channelName = $message->{"channel"};
my $pid = $message->{"pid"};
my $payload = $message->{"payload"};
#print STDERR "got '$channelName' from $pid: $payload\n";
my @payload = split /\t/, $payload;
eval {
if ($channelName eq "build_started" || $channelName eq "step_finished" ) {
my $event = Hydra::Event::new_event($channelName, $message->{"payload"});
runPluginsForEvent($event);
} elsif ($channelName eq "build_finished") {
my $buildId = int($payload[0]);
buildFinished($buildId, @payload[1..$#payload]);
}
my $event = Hydra::Event::new_event($channelName, $message->{"payload"});
runPluginsForEvent($event);
1;
} or do {
print STDERR "error processing message '$payload' on channel '$channelName': $@\n";