hydra-notify: move BuildStarted processing to an Event

[?]
Aug 12, 2021, 1:56 PM
OPSWSU4LKI2F6UJXL7B7MH4C3L7DVVKYW5FARAJ44XYH4WXELLCQC

Dependencies

  • [2] CQZQE32V Improve handling of Perl's block eval errors
  • [3] GQGQEMMA Event.pm: add a new_event helper to parse and construct an Event
  • [4] FN3QFV6V hydra-notify: Create a helper for running each plugin on an event
  • [5] NTEDD7T4 Provide a plugin hook for when build steps finish
  • [6] IE2PRAQU hydra-queue-runner: Send build notifications
  • [7] GE7LFZHQ hydra-notify: move buildFinished query in to the function impl
  • [8] EKHD4I44 Event: init structure and parse existing messages
  • [9] FCTX433O Add buildStarted plugin hook
  • [10] P4SME2BC Abstract over postgres' LISTEN/NOTIFY
  • [11] 4ZCUCACY hydra-notify: Fix processing notifications
  • [12] 32KJOERM Turn hydra-notify into a daemon
  • [*] 2JJP7673 tests: move to t, allow `yath test` from root

Change contents

  • replacement in src/lib/Hydra/Event/BuildStarted.pm at line 22
    [5.1229][5.1229:1276]()
    return bless { "build_id" => $id }, $self;
    [5.1229]
    [5.1276]
    return bless {
    "build_id" => $id,
    "build" => undef
    }, $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";
    }
    }
    sub execute {
    my ($self, $db, $plugin) = @_;
    $self->load($db);
    $plugin->buildStarted($self->{"build"});
    return 1;
  • edit in src/script/hydra-notify at line 46
    [4.273][4.273:274](),[4.274][5.1592:1635](),[5.1592][5.1592:1635](),[5.1635][5.3412:3413](),[5.1657][5.3412:3413](),[5.3820][5.3412:3413](),[5.3412][5.3412:3413](),[5.3413][5.1636:1743]()
    sub buildStarted {
    my ($buildId) = @_;
    my $build = $db->resultset('Builds')->find($buildId)
    or die "build $buildId does not exist\n";
  • edit in src/script/hydra-notify at line 47
    [5.1744][5.1744:1780](),[5.1780][2.699:857](),[2.857][5.1903:1921](),[5.1903][5.1903:1921](),[5.1921][5.3821:3822](),[5.1791][5.3821:3822]()
    foreach my $plugin (@plugins) {
    eval {
    $plugin->buildStarted($build);
    1;
    } or do {
    print STDERR "error with $plugin->buildStarted: $@\n";
    }
    }
    }
  • replacement in src/script/hydra-notify at line 123
    [5.286][5.286:334]()
    buildStarted(int($payload[0]));
    [5.286]
    [5.334]
    my $event = Hydra::Event::new_event($channelName, $message->{"payload"});
    runPluginsForEvent($event);
  • file addition: Event (d--r------)
    [14.697]
  • file addition: BuildStarted.t (----------)
    [0.587]
    use strict;
    use Setup;
    my %ctx = test_init();
    require Hydra::Schema;
    require Hydra::Model::DB;
    use Hydra::Event;
    use Hydra::Event::BuildStarted;
    use Test2::V0;
    use Test2::Tools::Exception;
    use Test2::Tools::Mock qw(mock_obj);
    my $db = Hydra::Model::DB->new;
    hydra_setup($db);
    my $project = $db->resultset('Projects')->create({name => "tests", displayname => "", owner => "root"});
    my $jobset = createBaseJobset("basic", "basic.nix", $ctx{jobsdir});
    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");
    subtest "Parsing build_started" => sub {
    like(
    dies { Hydra::Event::parse_payload("build_started", "") },
    qr/one argument/,
    "empty payload"
    );
    like(
    dies { Hydra::Event::parse_payload("build_started", "abc123\tabc123") },
    qr/only one argument/,
    "two arguments"
    );
    like(
    dies { Hydra::Event::parse_payload("build_started", "abc123") },
    qr/should be an integer/,
    "not an integer"
    );
    is(
    Hydra::Event::parse_payload("build_started", "19"),
    Hydra::Event::BuildStarted->new(19),
    "Valid parse"
    );
    };
    subtest "load" => sub {
    my $build = $db->resultset('Builds')->search(
    { },
    { limit => 1 }
    )->next;
    my $event = Hydra::Event::BuildStarted->new($build->id);
    $event->load($db);
    is($event->{"build"}->id, $build->id, "The build record matches.");
    # Create a fake "plugin" with a buildStarted sub, the sub sets this
    # global passedBuild variable.
    my $passedBuild;
    my $plugin = {};
    my $mock = mock_obj $plugin => (
    add => [
    "buildStarted" => sub {
    my ($self, $build) = @_;
    $passedBuild = $build;
    }
    ]
    );
    $event->execute($db, $plugin);
    is($passedBuild->id, $build->id, "The plugin's buildStarted hook is called with the proper build");
    };
    done_testing;
  • edit in t/Event.t at line 4
    [5.3034][5.3034:3066]()
    use Hydra::Event::BuildStarted;
  • edit in t/Event.t at line 15
    [3.466][3.466:467](),[3.467][5.3144:3478](),[5.3144][5.3144:3478]()
    subtest "Payload type: build_started" => sub {
    like(
    dies { Hydra::Event::parse_payload("build_started", "") },
    qr/one argument/,
    "empty payload"
    );
    like(
    dies { Hydra::Event::parse_payload("build_started", "abc123\tabc123") },
    qr/only one argument/,
    "two arguments"
    );
  • edit in t/Event.t at line 16
    [5.3479][5.3479:3773]()
    like(
    dies { Hydra::Event::parse_payload("build_started", "abc123") },
    qr/should be an integer/,
    "not an integer"
    );
    is(
    Hydra::Event::parse_payload("build_started", "19"),
    Hydra::Event::BuildStarted->new(19),
    "Valid parse"
    );
    };