Ensure Redis lock is always released with finally block
This commit is contained in:
@@ -898,7 +898,7 @@ async def handler(context):
|
|||||||
|
|
||||||
async def reload_db_indexes():
|
async def reload_db_indexes():
|
||||||
"""Reload database indexes after DB changes in phases."""
|
"""Reload database indexes after DB changes in phases."""
|
||||||
state['rows'] = conn.fetch(
|
state['rows'] = await conn.fetch(
|
||||||
"""
|
"""
|
||||||
SELECT * FROM calendar_sync
|
SELECT * FROM calendar_sync
|
||||||
WHERE employee_kuerzel = $1 AND deleted = FALSE
|
WHERE employee_kuerzel = $1 AND deleted = FALSE
|
||||||
@@ -914,9 +914,9 @@ async def handler(context):
|
|||||||
|
|
||||||
async def reload_api_maps():
|
async def reload_api_maps():
|
||||||
"""Reload API maps after creating new events in phases."""
|
"""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['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']}
|
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']))
|
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)
|
log_operation('debug', "Updated last_sync timestamps", context=context)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
await conn.close()
|
||||||
|
|
||||||
redis_client.delete(employee_lock_key)
|
|
||||||
log_operation('info', f"Calendar sync completed for {kuerzel}", context=context)
|
log_operation('info', f"Calendar sync completed for {kuerzel}", context=context)
|
||||||
return {'status': 200, 'body': {'status': 'completed', 'kuerzel': kuerzel}}
|
return {'status': 200, 'body': {'status': 'completed', 'kuerzel': kuerzel}}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_operation('error', f"Sync failed for {kuerzel}: {e}", context=context)
|
log_operation('error', f"Sync failed for {kuerzel}: {e}", context=context)
|
||||||
redis_client.delete(employee_lock_key)
|
|
||||||
return {'status': 500, 'body': {'error': str(e)}}
|
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
|
# Motia Step Configuration
|
||||||
config = {
|
config = {
|
||||||
|
|||||||
Reference in New Issue
Block a user