Skip to content

Commit

Permalink
[ADD] infinity loop for websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
shurshilov committed Jun 6, 2024
1 parent 3c33bf5 commit 40ea9a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
22 changes: 12 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,24 @@ async def catch_exception_internal(req: Request, exc: Exception):

async def producer_webhook(config: Config, timeout: int = 30) -> None:
"""Producer send events to cusomer webhook from config file"""

while True:
try:
websocket_client = WebsocketEvents(
ari_config=config.ari_config,
api_key=config.api_key,
api_key_base64=config.api_key_base64,
webhook_url=f"{config.webhook_url}",
timeout=timeout,
)
app.state.websocket_client = websocket_client
if not app.state.websocket_client:
websocket_client = WebsocketEvents(
ari_config=config.ari_config,
api_key=config.api_key,
api_key_base64=config.api_key_base64,
webhook_url=f"{config.webhook_url}",
timeout=timeout,
)
app.state.websocket_client = websocket_client

await websocket_client.start_consumer()
except asyncio.CancelledError:
break
except Exception as e:
log.exception("Unknown producer_webhook error: %s", e)
except Exception as exc:
log.exception("Unknown producer_webhook error: %s", exc)
finally:
await asyncio.sleep(timeout)

Expand Down
3 changes: 2 additions & 1 deletion services/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
self.api_key_base64 = api_key_base64

self.websocket_url = (
f"{websocket_url}?api_key={api_key}&app=AsteriskAgentPythonDev&subscribeAll=true"
f"{websocket_url}?api_key={api_key}&app=AsteriskAgentPython&subscribeAll=true"
)
self.webhook_url = webhook_url
self.timeout = timeout
Expand Down Expand Up @@ -224,5 +224,6 @@ async def start_consumer(self):
self.disconnect_count += 1
self.disconnected_reason = str(exc)
self.disconnected_time = str(datetime.datetime.now())
raise exc
# await asyncio.sleep(self.timeout)
# await self.start_consumer()

0 comments on commit 40ea9a3

Please sign in to comment.