From c57bf2eea2ea054d11ea16c371810f54508686dd Mon Sep 17 00:00:00 2001 From: Luiz Carvalho Date: Nov 21 2018 21:50:17 +0000 Subject: Publish event.state.changed.min message 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 --- diff --git a/freshmaker/models.py b/freshmaker/models.py index 68c29d6..2cbb5b8 100644 --- a/freshmaker/models.py +++ b/freshmaker/models.py @@ -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 @@ class Event(FreshmakerBase): db.session.commit() messaging.publish('event.state.changed', self.json()) + messaging.publish('event.state.changed.min', self.json_min()) def __repr__(self): return "" % (self.message_id, self.event_type, self.search_key) @@ -324,6 +326,22 @@ class Event(FreshmakerBase): 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 @@ class Event(FreshmakerBase): "url": event_url, "dry_run": self.dry_run, "requester": self.requester, - "builds": [b.json() for b in self.builds], } def find_dependent_events(self): diff --git a/tests/test_models.py b/tests/test_models.py index 799721d..af63281 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -157,6 +157,26 @@ class TestModels(helpers.ModelsTestCase): self.assertEqual( str(event), "") + 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"""