From 44b8d6f4491faec02bcc94f44414708489fa1d86 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 8 May 2013 18:34:18 +0200 Subject: [PATCH] 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}. } --- src/lib/Hydra/Plugin.pm | 15 ++++++++++++--- src/lib/Hydra/Plugin/EmailNotification.pm | 10 ++++++---- src/script/hydra-build | 7 ++++--- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/lib/Hydra/Plugin.pm b/src/lib/Hydra/Plugin.pm index 5d7f5c4b..6d07ab19 100644 --- a/src/lib/Hydra/Plugin.pm +++ b/src/lib/Hydra/Plugin.pm @@ -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; diff --git a/src/lib/Hydra/Plugin/EmailNotification.pm b/src/lib/Hydra/Plugin/EmailNotification.pm index 2bbe1dd9..f80c515c 100644 --- a/src/lib/Hydra/Plugin/EmailNotification.pm +++ b/src/lib/Hydra/Plugin/EmailNotification.pm @@ -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; diff --git a/src/script/hydra-build b/src/script/hydra-build index 13cc5b47..14da2dd9 100755 --- a/src/script/hydra-build +++ b/src/script/hydra-build @@ -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";