|
| 1 | +import logging |
| 2 | +from typing import Optional, cast |
| 3 | + |
| 4 | +from sqlalchemy import select |
| 5 | +from sqlalchemy.orm import Session |
| 6 | + |
| 7 | +from core.workflow.nodes.trigger_schedule.entities import SchedulePlanUpdate |
| 8 | +from events.app_event import app_published_workflow_was_updated |
| 9 | +from extensions.ext_database import db |
| 10 | +from models import AppMode, Workflow, WorkflowSchedulePlan |
| 11 | +from services.schedule_service import ScheduleService |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +@app_published_workflow_was_updated.connect |
| 17 | +def handle(sender, **kwargs): |
| 18 | + """ |
| 19 | + Handle app published workflow update event to sync workflow_schedule_plans table. |
| 20 | +
|
| 21 | + When a workflow is published, this handler will: |
| 22 | + 1. Extract schedule trigger nodes from the workflow graph |
| 23 | + 2. Compare with existing workflow_schedule_plans records |
| 24 | + 3. Create/update/delete schedule plans as needed |
| 25 | + """ |
| 26 | + app = sender |
| 27 | + if app.mode != AppMode.WORKFLOW.value: |
| 28 | + return |
| 29 | + |
| 30 | + published_workflow = kwargs.get("published_workflow") |
| 31 | + published_workflow = cast(Workflow, published_workflow) |
| 32 | + |
| 33 | + sync_schedule_from_workflow(tenant_id=app.tenant_id, app_id=app.id, workflow=published_workflow) |
| 34 | + |
| 35 | + |
| 36 | +def sync_schedule_from_workflow(tenant_id: str, app_id: str, workflow: Workflow) -> Optional[WorkflowSchedulePlan]: |
| 37 | + """ |
| 38 | + Sync schedule plan from workflow graph configuration. |
| 39 | +
|
| 40 | + Args: |
| 41 | + tenant_id: Tenant ID |
| 42 | + app_id: App ID |
| 43 | + workflow: Published workflow instance |
| 44 | +
|
| 45 | + Returns: |
| 46 | + Updated or created WorkflowSchedulePlan, or None if no schedule node |
| 47 | + """ |
| 48 | + with Session(db.engine) as session: |
| 49 | + schedule_config = ScheduleService.extract_schedule_config(workflow) |
| 50 | + |
| 51 | + existing_plan = session.scalar( |
| 52 | + select(WorkflowSchedulePlan).where( |
| 53 | + WorkflowSchedulePlan.tenant_id == tenant_id, |
| 54 | + WorkflowSchedulePlan.app_id == app_id, |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + if not schedule_config: |
| 59 | + if existing_plan: |
| 60 | + logger.info("No schedule node in workflow for app %s, removing schedule plan", app_id) |
| 61 | + ScheduleService.delete_schedule(session=session, schedule_id=existing_plan.id) |
| 62 | + session.commit() |
| 63 | + return None |
| 64 | + |
| 65 | + if existing_plan: |
| 66 | + updates = SchedulePlanUpdate( |
| 67 | + node_id=schedule_config.node_id, |
| 68 | + cron_expression=schedule_config.cron_expression, |
| 69 | + timezone=schedule_config.timezone, |
| 70 | + ) |
| 71 | + updated_plan = ScheduleService.update_schedule( |
| 72 | + session=session, |
| 73 | + schedule_id=existing_plan.id, |
| 74 | + updates=updates, |
| 75 | + ) |
| 76 | + session.commit() |
| 77 | + return updated_plan |
| 78 | + else: |
| 79 | + new_plan = ScheduleService.create_schedule( |
| 80 | + session=session, |
| 81 | + tenant_id=tenant_id, |
| 82 | + app_id=app_id, |
| 83 | + config=schedule_config, |
| 84 | + ) |
| 85 | + session.commit() |
| 86 | + return new_plan |
0 commit comments