Refactor and enhance logging in webhook handlers and Redis client
- Translated comments and docstrings from German to English for better clarity. - Improved logging consistency across various webhook handlers for create, delete, and update operations. - Centralized logging functionality by utilizing a dedicated logger utility. - Added new enums for file and XAI sync statuses in models. - Updated Redis client factory to use a centralized logger and improved error handling. - Enhanced API responses to include more descriptive messages and status codes.
This commit is contained in:
@@ -7,7 +7,7 @@ Supports syncing a single employee or all employees.
|
||||
import sys
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
from calendar_sync_utils import get_redis_client, set_employee_lock, log_operation
|
||||
from calendar_sync_utils import get_redis_client, set_employee_lock, get_logger
|
||||
|
||||
from motia import http, ApiRequest, ApiResponse, FlowContext
|
||||
|
||||
@@ -38,10 +38,10 @@ async def handler(request: ApiRequest, ctx: FlowContext) -> ApiResponse:
|
||||
kuerzel = body.get('kuerzel')
|
||||
if not kuerzel:
|
||||
return ApiResponse(
|
||||
status=400,
|
||||
status_code=400,
|
||||
body={
|
||||
'error': 'kuerzel required',
|
||||
'message': 'Bitte kuerzel im Body angeben'
|
||||
'message': 'Please provide kuerzel in body'
|
||||
}
|
||||
)
|
||||
|
||||
@@ -49,7 +49,7 @@ async def handler(request: ApiRequest, ctx: FlowContext) -> ApiResponse:
|
||||
|
||||
if kuerzel_upper == 'ALL':
|
||||
# Emit sync-all event
|
||||
log_operation('info', "Calendar Sync API: Emitting sync-all event", context=ctx)
|
||||
ctx.logger.info("Calendar Sync API: Emitting sync-all event")
|
||||
await ctx.enqueue({
|
||||
"topic": "calendar_sync_all",
|
||||
"data": {
|
||||
@@ -57,10 +57,10 @@ async def handler(request: ApiRequest, ctx: FlowContext) -> ApiResponse:
|
||||
}
|
||||
})
|
||||
return ApiResponse(
|
||||
status=200,
|
||||
status_code=200,
|
||||
body={
|
||||
'status': 'triggered',
|
||||
'message': 'Calendar sync wurde für alle Mitarbeiter ausgelöst',
|
||||
'message': 'Calendar sync triggered for all employees',
|
||||
'triggered_by': 'api'
|
||||
}
|
||||
)
|
||||
@@ -69,9 +69,9 @@ async def handler(request: ApiRequest, ctx: FlowContext) -> ApiResponse:
|
||||
redis_client = get_redis_client(ctx)
|
||||
|
||||
if not set_employee_lock(redis_client, kuerzel_upper, 'api', ctx):
|
||||
log_operation('info', f"Calendar Sync API: Sync already active for {kuerzel_upper}, skipping", context=ctx)
|
||||
ctx.logger.info(f"Calendar Sync API: Sync already active for {kuerzel_upper}, skipping")
|
||||
return ApiResponse(
|
||||
status=409,
|
||||
status_code=409,
|
||||
body={
|
||||
'status': 'conflict',
|
||||
'message': f'Calendar sync already active for {kuerzel_upper}',
|
||||
@@ -80,7 +80,7 @@ async def handler(request: ApiRequest, ctx: FlowContext) -> ApiResponse:
|
||||
}
|
||||
)
|
||||
|
||||
log_operation('info', f"Calendar Sync API called for {kuerzel_upper}", context=ctx)
|
||||
ctx.logger.info(f"Calendar Sync API called for {kuerzel_upper}")
|
||||
|
||||
# Lock successfully set, now emit event
|
||||
await ctx.enqueue({
|
||||
@@ -92,19 +92,19 @@ async def handler(request: ApiRequest, ctx: FlowContext) -> ApiResponse:
|
||||
})
|
||||
|
||||
return ApiResponse(
|
||||
status=200,
|
||||
status_code=200,
|
||||
body={
|
||||
'status': 'triggered',
|
||||
'message': f'Calendar sync was triggered for {kuerzel_upper}',
|
||||
'message': f'Calendar sync triggered for {kuerzel_upper}',
|
||||
'kuerzel': kuerzel_upper,
|
||||
'triggered_by': 'api'
|
||||
}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
log_operation('error', f"Error in API trigger: {e}", context=ctx)
|
||||
ctx.logger.error(f"Error in API trigger: {e}")
|
||||
return ApiResponse(
|
||||
status=500,
|
||||
status_code=500,
|
||||
body={
|
||||
'error': 'Internal server error',
|
||||
'details': str(e)
|
||||
|
||||
Reference in New Issue
Block a user