Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
perf: Use scheduler for queuing
Clémence Lesné6a652f96b4d3
diff --git a/app/persistence/azure_queue_storage.py b/app/persistence/azure_queue_storage.pyindex a74cf9f..239c7ef 100644--- a/app/persistence/azure_queue_storage.py+++ b/app/persistence/azure_queue_storage.py@@ -4,6 +4,7 @@ from binascii import Error as BinasciiErrorfrom collections.abc import AsyncGenerator, Awaitable, Callablefrom contextlib import asynccontextmanager+import aiojobsfrom azure.core.exceptions import ServiceRequestErrorfrom azure.storage.queue.aio import QueueClient, QueueServiceClientfrom pydantic import BaseModel@@ -125,7 +126,7 @@ class AzureQueueStorage:self,arg: str,func: Callable[..., Awaitable],- ):+ ) -> None:"""Trigger a local function when a message is received."""@@ -134,19 +135,36 @@ class AzureQueueStorage:self._name,func.__name__,)- # Loop forever to receive messages- while messages := self.receive_messages(- max_messages=32,- visibility_timeout=32 * 5, # 5 secs per message- ):- # Process messages- async for message in messages:- # Call function with the selected argument name- kwargs = {}- kwargs[arg] = message- # First, call function- await func(**kwargs)- # Then, delete message- await self.delete_message(message)- # Add a small delay to avoid tight loop- await asyncio.sleep(1)+ async with aiojobs.Scheduler() as scheduler:+ # Loop forever to receive messages+ while messages := self.receive_messages(+ max_messages=32,+ visibility_timeout=32 * 5, # 5 secs per message+ ):+ # Process messages+ async for message in messages:+ await scheduler.spawn(+ self._process_message(+ arg=arg,+ func=func,+ message=message,+ )+ )+ # Add a small delay to avoid high CPU usage+ await asyncio.sleep(1)++ async def _process_message(+ self,+ arg: str,+ func: Callable[..., Awaitable],+ message: Message,+ ) -> None:+ """+ Process a message with a function.+ """+ # First, call function with the selected argument name+ kwargs = {}+ kwargs[arg] = message+ await func(**kwargs)+ # Then, delete message+ await self.delete_message(message)diff --git a/pyproject.toml b/pyproject.tomlindex 84a3889..ba76203 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -8,6 +8,7 @@ requires-python = ">=3.12"dependencies = ["aiohttp-retry~=2.9", # Retry middleware for aiohttp, used with Twilio SDK"aiohttp[speedups]~=3.10", # Async HTTP client for Azure and Twilio SDKs, plus async DNS resolver and async Brotli compression+ "aiojobs~=1.3", # Async job scheduler"aiosqlite~=0.20", # Async SQLite3 driver"azure-ai-translation-text~=1.0", # Azure Cognitive Services Text Translation"azure-appconfiguration~=1.7", # Outsourced configuration for live updatesdiff --git a/requirements-dev.txt b/requirements-dev.txtindex 8eba0c4..a7f6adf 100644--- a/requirements-dev.txt+++ b/requirements-dev.txt@@ -118,10 +118,18 @@ aiohttp-retry==2.9.1 \# via# call-center-ai (pyproject.toml)# twilio+aiojobs==1.3.0 \+ --hash=sha256:03074c884b3dc388b8d798c0de24ec17d72b2799018497fda8062c0431a494b5 \+ --hash=sha256:1f9f36179b6d50796c4fc9e8851fdae10f38d6c2f64412a91e2c4eff73054ce0+ # via call-center-ai (pyproject.toml)aiosignal==1.3.1 \--hash=sha256:54cd96e15e1649b75d6c87526a6ff0b6c1b0dd3459f43d9ca11d48c339b68cfc \--hash=sha256:f8376fb07dd1e86a584e4fcdec80b36b7f81aac666ebc724e2c090300dd83b17# via aiohttp+aiosqlite==0.20.0 \+ --hash=sha256:54cd96e15e1649b75d6c87526a6ff0b6c1b0dd3459f43d9ca11d48c339b68cfc \+ --hash=sha256:f8376fb07dd1e86a584e4fcdec80b36b7f81aac666ebc724e2c090300dd83b17+ # via aiohttpaiosqlite==0.20.0 \--hash=sha256:36a1deaca0cac40ebe32aac9977a6e2bbc7f5189f23f4a54d5908986729e5bd6 \--hash=sha256:6d35c8c256637f4672f843c31021464090805bf925385ac39473fb16eaaca3d7diff --git a/requirements.txt b/requirements.txtindex 89ab01d..ac75002 100644--- a/requirements.txt+++ b/requirements.txt@@ -114,6 +114,10 @@ aiohttp-retry==2.9.1 \# via# call-center-ai (pyproject.toml)# twilio+aiojobs==1.3.0 \+ --hash=sha256:03074c884b3dc388b8d798c0de24ec17d72b2799018497fda8062c0431a494b5 \+ --hash=sha256:1f9f36179b6d50796c4fc9e8851fdae10f38d6c2f64412a91e2c4eff73054ce0+ # via call-center-ai (pyproject.toml)aiosignal==1.3.1 \--hash=sha256:54cd96e15e1649b75d6c87526a6ff0b6c1b0dd3459f43d9ca11d48c339b68cfc \--hash=sha256:f8376fb07dd1e86a584e4fcdec80b36b7f81aac666ebc724e2c090300dd83b17