Blob Blame History Raw
From a8d0125a4d70c31a8497587f5f04b5dd12719ffa Mon Sep 17 00:00:00 2001
From: Adam Williamson <adamw@happyassassin.net>
Date: Sat, 3 Mar 2018 00:30:05 -0800
Subject: [PATCH 1/2] Emit openqa_job_create events when an ISO is posted
 (#1584)

We currently don't actually emit any openqa_job_create events
when posting an ISO - only a batch openqa_iso_create event. It
is easy to also emit an openqa_job_create event for each of the
newly-created jobs, so let's do that.

This also tweaks the Fedmsg plugin a bit, as this change exposed
an issue with it: when hundreds of events occur very quickly, as
now happens when an ISO is posted, the plugin seems to get backed
up waiting for the fedmsgs to be sent out. Eventually I think
something times out and the scheduler gets an error response,
which causes it to give up on scheduling the rest of the ISOs for
the compose it's working on. So to avoid that, we'll daemonize
the fedmsg-logger run so it doesn't block the plugin.

With this approach I suppose we actually run the risk of creating
too many fedmsg-logger processes simultaneously if we ever get to
the point where many hundreds or >1000 jobs are created for each
compose, but let's burn that bridge when we get to it.

Signed-off-by: Adam Williamson <awilliam@redhat.com>
---
 lib/OpenQA/WebAPI/Controller/API/V1/Iso.pm |  3 +++
 lib/OpenQA/WebAPI/Plugin/Fedmsg.pm         | 13 ++++++++++---
 t/18-fedmsg.t                              | 16 ++++++++++------
 3 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/lib/OpenQA/WebAPI/Controller/API/V1/Iso.pm b/lib/OpenQA/WebAPI/Controller/API/V1/Iso.pm
index bb207b9d..b3a9c80b 100644
--- a/lib/OpenQA/WebAPI/Controller/API/V1/Iso.pm
+++ b/lib/OpenQA/WebAPI/Controller/API/V1/Iso.pm
@@ -453,6 +453,9 @@ sub schedule_iso {
         });
 
     $self->emit_event('openqa_iso_create', $args);
