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:
root
2025-10-25 09:21:45 +00:00
parent e4bf21e676
commit b18e770f12
4 changed files with 53 additions and 49 deletions

View File

@@ -14,7 +14,7 @@ import time
import random
from config import Config # Assuming Config has POSTGRES_HOST='localhost', USER, PASSWORD, DB_NAME, GOOGLE_CALENDAR_SERVICE_ACCOUNT_PATH, GOOGLE_CALENDAR_SCOPES, etc.
from services.advoware import AdvowareAPI # Assuming this is the existing wrapper for Advoware API calls
from .calendar_sync_utils import connect_db, get_google_service, log_operation
from .calendar_sync_utils import connect_db, get_google_service, log_operation, get_redis_client, get_advoware_employees, set_employee_lock, clear_employee_lock
# Setup logging
logger = logging.getLogger(__name__)
@@ -617,17 +617,6 @@ async def safe_advoware_operation(operation, write_allowed, context=None, *args,
return None
return await operation(*args, **kwargs)
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
async def get_advoware_timestamp(advoware, frnr, context=None):
"""Fetch the last modified timestamp for an Advoware appointment."""
try:
@@ -939,12 +928,7 @@ async def handler(event_data, context):
log_operation('info', f"Starting calendar sync for employee {kuerzel}", context=context)
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
)
redis_client = get_redis_client(context)
try:
@@ -1048,10 +1032,7 @@ async def handler(event_data, context):
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
clear_employee_lock(redis_client, kuerzel, context)
# Motia Step Configuration
config = {