mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-31 19:45:15 +02:00
add cron-based schedule trigger
This commit is contained in:
parent
f08b316441
commit
3b1d7c4389
4 changed files with 105 additions and 2 deletions
|
|
@ -0,0 +1,48 @@
|
|||
"""Schedule dispatch adapter: load + guard, then call generic dispatch."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.automations.dispatch import DispatchError, dispatch_run
|
||||
from app.automations.persistence.enums.automation_status import AutomationStatus
|
||||
from app.automations.persistence.models.automation import Automation
|
||||
from app.automations.persistence.models.run import AutomationRun
|
||||
from app.automations.persistence.models.trigger import AutomationTrigger
|
||||
|
||||
|
||||
async def dispatch_schedule_run(
|
||||
*,
|
||||
session: AsyncSession,
|
||||
trigger: AutomationTrigger,
|
||||
) -> AutomationRun:
|
||||
"""Fire one scheduled run for ``trigger``.
|
||||
|
||||
The caller (the schedule tick) is responsible for selecting due triggers
|
||||
and advancing ``next_fire_at`` / ``last_fired_at`` before invoking this.
|
||||
"""
|
||||
automation = await _load_automation(session, trigger.automation_id)
|
||||
if automation is None:
|
||||
raise DispatchError(
|
||||
f"automation {trigger.automation_id} not found for trigger {trigger.id}"
|
||||
)
|
||||
|
||||
if automation.status != AutomationStatus.ACTIVE:
|
||||
raise DispatchError(
|
||||
f"automation {trigger.automation_id} is {automation.status.value}, not active"
|
||||
)
|
||||
|
||||
return await dispatch_run(
|
||||
session=session,
|
||||
automation=automation,
|
||||
trigger=trigger,
|
||||
payload=None,
|
||||
)
|
||||
|
||||
|
||||
async def _load_automation(
|
||||
session: AsyncSession, automation_id: int
|
||||
) -> Automation | None:
|
||||
stmt = select(Automation).where(Automation.id == automation_id)
|
||||
return (await session.execute(stmt)).scalar_one_or_none()
|
||||
Loading…
Add table
Add a link
Reference in a new issue