Update loop_interval in Tracker class and handle timeout while processing shipments

- Update loop_interval to 60 seconds
- Add loop_timeout to Tracker class with a value of 30 seconds
- Wrap process_shipment calls in asyncio.wait_for with timeout set to loop_timeout value
- Add try-except block to handle TimeoutError and log a warning message
- Code changes aim to improve the handling of long-running shipments processing with a timeout mechanism in place
This commit is contained in:
Kumi 2023-08-24 17:34:18 +02:00
parent 080b019efc
commit 9374b1284e
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -14,6 +14,7 @@ from pykeydelivery import KeyDelivery
class Tracker: class Tracker:
loop_interval = 60 loop_interval = 60
loop_timeout = 30
def __init__(self): def __init__(self):
logging.basicConfig( logging.basicConfig(
@ -158,10 +159,14 @@ class Tracker:
while True: while True:
tasks = [] tasks = []
for shipment in self.db.get_shipments(): for shipment in self.db.get_shipments():
task = asyncio.create_task(asyncio.to_thread(self.process_shipment, shipment)) task = asyncio.wait_for(asyncio.to_thread(self.process_shipment, shipment), timeout=self.loop_timeout)
tasks.append(task) tasks.append(task)
await asyncio.gather(*tasks) try:
await asyncio.gather(*tasks)
except asyncio.TimeoutError:
logging.warning("Timeout while processing shipments")
await asyncio.sleep(self.loop_interval) await asyncio.sleep(self.loop_interval)
def start(self): def start(self):