refactor: extract common functions to utils
- Add get_redis_client() to calendar_sync_utils.py - Add get_advoware_employees() to calendar_sync_utils.py - Add set_employee_lock() and clear_employee_lock() to calendar_sync_utils.py - Update all step files to use shared utility functions - Remove duplicate code across calendar_sync_*.py files
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import logging
|
||||
import asyncpg
|
||||
import os
|
||||
import redis
|
||||
from config import Config
|
||||
from googleapiclient.discovery import build
|
||||
from google.oauth2 import service_account
|
||||
@@ -61,4 +62,45 @@ async def get_google_service(context=None):
|
||||
return service
|
||||
except Exception as e:
|
||||
log_operation('error', f"Failed to initialize Google service: {e}", context=context)
|
||||
raise
|
||||
raise
|
||||
|
||||
def get_redis_client(context=None):
|
||||
"""Initialize Redis client for calendar sync operations."""
|
||||
try:
|
||||
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
|
||||
)
|
||||
return redis_client
|
||||
except Exception as e:
|
||||
log_operation('error', f"Failed to initialize Redis client: {e}", context=context)
|
||||
raise
|
||||
|
||||
async def get_advoware_employees(advoware, context=None):
|
||||
"""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 []
|
||||
log_operation('info', f"Fetched {len(employees)} Advoware employees", context=context)
|
||||
return employees
|
||||
except Exception as e:
|
||||
log_operation('error', f"Failed to fetch Advoware employees: {e}", context=context)
|
||||
raise
|
||||
|
||||
def set_employee_lock(redis_client, kuerzel, triggered_by, context=None):
|
||||
"""Set lock for employee sync operation."""
|
||||
employee_lock_key = f'calendar_sync_lock_{kuerzel}'
|
||||
if redis_client.set(employee_lock_key, triggered_by, ex=1800, nx=True) is None:
|
||||
log_operation('info', f"Sync already active for {kuerzel}, skipping", context=context)
|
||||
return False
|
||||
return True
|
||||
|
||||
def clear_employee_lock(redis_client, kuerzel, context=None):
|
||||
"""Clear lock for employee sync operation."""
|
||||
try:
|
||||
employee_lock_key = f'calendar_sync_lock_{kuerzel}'
|
||||
redis_client.delete(employee_lock_key)
|
||||
except Exception as e:
|
||||
log_operation('warning', f"Failed to clear lock for {kuerzel}: {e}", context=context)
|
||||
Reference in New Issue
Block a user