Files
motia/bitbylaw/steps/advoware_cal_sync/calendar_sync_cron_step.py
root 9d40f47e19 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
2025-10-24 19:13:41 +00:00

52 lines
1.5 KiB
Python

import json
import redis
from config import Config
from services.advoware import AdvowareAPI
CALENDAR_SYNC_LOCK_KEY = 'calendar_sync_lock'
config = {
'type': 'cron',
'name': 'Calendar Sync Cron Job',
'description': 'Führt den Calendar Sync alle 5 Minuten automatisch aus',
'cron': '*/5 * * * *', # Alle 5 Minuten
'emits': ['calendar_sync_all'],
'flows': ['advoware']
}
async def get_advoware_employees(context, advoware):
"""Fetch list of employees from Advoware."""
try:
result = await advoware.api_call('api/v1/advonet/Mitarbeiter', method='GET', params={'aktiv': 'true'})
employees = result if isinstance(result, list) else []
context.logger.info(f"Fetched {len(employees)} Advoware employees")
return employees
except Exception as e:
context.logger.error(f"Failed to fetch Advoware employees: {e}")
raise
async def handler(context):
try:
context.logger.info("Calendar Sync Cron: Starting to emit sync-all event")
# Emit sync-all event
await context.emit({
"topic": "calendar_sync_all",
"data": {
"triggered_by": "cron"
}
})
context.logger.info("Calendar Sync Cron: Emitted sync-all event")
return {
'status': 'completed',
'triggered_by': 'cron'
}
except Exception as e:
context.logger.error(f"Fehler beim Cron-Job: {e}")
return {
'status': 'error',
'error': str(e)
}