SIENTIAPDE-1350: Implement scheduled cleanup workflow using Temporal schedules. Adds schedule creation to worker startup and configures environment variables.
This commit is contained in:
0
model_manager/schedules/__init__.py
Normal file
0
model_manager/schedules/__init__.py
Normal file
85
model_manager/schedules/cleanup_schedule.py
Normal file
85
model_manager/schedules/cleanup_schedule.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""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,
|
||||
)
|
||||
@@ -37,6 +37,7 @@ with workflow.unsafe.imports_passed_through():
|
||||
|
||||
from model_manager import metrics
|
||||
from model_manager.activities.activities import Activities
|
||||
from model_manager.schedules.cleanup_schedule import create_cleanup_schedule
|
||||
from model_manager.utils.connectors_config import (
|
||||
build_minio_config,
|
||||
build_mlflow_config,
|
||||
@@ -117,6 +118,14 @@ async def main():
|
||||
|
||||
logger.custom_info(f'Temporal client initialized at {host}', metadata)
|
||||
|
||||
# Create cleanup schedule (idempotent - only creates if doesn't exist)
|
||||
try:
|
||||
await create_cleanup_schedule(temporal_client, logger, metadata)
|
||||
except Exception as e: # noqa: BLE001
|
||||
logger.custom_error(f'Failed to configure cleanup schedule: {e}', metadata)
|
||||
# Don't fail the worker startup if schedule creation fails
|
||||
# The schedule can be created manually if needed
|
||||
|
||||
workers = [
|
||||
Worker(
|
||||
temporal_client,
|
||||
|
||||
Reference in New Issue
Block a user