114 lines
4.3 KiB
Python
114 lines
4.3 KiB
Python
"""
|
|
Calendar Sync All Step
|
|
|
|
Handles calendar_sync_all event and emits individual sync events for oldest employees.
|
|
Uses Redis to track last sync times and distribute work.
|
|
"""
|
|
import sys
|
|
from pathlib import Path
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
from calendar_sync_utils import (
|
|
get_redis_client,
|
|
get_advoware_employees,
|
|
set_employee_lock,
|
|
log_operation
|
|
)
|
|
|
|
import math
|
|
import time
|
|
from datetime import datetime
|
|
from typing import Any, Dict
|
|
from motia import queue, FlowContext
|
|
from pydantic import BaseModel, Field
|
|
from services.advoware_service import AdvowareService
|
|
|
|
config = {
|
|
'name': 'Calendar Sync All Step',
|
|
'description': 'Receives sync-all event and emits individual events for oldest employees',
|
|
'flows': ['advoware-calendar-sync'],
|
|
'triggers': [
|
|
queue('calendar_sync_all')
|
|
],
|
|
'enqueues': ['calendar_sync_employee']
|
|
}
|
|
|
|
|
|
async def handler(input_data: Dict[str, Any], ctx: FlowContext) -> None:
|
|
"""
|
|
Handler that fetches all employees, sorts by last sync time,
|
|
and emits calendar_sync_employee events for the oldest ones.
|
|
"""
|
|
try:
|
|
triggered_by = input_data.get('triggered_by', 'unknown')
|
|
log_operation('info', f"Calendar Sync All: Starting to emit events for oldest employees, triggered by {triggered_by}", context=ctx)
|
|
|
|
# Initialize Advoware service
|
|
advoware = AdvowareService(ctx)
|
|
|
|
# Fetch employees
|
|
employees = await get_advoware_employees(advoware, ctx)
|
|
if not employees:
|
|
log_operation('error', "Keine Mitarbeiter gefunden. All-Sync abgebrochen.", context=ctx)
|
|
return {'status': 500, 'body': {'error': 'Keine Mitarbeiter gefunden'}}
|
|
|
|
redis_client = get_redis_client(ctx)
|
|
|
|
# Collect last_synced timestamps
|
|
employee_timestamps = {}
|
|
for employee in employees:
|
|
kuerzel = employee.get('kuerzel')
|
|
if not kuerzel:
|
|
continue
|
|
employee_last_synced_key = f'calendar_sync_last_synced_{kuerzel}'
|
|
timestamp_str = redis_client.get(employee_last_synced_key)
|
|
timestamp = int(timestamp_str) if timestamp_str else 0 # 0 if no timestamp (very old)
|
|
employee_timestamps[kuerzel] = timestamp
|
|
|
|
# Sort employees by last_synced (ascending, oldest first), then by kuerzel alphabetically
|
|
sorted_kuerzel = sorted(employee_timestamps.keys(), key=lambda k: (employee_timestamps[k], k))
|
|
|
|
# Log the sorted list with timestamps
|
|
def format_timestamp(ts):
|
|
if ts == 0:
|
|
return "never"
|
|
return datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
sorted_list_str = ", ".join(f"{k} ({format_timestamp(employee_timestamps[k])})" for k in sorted_kuerzel)
|
|
log_operation('info', f"Calendar Sync All: Sorted employees by last synced: {sorted_list_str}", context=ctx)
|
|
|
|
# Calculate number to sync: ceil(N / 10)
|
|
num_to_sync = math.ceil(len(sorted_kuerzel) / 1)
|
|
log_operation('info', f"Calendar Sync All: Total employees {len(sorted_kuerzel)}, syncing {num_to_sync} oldest", context=ctx)
|
|
|
|
# Emit for the oldest num_to_sync employees, if not locked
|
|
emitted_count = 0
|
|
for kuerzel in sorted_kuerzel[:num_to_sync]:
|
|
if not set_employee_lock(redis_client, kuerzel, triggered_by, ctx):
|
|
log_operation('info', f"Calendar Sync All: Sync already active for {kuerzel}, skipping", context=ctx)
|
|
continue
|
|
|
|
# Emit event for this employee
|
|
await ctx.enqueue({
|
|
"topic": "calendar_sync_employee",
|
|
"data": {
|
|
"kuerzel": kuerzel,
|
|
"triggered_by": triggered_by
|
|
}
|
|
})
|
|
log_operation('info', f"Calendar Sync All: Emitted event for employee {kuerzel} (last synced: {format_timestamp(employee_timestamps[kuerzel])})", context=ctx)
|
|
emitted_count += 1
|
|
|
|
log_operation('info', f"Calendar Sync All: Completed, emitted {emitted_count} events", context=ctx)
|
|
return {
|
|
'status': 'completed',
|
|
'triggered_by': triggered_by,
|
|
'emitted_count': emitted_count
|
|
}
|
|
|
|
except Exception as e:
|
|
log_operation('error', f"Fehler beim All-Sync: {e}", context=ctx)
|
|
return {
|
|
'status': 'error',
|
|
'error': str(e)
|
|
}
|