Use OO-style plugins

This is mostly so we don't have to pass around common parameters like
"db" and "config", and we don't have to check for the existence of
methods.

A plugin now looks like this:

  package Hydra::Plugin::TwitterNotification;

  use parent 'Hydra::Plugin';

  sub buildFinished {
      my ($self, $build, $dependents) = @_;
      print STDERR "tweeting about build ", $build->id, "\n";
      # Send tweet...
      # Hydra database is $self->{db}.
  }
This commit is contained in:
Eelco Dolstra 2013-05-08 18:34:18 +02:00
parent a420a33f66
commit 44b8d6f449
3 changed files with 22 additions and 10 deletions

View file

@ -1,14 +1,23 @@
package Hydra::Plugin;
use strict;
use Module::Pluggable
search_path => "Hydra::Plugin",
require => 1;
instantiate => 'new';
sub new {
my ($class, %args) = @_;
my $self = { db => $args{db}, config => $args{config} };
bless $self, $class;
return $self;
}
# $plugin->buildFinished($db, $config, $build, $dependents):
#
# Called when build $build has finished. If the build failed, then
# $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
# dependeny of $build).
sub buildFinished {
my ($self, $build, $dependents) = @_;
}
1;

View file

@ -1,6 +1,7 @@
package Hydra::Plugin::EmailNotification;
use strict;
use parent 'Hydra::Plugin';
use feature qw/switch/;
use POSIX qw(strftime);
use Email::Sender::Simple qw(sendmail);
@ -29,12 +30,12 @@ sub statusDescription {
sub buildFinished {
my ($self, $db, $config, $build, $dependents) = @_;
my ($self, $build, $dependents) = @_;
die unless $build->finished;
my $prevBuild;
($prevBuild) = $db->resultset('Builds')->search(
($prevBuild) = $self->{db}->resultset('Builds')->search(
{ project => $build->project->name
, jobset => $build->jobset->name
, job => $build->job->name
@ -68,10 +69,10 @@ sub buildFinished {
my $status = statusDescription($build->buildstatus);
my $baseurl = hostname_long;
my $sender = $config->{'notification_sender'} ||
my $sender = $self->{config}->{'notification_sender'} ||
(($ENV{'USER'} || "hydra") . "@" . $baseurl);
my $selfURI = $config->{'base_uri'} || "http://localhost:3000";
my $selfURI = $self->{config}->{'base_uri'} || "http://localhost:3000";
sub showTime { my ($x) = @_; return strftime('%Y-%m-%d %H:%M:%S', localtime($x)); }
@ -163,4 +164,5 @@ sub buildFinished {
}
}
1;

View file

@ -17,6 +17,8 @@ my $db = Hydra::Model::DB->new();
my $config = getHydraConfig();
my @plugins = Hydra::Plugin->plugins(db => $db, config => $config);
sub addBuildStepOutputs {
my ($step) = @_;
@ -79,10 +81,9 @@ sub failDependents {
sub notify {
my ($build, $dependents) = @_;
foreach my $plugin (Hydra::Plugin->plugins) {
next unless $plugin->can('buildFinished');
foreach my $plugin (@plugins) {
eval {
$plugin->buildFinished($db, $config, $build, []);
$plugin->buildFinished($build, []);
};
if ($@) {
print STDERR "$plugin->buildFinished: $@\n";