From e8717d084be0295f297ccd1858d5bf4854a10007 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Nov 05 2024 19:18:27 +0000 Subject: [PATCH 1/2] allow setting ttl in protonmsg --- diff --git a/docs/source/plugins.rst b/docs/source/plugins.rst index 7f27895..d28a877 100644 --- a/docs/source/plugins.rst +++ b/docs/source/plugins.rst @@ -224,12 +224,12 @@ The following fields are understood: before timing out The ``[message]`` section sets parameters for how messages are formed. -Currently only one field is understood: * ``extra_limit`` -- the maximum allowed size for ``build.extra`` fields that appear in messages. If the ``build.extra`` field is longer (in terms of json-encoded length), then it will be omitted. The default value is ``0`` which means no limit. +* ``ttl`` -- The time to live to set for messages. Measured in seconds The ``[queue]`` section controls how (or if) the plugin will use the database to queue messages when they cannot be immediately sent. diff --git a/plugins/hub/protonmsg.conf b/plugins/hub/protonmsg.conf index 45f43c4..72c1d26 100644 --- a/plugins/hub/protonmsg.conf +++ b/plugins/hub/protonmsg.conf @@ -11,6 +11,8 @@ send_timeout = 60 # if field is longer (json.dumps), ignore it # default value is 0 - unlimited size extra_limit = 0 +# message ttl can be specified in seconds, default is no ttl +# ttl = 86400 [queue] # enable persistent database queue diff --git a/plugins/hub/protonmsg.py b/plugins/hub/protonmsg.py index c28aa4d..27f13f0 100644 --- a/plugins/hub/protonmsg.py +++ b/plugins/hub/protonmsg.py @@ -93,6 +93,7 @@ class TimeoutHandler(MessagingHandler): return 'topic://' + koji_topic_prefix def send_msgs(self, event): + ttl = self.conf.getfloat('message', 'ttl', fallback=None) for msg in self.msgs: # address is like "topic://koji.package.add" address = self.topic_prefix + '.' + msg['address'] @@ -104,6 +105,9 @@ class TimeoutHandler(MessagingHandler): self.log.debug('created new sender for %s', address) self.senders[address] = sender pmsg = Message(properties=msg['props'], body=msg['body']) + if ttl: + # The message class expects seconds, even though the c api uses milliseconds + pmsg.ttl = ttl delivery = sender.send(pmsg) self.log.debug('sent message: %s', msg['props']) self.pending[delivery] = msg From 26a398b6e1daf184331a27c7fe6778685e41b1e2 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Mar 25 2025 01:00:29 +0000 Subject: [PATCH 2/2] unit test --- diff --git a/tests/test_plugins/test_protonmsg.py b/tests/test_plugins/test_protonmsg.py index 8120adc..de5a03a 100644 --- a/tests/test_plugins/test_protonmsg.py +++ b/tests/test_plugins/test_protonmsg.py @@ -333,6 +333,9 @@ cacert = /etc/koji-hub/plugins/ca.pem topic_prefix = koji connect_timeout = 10 send_timeout = 60 + +[message] +ttl = 7200 """) conf = ConfigParser() if six.PY2: @@ -401,6 +404,8 @@ send_timeout = 60 @patch('protonmsg.SSLDomain') def test_send_msgs(self, SSLDomain, Message): event = MagicMock() + msg = mock.MagicMock() + Message.return_value = msg self.handler.on_start(event) self.handler.msgs = [{'address': 'testtopic', 'props': {'testheader': 1}, 'body': '"test body"'}] @@ -408,6 +413,7 @@ send_timeout = 60 event.container.create_sender.assert_called_once_with(event.connection, target='topic://koji.testtopic') Message.assert_called_once_with(properties={'testheader': 1}, body='"test body"') + self.assertEqual(int(msg.ttl), 7200) sender = event.container.create_sender.return_value sender.send.assert_called_once_with(Message.return_value)