|
4 | 4 | ClientRequest,
|
5 | 5 | )
|
6 | 6 | from pubsub import pub
|
| 7 | +import sys |
7 | 8 |
|
8 | 9 | from .controllers.connections import ConnectionsController
|
9 | 10 | from .controllers.messaging import MessagingController
|
@@ -143,51 +144,88 @@ def update_tennant_jwt(self, tennant_jwt):
|
143 | 144 |
|
144 | 145 |
|
145 | 146 | def register_listeners(self, listeners, defaults=True):
|
146 |
| - if defaults: |
147 |
| - if self.connections: |
148 |
| - pub.subscribe(self.connections.default_handler, "connections") |
149 |
| - if self.messaging: |
150 |
| - pub.subscribe(self.messaging.default_handler, "basicmessages") |
151 |
| - if self.proofs: |
152 |
| - pub.subscribe(self.proofs.default_handler, "present_proof") |
| 147 | + try: |
| 148 | + if defaults: |
| 149 | + if self.connections: |
| 150 | + pub.subscribe(self.connections.default_handler, "connections") |
| 151 | + if self.messaging: |
| 152 | + pub.subscribe(self.messaging.default_handler, "basicmessages") |
| 153 | + if self.proofs: |
| 154 | + pub.subscribe(self.proofs.default_handler, "present_proof") |
| 155 | + |
| 156 | + for listener in listeners: |
| 157 | + self.add_listener(listener) |
| 158 | + except Exception as exc: |
| 159 | + print(f"Register webhooks listeners failed! {exc!r} occurred.") |
| 160 | + logger.warn(f"Register webhooks listeners failed! {exc!r} occurred.") |
153 | 161 |
|
154 |
| - for listener in listeners: |
155 |
| - self.add_listener(listener) |
156 | 162 |
|
157 | 163 | def add_listener(self, listener):
|
158 |
| - pub.subscribe(listener["handler"], listener["topic"]) |
| 164 | + try: |
| 165 | + pub.subscribe(listener["handler"], listener["topic"]) |
| 166 | + except Exception as exc: |
| 167 | + print(f"Adding webhooks listener failed! {exc!r} occurred.") |
| 168 | + logger.warn(f"Adding webhooks listener failed! {exc!r} occurred.") |
| 169 | + |
159 | 170 |
|
160 | 171 | def remove_listener(self, listener):
|
161 |
| - if pub.isSubscribed(listener["handler"], listener["topic"]): |
162 |
| - pub.unsubscribe(listener["handler"], listener["topic"]) |
163 |
| - else: |
164 |
| - logger.debug("Listener not subscribed", listener) |
| 172 | + try: |
| 173 | + if pub.isSubscribed(listener["handler"], listener["topic"]): |
| 174 | + pub.unsubscribe(listener["handler"], listener["topic"]) |
| 175 | + else: |
| 176 | + logger.debug("Listener not subscribed", listener) |
| 177 | + except Exception as exc: |
| 178 | + print(f"Removing webhooks listener failed! {exc!r} occurred.") |
| 179 | + logger.warn(f"Removing webhooks listener failed! {exc!r} occurred.") |
| 180 | + |
165 | 181 |
|
166 | 182 | def remove_all_listeners(self, topic: str = None):
|
167 | 183 | # Note advanced use of function can include both listenerFilter and topicFilter for this
|
168 | 184 | # Add when needed
|
169 |
| - pub.unsubAll(topicName=topic) |
| 185 | + try: |
| 186 | + pub.unsubAll(topicName=topic) |
| 187 | + except Exception as exc: |
| 188 | + print(f"Removing all webhooks listeners failed! {exc!r} occurred.") |
| 189 | + logger.warn(f"Removing all webhooks listeners failed! {exc!r} occurred.") |
| 190 | + |
170 | 191 |
|
171 | 192 | async def listen_webhooks(self):
|
172 |
| - app = web.Application() |
173 |
| - app.add_routes([web.post(self.webhook_base + "/topic/{topic}/", self._receive_webhook)]) |
174 |
| - runner = web.AppRunner(app) |
175 |
| - await runner.setup() |
176 |
| - self.webhook_site = web.TCPSite(runner, self.webhook_host, self.webhook_port) |
177 |
| - await self.webhook_site.start() |
| 193 | + try: |
| 194 | + app = web.Application() |
| 195 | + app.add_routes([web.post(self.webhook_base + "/topic/{topic}/", self._receive_webhook)]) |
| 196 | + runner = web.AppRunner(app) |
| 197 | + await runner.setup() |
| 198 | + self.webhook_site = web.TCPSite(runner, self.webhook_host, self.webhook_port) |
| 199 | + await self.webhook_site.start() |
| 200 | + except Exception as exc: |
| 201 | + print(f"Listening webhooks failed! {exc!r} occurred.") |
| 202 | + logger.warn(f"Listening webhooks failed! {exc!r} occurred.") |
| 203 | + |
178 | 204 |
|
179 | 205 | async def _receive_webhook(self, request: ClientRequest):
|
180 | 206 | topic = request.match_info["topic"]
|
181 |
| - payload = await request.json() |
182 |
| - await self._handle_webhook(topic, payload) |
183 |
| - return web.Response(status=200) |
| 207 | + try: |
| 208 | + payload = await request.json() |
| 209 | + await self._handle_webhook(topic, payload) |
| 210 | + return web.Response(status=200) |
| 211 | + except Exception as exc: |
| 212 | + logger.warn(f"Receiving webhooks failed! {exc!r} occurred.") |
| 213 | + |
184 | 214 |
|
185 | 215 | async def _handle_webhook(self, topic, payload):
|
186 |
| - logging.debug(f"Handle Webhook - {topic}", payload) |
187 |
| - pub.sendMessage(topic, payload=payload) |
188 |
| - # return web.Response(status=200) |
| 216 | + try: |
| 217 | + logging.debug(f"Handle Webhook - {topic}", payload) |
| 218 | + pub.sendMessage(topic, payload=payload) |
| 219 | + # return web.Response(status=200) |
| 220 | + except Exception as exc: |
| 221 | + logger.warn(f"Handling webhooks failed! {exc!r} occurred when trying to handle this topic: {topic}") |
| 222 | + |
189 | 223 |
|
190 | 224 | async def terminate(self):
|
191 |
| - await self.client_session.close() |
192 |
| - if self.webhook_site: |
193 |
| - await self.webhook_site.stop() |
| 225 | + try: |
| 226 | + await self.client_session.close() |
| 227 | + if self.webhook_site: |
| 228 | + await self.webhook_site.stop() |
| 229 | + except Exception as exc: |
| 230 | + print(f"Terminating webhooks listener failed! {exc!r} occurred.") |
| 231 | + logger.warn(f"Terminating webhooks listener failed! {exc!r} occurred.") |
0 commit comments