From 6d2089ec69c1450444c44fda0cc8ee457acfd5e4 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 24 Oct 2025 00:26:18 +0000 Subject: [PATCH] Ensure Redis lock is always released with finally block --- .../calendar_sync_event_step.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/bitbylaw/steps/advoware_cal_sync/calendar_sync_event_step.py b/bitbylaw/steps/advoware_cal_sync/calendar_sync_event_step.py index 92254f75..4342ce24 100644 --- a/bitbylaw/steps/advoware_cal_sync/calendar_sync_event_step.py +++ b/bitbylaw/steps/advoware_cal_sync/calendar_sync_event_step.py @@ -898,7 +898,7 @@ async def handler(context): async def reload_db_indexes(): """Reload database indexes after DB changes in phases.""" - state['rows'] = conn.fetch( + state['rows'] = await conn.fetch( """ SELECT * FROM calendar_sync WHERE employee_kuerzel = $1 AND deleted = FALSE @@ -914,9 +914,9 @@ async def handler(context): async def reload_api_maps(): """Reload API maps after creating new events in phases.""" - state['adv_appointments'] = fetch_advoware_appointments(advoware, kuerzel) + state['adv_appointments'] = await fetch_advoware_appointments(advoware, kuerzel) state['adv_map'] = {str(app['frNr']): app for app in state['adv_appointments'] if app.get('frNr')} - state['google_events'] = fetch_google_events(service, calendar_id) + state['google_events'] = await fetch_google_events(service, calendar_id) state['google_map'] = {evt['id']: evt for evt in state['google_events']} log_operation('debug', "Reloaded API maps", context=context, adv=len(state['adv_map']), google=len(state['google_map'])) @@ -957,16 +957,20 @@ async def handler(context): log_operation('debug', "Updated last_sync timestamps", context=context) finally: - conn.close() + await conn.close() - redis_client.delete(employee_lock_key) log_operation('info', f"Calendar sync completed for {kuerzel}", context=context) return {'status': 200, 'body': {'status': 'completed', 'kuerzel': kuerzel}} except Exception as e: log_operation('error', f"Sync failed for {kuerzel}: {e}", context=context) - redis_client.delete(employee_lock_key) return {'status': 500, 'body': {'error': str(e)}} + finally: + # Ensure lock is always released + try: + redis_client.delete(employee_lock_key) + except Exception: + pass # Ignore errors when deleting lock # Motia Step Configuration config = {