+    for my $succjob (@successful_job_ids) {
+        $self->emit_event('openqa_job_create', {id => $succjob});
+    }
     return {
         successful_job_ids => \@successful_job_ids,
         failed_job_info    => \@failed_job_info,
diff --git a/lib/OpenQA/WebAPI/Plugin/Fedmsg.pm b/lib/OpenQA/WebAPI/Plugin/Fedmsg.pm
index b2018ce3..1033d669 100644
--- a/lib/OpenQA/WebAPI/Plugin/Fedmsg.pm
+++ b/lib/OpenQA/WebAPI/Plugin/Fedmsg.pm
@@ -13,6 +13,11 @@
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, see <http://www.gnu.org/licenses/>.
 
+# An openQA plugin that emits fedmsgs for certain openQA events (by shadowing
+# fedmsg internal events). See http://www.fedmsg.com for more on fedmsg.
+# Currently quite specific to Fedora usage. Requires daemonize and
+# fedmsg-logger.
+
 package OpenQA::WebAPI::Plugin::Fedmsg;
 
 use strict;
@@ -56,10 +61,12 @@ sub log_event {
     # do you want to write perl bindings for fedmsg? no? me either.
     # FIXME: should be some way for plugins to have configuration and then
     # cert-prefix could be configurable, for now we hard code it
-    # we use IPC::Run rather than system() as it's easier to mock for testing
+    # we use IPC::Run rather than system() as it's easier to mock for testing,
+    # and we daemonize so we don't block until the message is sent (which can
+    # cause problems when sending hundreds of messages on ISO post)
     my @command = (
-        "fedmsg-logger", "--cert-prefix=openqa", "--modname=openqa", "--topic=$event",
-        "--json-input",  "--message=$event_data"
+        "/usr/sbin/daemonize", "/usr/bin/fedmsg-logger", "--cert-prefix=openqa", "--modname=openqa",
+        "--topic=$event",      "--json-input",           "--message=$event_data"
     );
     my ($stdin, $stderr, $output) = (undef, undef, undef);
     IPC::Run::run(\@command, \$stdin, \$output, \$stderr);
diff --git a/t/18-fedmsg.t b/t/18-fedmsg.t
index a6e56169..93246f55 100644
--- a/t/18-fedmsg.t
+++ b/t/18-fedmsg.t
@@ -84,12 +84,14 @@ my $settings = {
     ARCH        => 'x86_64'
 };
 
+my $commonexpr = '/usr/sbin/daemonize /usr/bin/fedmsg-logger --cert-prefix=openqa --modname=openqa';
 # create a job via API
 my $post = $t->post_ok("/api/v1/jobs" => form => $settings)->status_is(200);
 my $job = $post->tx->res->json->{id};
 is(
     $args,
-    'fedmsg-logger --cert-prefix=openqa --modname=openqa --topic=job.create --json-input --message='
+    $commonexpr
+      . ' --topic=job.create --json-input --message='
       . '{"ARCH":"x86_64","BUILD":"666","DESKTOP":"DESKTOP","DISTRI":"Unicorn","FLAVOR":"pink","ISO":"whatever.iso",'
       . '"ISO_MAXSIZE":"1","KVM":"KVM","MACHINE":"RainbowPC","TEST":"rainbow","VERSION":"42","id":'
       . $job
@@ -106,7 +108,8 @@ $post = $t->post_ok("/api/v1/jobs/" . $job . "/set_done")->status_is(200);
 # check plugin called fedmsg-logger correctly
 is(
     $args,
-    'fedmsg-logger --cert-prefix=openqa --modname=openqa --topic=job.done --json-input --message='
+    $commonexpr
+      . ' --topic=job.done --json-input --message='
       . '{"ARCH":"x86_64","BUILD":"666","FLAVOR":"pink","ISO":"whatever.iso","MACHINE":"RainbowPC",'
       . '"TEST":"rainbow","id":'
       . $job
@@ -124,7 +127,8 @@ my $newjob = $post->tx->res->json->{id};
 # check plugin called fedmsg-logger correctly
 is(
     $args,
-    'fedmsg-logger --cert-prefix=openqa --modname=openqa --topic=job.duplicate --json-input --message='
+    $commonexpr
+      . ' --topic=job.duplicate --json-input --message='
       . '{"ARCH":"x86_64","BUILD":"666","FLAVOR":"pink","ISO":"whatever.iso","MACHINE":"RainbowPC",'
       . '"TEST":"rainbow","auto":0,"id":'
       . $job
@@ -140,7 +144,8 @@ $post = $t->post_ok("/api/v1/jobs/" . $newjob . "/cancel")->status_is(200);
 # check plugin called fedmsg-logger correctly
 is(
     $args,
-    'fedmsg-logger --cert-prefix=openqa --modname=openqa --topic=job.cancel --json-input --message='
+    $commonexpr
+      . ' --topic=job.cancel --json-input --message='
       . '{"ARCH":"x86_64","BUILD":"666","FLAVOR":"pink","ISO":"whatever.iso","MACHINE":"RainbowPC",'
       . '"TEST":"rainbow","id":'
       . $newjob
@@ -157,8 +162,7 @@ $post = $t->post_ok("/api/v1/jobs/$job/comments" => form => {text => "test comme
 # stash the comment ID
 my $comment = $post->tx->res->json->{id};
 # check plugin called fedmsg-logger correctly
-my $commonexpr = 'fedmsg-logger --cert-prefix=openqa --modname=openqa';
-my $dateexpr   = '\d{4}-\d{1,2}-\d{1,2}T\d{2}:\d{2}:\d{2}Z';
+my $dateexpr = '\d{4}-\d{1,2}-\d{1,2}T\d{2}:\d{2}:\d{2}Z';
 like(
     $args,
 qr/$commonexpr --topic=comment.create --json-input --message=\{"created":"$dateexpr","group_id":null,"id":$comment,"job_id":$job,"text":"test comment","updated":"$dateexpr","user":"perci"\}/,
-- 
2.18.0