Ensure Redis lock is always released with finally block

This commit is contained in:
root
2025-10-24 00:26:18 +00:00
parent 9ca3191542
commit 6d2089ec69

View File

@@ -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 = {