Refaktorierung Calendar Sync: Event-driven Design, Fixes für mehrtägige Termine, Logging und Locking
- Refaktorierung zu event-driven Ansatz ohne PostgreSQL Hub - Fixes für mehrtägige Termine: korrekte Verwendung von datumBis, Entfernung 24h-Limit - Per-Employee Locking mit Redis - Logging via context.logger für Motia Workbench - Neue Schritte: calendar_sync_all_step.py, calendar_sync_cron_step.py - Aktualisiertes README.md mit aktueller Architektur - Workbench-Gruppierung: advoware-calendar-sync
This commit is contained in:
@@ -10,7 +10,7 @@ config = {
|
||||
'name': 'Calendar Sync Cron Job',
|
||||
'description': 'Führt den Calendar Sync alle 5 Minuten automatisch aus',
|
||||
'cron': '*/5 * * * *', # Alle 5 Minuten
|
||||
'emits': ['calendar_sync_employee'],
|
||||
'emits': ['calendar_sync_all'],
|
||||
'flows': ['advoware']
|
||||
}
|
||||
|
||||
@@ -27,53 +27,17 @@ async def get_advoware_employees(context, advoware):
|
||||
|
||||
async def handler(context):
|
||||
try:
|
||||
context.logger.info("Calendar Sync Cron: Starting to fetch employees and emit events")
|
||||
context.logger.info("Calendar Sync Cron: Starting to emit sync-all event")
|
||||
|
||||
# Initialize Advoware API
|
||||
advoware = AdvowareAPI(context)
|
||||
# Emit sync-all event
|
||||
await context.emit({
|
||||
"topic": "calendar_sync_all",
|
||||
"data": {
|
||||
"triggered_by": "cron"
|
||||
}
|
||||
})
|
||||
|
||||
# Fetch employees
|
||||
employees = await get_advoware_employees(context, advoware)
|
||||
if not employees:
|
||||
context.logger.error("Keine Mitarbeiter gefunden. Cron abgebrochen.")
|
||||
return {'status': 500, 'body': {'error': 'Keine Mitarbeiter gefunden'}}
|
||||
|
||||
# Emit event for each employee (DEBUG: only for SB)
|
||||
for employee in employees:
|
||||
kuerzel = employee.get('kuerzel')
|
||||
if not kuerzel:
|
||||
context.logger.warning(f"Mitarbeiter ohne Kürzel übersprungen: {employee}")
|
||||
continue
|
||||
|
||||
# DEBUG: Nur für konfigurierte Nutzer syncen
|
||||
if kuerzel not in Config.CALENDAR_SYNC_DEBUG_KUERZEL:
|
||||
context.logger.info(f"DEBUG: Überspringe {kuerzel}, nur {Config.CALENDAR_SYNC_DEBUG_KUERZEL} werden gesynct")
|
||||
continue
|
||||
|
||||
employee_lock_key = f'calendar_sync_lock_{kuerzel}'
|
||||
|
||||
redis_client = redis.Redis(
|
||||
host=Config.REDIS_HOST,
|
||||
port=int(Config.REDIS_PORT),
|
||||
db=int(Config.REDIS_DB_CALENDAR_SYNC),
|
||||
socket_timeout=Config.REDIS_TIMEOUT_SECONDS
|
||||
)
|
||||
|
||||
if redis_client.set(employee_lock_key, 'cron', ex=1800, nx=True) is None:
|
||||
context.logger.info(f"Calendar Sync Cron: Sync bereits aktiv für {kuerzel}, überspringe")
|
||||
continue
|
||||
|
||||
# Emit event for this employee
|
||||
await context.emit({
|
||||
"topic": "calendar_sync_employee",
|
||||
"data": {
|
||||
"kuerzel": kuerzel,
|
||||
"triggered_by": "cron"
|
||||
}
|
||||
})
|
||||
context.logger.info(f"Calendar Sync Cron: Emitted event for employee {kuerzel}")
|
||||
|
||||
context.logger.info("Calendar Sync Cron: Completed emitting events for employees")
|
||||
context.logger.info("Calendar Sync Cron: Emitted sync-all event")
|
||||
return {
|
||||
'status': 'completed',
|
||||
'triggered_by': 'cron'
|
||||
|
||||
Reference in New Issue
Block a user