Tasks: only execute the event if the plugin is interested in it

This commit is contained in:
Graham Christensen 2021-12-20 13:27:59 -05:00
parent 633fc36d6a
commit a14501c616
12 changed files with 150 additions and 21 deletions

View file

@ -38,6 +38,12 @@ sub new_event {
}, $self; }, $self;
} }
sub interested {
my ($self, $plugin) = @_;
return $self->{"event"}->interestedIn($plugin);
}
sub execute { sub execute {
my ($self, $db, $plugin) = @_; my ($self, $db, $plugin) = @_;
return $self->{"event"}->execute($db, $plugin); return $self->{"event"}->execute($db, $plugin);

View file

@ -27,6 +27,11 @@ sub new {
}, $self; }, $self;
} }
sub interestedIn {
my ($self, $plugin) = @_;
return int(defined($plugin->can('buildFinished')));
}
sub load { sub load {
my ($self, $db) = @_; my ($self, $db) = @_;

View file

@ -25,6 +25,11 @@ sub new {
}, $self; }, $self;
} }
sub interestedIn {
my ($self, $plugin) = @_;
return int(defined($plugin->can('buildQueued')));
}
sub load { sub load {
my ($self, $db) = @_; my ($self, $db) = @_;

View file

@ -25,6 +25,11 @@ sub new {
}, $self; }, $self;
} }
sub interestedIn {
my ($self, $plugin) = @_;
return int(defined($plugin->can('buildStarted')));
}
sub load { sub load {
my ($self, $db) = @_; my ($self, $db) = @_;

View file

@ -34,6 +34,11 @@ sub new :prototype($$$) {
}, $self; }, $self;
} }
sub interestedIn {
my ($self, $plugin) = @_;
return int(defined($plugin->can('stepFinished')));
}
sub load { sub load {
my ($self, $db) = @_; my ($self, $db) = @_;

View file

@ -25,29 +25,35 @@ sub instantiate {
return @$plugins; return @$plugins;
} }
# Called when build $build has been queued. # To implement behaviors in response to the following events, implement
sub buildQueued { # the function in your plugin and it will be executed by hydra-notify.
my ($self, $build) = @_; #
} # See the tests in t/Event/*.t for arguments, and the documentation for
# notify events for semantics.
#
# # Called when build $build has been queued.
# sub buildQueued {
# my ($self, $build) = @_;
# }
# Called when build $build has started. # # Called when build $build has started.
sub buildStarted { # sub buildStarted {
my ($self, $build) = @_; # my ($self, $build) = @_;
} # }
# Called when build $build has finished. If the build failed, then # # Called when build $build has finished. If the build failed, then
# $dependents is an array ref to a list of builds that have also # # $dependents is an array ref to a list of builds that have also
# failed as a result (i.e. because they depend on $build or a failed # # failed as a result (i.e. because they depend on $build or a failed
# dependeny of $build). # # dependeny of $build).
sub buildFinished { # sub buildFinished {
my ($self, $build, $dependents) = @_; # my ($self, $build, $dependents) = @_;
} # }
# Called when step $step has finished. The build log is stored in the # # Called when step $step has finished. The build log is stored in the
# file $logPath (bzip2-compressed). # # file $logPath (bzip2-compressed).
sub stepFinished { # sub stepFinished {
my ($self, $step, $logPath) = @_; # my ($self, $step, $logPath) = @_;
} # }
# Called to determine the set of supported input types. The plugin # Called to determine the set of supported input types. The plugin
# should add these to the $inputTypes hashref, e.g. $inputTypes{'svn'} # should add these to the $inputTypes hashref, e.g. $inputTypes{'svn'}

View file

@ -14,7 +14,6 @@ use Data::Dumper;
my $CONFIG_SECTION = "git-input"; my $CONFIG_SECTION = "git-input";
sub supportedInputTypes { sub supportedInputTypes {
my ($self, $inputTypes) = @_; my ($self, $inputTypes) = @_;
$inputTypes->{'git'} = 'Git checkout'; $inputTypes->{'git'} = 'Git checkout';

View file

@ -117,6 +117,11 @@ sub new {
type => "counter", type => "counter",
help => "Number of tasks that have not been processed because the plugin does not exist." help => "Number of tasks that have not been processed because the plugin does not exist."
); );
$prometheus->declare(
"notify_plugin_not_interested",
type => "counter",
help => "Number of tasks that have not been processed because the plugin was not interested in the event."
);
my %plugins_by_name = map { ref $_ => $_ } @{$plugins}; my %plugins_by_name = map { ref $_ => $_ } @{$plugins};
@ -190,6 +195,11 @@ sub dispatch_task {
return 0; return 0;
} }
if (!$task->{"event"}->interested($plugin)) {
$self->{"prometheus"}->inc("notify_plugin_not_interested", $event_labels);
return 0;
}
$self->{"prometheus"}->inc("notify_plugin_executions", $event_labels); $self->{"prometheus"}->inc("notify_plugin_executions", $event_labels);
eval { eval {
my $start_time = [gettimeofday()]; my $start_time = [gettimeofday()];

View file

@ -54,6 +54,28 @@ my $jobset = createBaseJobset("basic", "basic.nix", $ctx{jobsdir});
ok(evalSucceeds($jobset), "Evaluating jobs/basic.nix should exit with return code 0"); 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"); is(nrQueuedBuildsForJobset($jobset), 3, "Evaluating jobs/basic.nix should result in 3 builds");
subtest "interested" => sub {
my $event = Hydra::Event::BuildFinished->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 => [
"buildFinished" => sub {}
]
);
is($event->interestedIn($plugin), 1, "The plugin is interesting.");
};
};
subtest "load" => sub { subtest "load" => sub {
my ($build, $dependent_a, $dependent_b) = $db->resultset('Builds')->search( my ($build, $dependent_a, $dependent_b) = $db->resultset('Builds')->search(
{ }, { },

View file

@ -39,6 +39,28 @@ subtest "Parsing build_queued" => sub {
); );
}; };
subtest "interested" => sub {
my $event = Hydra::Event::BuildQueued->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 => [
"buildQueued" => sub {}
]
);
is($event->interestedIn($plugin), 1, "The plugin is interesting.");
};
};
subtest "load" => sub { subtest "load" => sub {
my $build = $builds->{"empty_dir"}; my $build = $builds->{"empty_dir"};

View file

@ -45,6 +45,28 @@ subtest "Parsing build_started" => sub {
); );
}; };
subtest "interested" => sub {
my $event = Hydra::Event::BuildStarted->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 => [
"buildStarted" => sub {}
]
);
is($event->interestedIn($plugin), 1, "The plugin is interesting.");
};
};
subtest "load" => sub { subtest "load" => sub {
my $build = $db->resultset('Builds')->search( my $build = $db->resultset('Builds')->search(
{ }, { },

View file

@ -64,6 +64,28 @@ subtest "Parsing step_finished" => sub {
); );
}; };
subtest "interested" => sub {
my $event = Hydra::Event::StepFinished->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 => [
"stepFinished" => sub {}
]
);
is($event->interestedIn($plugin), 1, "The plugin is interesting.");
};
};
subtest "load" => sub { subtest "load" => sub {
my $step = $db->resultset('BuildSteps')->search( my $step = $db->resultset('BuildSteps')->search(