86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
"""Schedule configuration for cleanup workflow."""
|
|
|
|
import os
|
|
from datetime import timedelta
|
|
|
|
from sientia_do.observability.logger import Logger as SientiaLogger
|
|
from temporalio.client import (
|
|
Client,
|
|
Schedule,
|
|
ScheduleActionStartWorkflow,
|
|
ScheduleSpec,
|
|
)
|
|
|
|
# Schedule configuration from environment variables
|
|
SCHEDULE_ID = os.getenv('CLEANUP_SCHEDULE_ID', 'cleanup-files-daily')
|
|
CLEANUP_CRON = os.getenv('CLEANUP_CRON', '0 0 * * *') # Default: midnight UTC
|
|
CLEANUP_TIMEZONE = os.getenv('CLEANUP_TIMEZONE', 'UTC')
|
|
CLEANUP_TASK_QUEUE = os.getenv('CLEANUP_TASK_QUEUE', 'cleanup_queue')
|
|
CLEANUP_EXECUTION_TIMEOUT_HOURS = int(os.getenv('CLEANUP_EXECUTION_TIMEOUT_HOURS', '1'))
|
|
|
|
|
|
async def schedule_exists(
|
|
client: Client, schedule_id: str, logger: SientiaLogger, metadata: dict[str, str | None]
|
|
) -> bool:
|
|
"""
|
|
Check if a schedule already exists.
|
|
|
|
Args:
|
|
client: Temporal client instance
|
|
schedule_id: ID of the schedule to check
|
|
|
|
Returns:
|
|
True if schedule exists, False otherwise
|
|
"""
|
|
try:
|
|
async for schedule in await client.list_schedules():
|
|
if schedule.id == schedule_id:
|
|
return True
|
|
return False
|
|
except Exception as e: # noqa: BLE001
|
|
logger.custom_error(f'Error checking if schedule exists: {e}', metadata)
|
|
return False
|
|
|
|
|
|
async def create_cleanup_schedule(
|
|
client: Client, logger: SientiaLogger, metadata: dict[str, str | None]
|
|
) -> None:
|
|
"""
|
|
Create or update the cleanup files schedule.
|
|
|
|
This function is idempotent and can be called multiple times safely.
|
|
It will only create the schedule if it doesn't already exist.
|
|
|
|
Args:
|
|
client: Temporal client instance
|
|
"""
|
|
# Check if schedule already exists
|
|
if await schedule_exists(client, SCHEDULE_ID, logger, metadata):
|
|
logger.custom_info(
|
|
f"Schedule '{SCHEDULE_ID}' already configured, skipping creation", metadata
|
|
)
|
|
return
|
|
|
|
await client.create_schedule(
|
|
SCHEDULE_ID,
|
|
Schedule(
|
|
action=ScheduleActionStartWorkflow(
|
|
'cleanup_files',
|
|
{}, # Empty input, will use default bucket from environment
|
|
id=f'cleanup-files-scheduled-{SCHEDULE_ID}',
|
|
task_queue=CLEANUP_TASK_QUEUE,
|
|
execution_timeout=timedelta(hours=CLEANUP_EXECUTION_TIMEOUT_HOURS),
|
|
),
|
|
spec=ScheduleSpec(
|
|
cron_expressions=[CLEANUP_CRON],
|
|
time_zone_name=CLEANUP_TIMEZONE,
|
|
),
|
|
),
|
|
)
|
|
|
|
logger.custom_info(
|
|
f"Schedule '{SCHEDULE_ID}' created successfully. "
|
|
f'Cleanup will run at: {CLEANUP_CRON} ({CLEANUP_TIMEZONE})',
|
|
metadata,
|
|
)
|