#331 Publish event.state.changed.min message
Merged 5 months ago by cqi. Opened 5 months ago by lucarval.
lucarval/freshmaker event-min-message  into  master

file modified
+18 -1

@@ -26,6 +26,7 @@ 

  

  import json

  

+ from collections import defaultdict

  from datetime import datetime

  from sqlalchemy.orm import (validates, relationship)

  from sqlalchemy.schema import Index

@@ -312,6 +313,7 @@ 

  

          db.session.commit()

          messaging.publish('event.state.changed', self.json())

+         messaging.publish('event.state.changed.min', self.json_min())

  

      def __repr__(self):

          return "<Event %s, %r, %s>" % (self.message_id, self.event_type, self.search_key)

@@ -324,6 +326,22 @@ 

          return "<%s, search_key=%s>" % (type_name, self.search_key)

  

      def json(self):

+         data = self._common_json()

+         data['builds'] = [b.json() for b in self.builds]

+         return data

+ 

+     def json_min(self):

+         builds_summary = defaultdict(int)

+         builds_summary['total'] = len(self.builds)

+         for build in self.builds:

+             state_name = ArtifactBuildState(build.state).name

+             builds_summary[state_name] += 1

+ 

+         data = self._common_json()

+         data['builds_summary'] = dict(builds_summary)

+         return data

+ 

+     def _common_json(self):

          event_url = get_url_for('event', id=self.id)

          db.session.add(self)

          return {

@@ -337,7 +355,6 @@ 

              "url": event_url,

              "dry_run": self.dry_run,

              "requester": self.requester,

-             "builds": [b.json() for b in self.builds],

          }

  

      def find_dependent_events(self):

file modified
+20

@@ -157,6 +157,26 @@ 

          self.assertEqual(

              str(event), "<UnknownEventType 1024, search_key=test>")

  

+     def test_event_json_min(self):

+         event = Event.create(db.session, "test_msg_id5", "RHSA-2017-289", events.TestingEvent)

+         build = ArtifactBuild.create(db.session, event, "ed", "module", 1234)

+         build.state = ArtifactBuildState.FAILED

+         ArtifactBuild.create(db.session, event, "mksh", "module", 1235, build)

+         db.session.commit()

+         self.assertEqual(event.json_min(), {

+             'builds_summary': {'BUILD': 1, 'FAILED': 1, 'total': 2},

+             'dry_run': False,

+             'event_type_id': 3,

+             'id': 1,

+             'message_id': 'test_msg_id5',

+             'requester': None,

+             'search_key': 'RHSA-2017-289',

+             'state': 0,

+             'state_name': 'INITIALIZED',

+             'state_reason': None,

+             'url': 'http://localhost:5001/api/1/events/1',

+         })

+ 

  

  class TestFindDependentEvents(helpers.ModelsTestCase):

      """Test Event.find_dependent_events"""

Some clients load incoming UMB messages into an environment variable.
Since environment variables have a system dependent size restriction,
the existing event.state.changed message can easily exceed it.

This change makes Freshmaker publish an additional message that is
much smaller. It does so by omitting the list of builds associated with
the event. Instead, it provides a summary of total builds count as well
as builds count for each state. Clients loading received messages in
environment variables should use the new message.

Although the new message provides sufficient details for clients to find
out the list of builds, some clients already exist that rely on this
information to be in the message directly. For this reason, both
messages are now sent.

Signed-off-by: Luiz Carvalho lucarval@redhat.com

:thumbsup: Tests pass locally.

Commit 2587611 fixes this pull-request

Pull-Request has been merged by cqi

5 months ago

Pull-Request has been merged by cqi

5 months ago