f24dfbe
# Copyright (C) Red Hat Inc.
f24dfbe
#
f24dfbe
# This program is free software; you can redistribute it and/or modify
f24dfbe
# it under the terms of the GNU General Public License as published by
f24dfbe
# the Free Software Foundation; either version 2 of the License, or
f24dfbe
# (at your option) any later version.
f24dfbe
#
f24dfbe
# This program is distributed in the hope that it will be useful,
f24dfbe
# but WITHOUT ANY WARRANTY; without even the implied warranty of
f24dfbe
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f24dfbe
# GNU General Public License for more details.
f24dfbe
#
f24dfbe
# You should have received a copy of the GNU General Public License along
f24dfbe
# with this program; if not, see <http://www.gnu.org/licenses/>.
f24dfbe
f24dfbe
package OpenQA::WebAPI::Plugin::FedoraUpdateRestart;
f24dfbe
f24dfbe
use strict;
f24dfbe
use warnings;
f24dfbe
f24dfbe
use parent 'Mojolicious::Plugin';
f24dfbe
use Mojo::IOLoop;
1fd2d39
use OpenQA::Events;
f24dfbe
use OpenQA::Schema::Result::Jobs;
f24dfbe
f24dfbe
sub register {
f24dfbe
    my ($self, $app) = @_;
1fd2d39
    OpenQA::Events->singleton->on("openqa_job_done" => sub { shift; $self->on_job_done($app, @_) });
f24dfbe
}
f24dfbe
f24dfbe
# when a job completes, if it's an update test, it failed, and it's
33fb32b
# not already a clone, restart it, unless it's one of the FreeIPA
33fb32b
# tests (which don't work properly on restart and take forever). We
33fb32b
# could use the `dup_type_auto` behaviour, but that allows 3 restarts,
33fb32b
# which is kinda a lot.
f24dfbe
sub on_job_done {
f24dfbe
    my ($self, $app, $args) = @_;
f24dfbe
    my ($user_id, $connection_id, $event, $event_data) = @$args;
f24dfbe
    my $job = $app->db->resultset('Jobs')->find({id => $event_data->{id}});
f24dfbe
    my $clone_of = $app->db->resultset("Jobs")->find({clone_id => $job->id});
33fb32b
    if (
33fb32b
            $job->result eq OpenQA::Schema::Result::Jobs::FAILED && !$clone_of &&
33fb32b
            $job->FLAVOR =~ /^updates-/ &&
33fb32b
            $job->TEST !~ /^.*(domain|realmd)/
33fb32b
    ) {
f24dfbe
        $job->auto_duplicate;
f24dfbe
    }
f24dfbe
}
f24dfbe
f24dfbe
1;