feat: Add logging utility for calendar sync operations and enhance error handling
This commit is contained in:
@@ -1056,6 +1056,11 @@ async def handler(input_data: Dict[str, Any], ctx: FlowContext) -> None:
|
||||
|
||||
return {'status': 200, 'body': {'status': 'completed', 'kuerzel': kuerzel}}
|
||||
|
||||
except Exception as e:
|
||||
log_operation('error', f"Sync failed for {kuerzel}: {e}", context=ctx)
|
||||
log_operation('info', f"Handler duration (failed): {time.time() - start_time}", context=ctx)
|
||||
return {'status': 500, 'body': {'error': str(e)}}
|
||||
|
||||
finally:
|
||||
# Always close resources to prevent memory leaks
|
||||
if service is not None:
|
||||
@@ -1068,11 +1073,6 @@ async def handler(input_data: Dict[str, Any], ctx: FlowContext) -> None:
|
||||
redis_client.close()
|
||||
except Exception as e:
|
||||
log_operation('debug', f"Error closing Redis client: {e}", context=ctx)
|
||||
|
||||
except Exception as e:
|
||||
log_operation('error', f"Sync failed for {kuerzel}: {e}", context=ctx)
|
||||
log_operation('info', f"Handler duration (failed): {time.time() - start_time}", context=ctx)
|
||||
return {'status': 500, 'body': {'error': str(e)}}
|
||||
finally:
|
||||
|
||||
# Ensure lock is always released
|
||||
clear_employee_lock(redis_client, kuerzel, ctx)
|
||||
|
||||
@@ -18,6 +18,26 @@ def get_logger(context=None):
|
||||
return get_service_logger('calendar_sync', context)
|
||||
|
||||
|
||||
def log_operation(level: str, message: str, context=None, **extra):
|
||||
"""
|
||||
Log calendar sync operations with structured context.
|
||||
|
||||
Args:
|
||||
level: Log level ('debug', 'info', 'warning', 'error')
|
||||
message: Log message
|
||||
context: FlowContext if available
|
||||
**extra: Additional key-value pairs to log
|
||||
"""
|
||||
logger = get_logger(context)
|
||||
log_func = getattr(logger, level.lower(), logger.info)
|
||||
|
||||
if extra:
|
||||
extra_str = " | " + " | ".join(f"{k}={v}" for k, v in extra.items())
|
||||
log_func(message + extra_str)
|
||||
else:
|
||||
log_func(message)
|
||||
|
||||
|
||||
async def connect_db(context=None):
|
||||
"""Connect to Postgres DB from environment variables."""
|
||||
logger = get_logger(context)
|
||||
|
||||
@@ -69,4 +69,3 @@ async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
|
||||
status=500,
|
||||
body={'error': 'Internal server error', 'details': str(e)}
|
||||
)
|
||||
)
|
||||
|
||||
@@ -26,7 +26,7 @@ async def handler(event_data: Dict[str, Any], ctx: FlowContext[Any]) -> None:
|
||||
event_data: Event payload with knowledge_id
|
||||
ctx: Motia context
|
||||
"""
|
||||
from services.config import get_redis_client
|
||||
from services.redis_client import RedisClientFactory
|
||||
from services.aiknowledge_sync_utils import AIKnowledgeSync
|
||||
|
||||
ctx.logger.info("=" * 80)
|
||||
@@ -46,7 +46,7 @@ async def handler(event_data: Dict[str, Any], ctx: FlowContext[Any]) -> None:
|
||||
ctx.logger.info("=" * 80)
|
||||
|
||||
# Get Redis for locking
|
||||
redis_client: Redis = get_redis_client(strict=False)
|
||||
redis_client = RedisClientFactory.get_client(strict=False)
|
||||
|
||||
# Initialize sync utils
|
||||
sync_utils = AIKnowledgeSync(ctx, redis_client)
|
||||
|
||||
@@ -39,7 +39,7 @@ async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
|
||||
if not knowledge_id:
|
||||
ctx.logger.error("❌ Missing entity_id in payload")
|
||||
return ApiResponse(
|
||||
status_code=400,
|
||||
status=400,
|
||||
body={'success': False, 'error': 'Missing entity_id'}
|
||||
)
|
||||
|
||||
@@ -61,13 +61,13 @@ async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
|
||||
ctx.logger.info("=" * 80)
|
||||
|
||||
return ApiResponse(
|
||||
status_code=200,
|
||||
status=200,
|
||||
body={'success': True, 'knowledge_id': knowledge_id}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
ctx.logger.error(f"❌ Webhook error: {e}")
|
||||
return ApiResponse(
|
||||
status_code=500,
|
||||
status=500,
|
||||
body={'success': False, 'error': str(e)}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user