From 63e132394241498a44f9bd0adf2719362bebac6f Mon Sep 17 00:00:00 2001 From: Ro'e Katz Date: Tue, 27 Jun 2023 13:47:46 +0300 Subject: [PATCH] Webhook: Fix not listening to notifications when no broadcaster --- .../opal_server/policy/watcher/task.py | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/opal-server/opal_server/policy/watcher/task.py b/packages/opal-server/opal_server/policy/watcher/task.py index f49582fe..a2ba5755 100644 --- a/packages/opal-server/opal_server/policy/watcher/task.py +++ b/packages/opal-server/opal_server/policy/watcher/task.py @@ -38,21 +38,27 @@ async def _on_webhook(self, topic: Topic, data: Any): async def _listen_to_webhook_notifications(self): # Webhook api route can be hit randomly in all workers, so it publishes a message to the webhook topic. # This listener, running in the leader's context, would actually trigger the repo pull + + async def _subscribe_internal(): + logger.info( + "listening on webhook topic: '{topic}'", + topic=opal_server_config.POLICY_REPO_WEBHOOK_TOPIC, + ) + await self._pubsub_endpoint.subscribe( + [opal_server_config.POLICY_REPO_WEBHOOK_TOPIC], + self._on_webhook, + ) + if self._pubsub_endpoint.broadcaster is not None: async with self._pubsub_endpoint.broadcaster.get_listening_context(): - logger.info( - "listening on webhook topic: '{topic}'", - topic=opal_server_config.POLICY_REPO_WEBHOOK_TOPIC, - ) - - await self._pubsub_endpoint.subscribe( - [opal_server_config.POLICY_REPO_WEBHOOK_TOPIC], - self._on_webhook, - ) + await _subscribe_internal() await self._pubsub_endpoint.broadcaster.get_reader_task() # Stop the watcher if broadcaster disconnects self.signal_stop() + else: + # If no broadcaster is configured, just subscribe, no need to wait on anything + await _subscribe_internal() async def start(self): """starts the policy watcher and registers a failure callback to