mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-31 19:45:15 +02:00
refactor(automations): move schedule trigger into builtin package
This commit is contained in:
parent
acd673023a
commit
f293aa6bdf
13 changed files with 14 additions and 263 deletions
|
|
@ -1,7 +1,6 @@
|
|||
"""Triggers domain: registry surface + built-in trigger packages.
|
||||
|
||||
Each trigger lives in its own subpackage (``schedule/``, ...) and
|
||||
self-registers at import time via its ``definition`` module.
|
||||
Built-in trigger types live under ``builtin/`` and self-register at import time.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
|
@ -17,4 +16,4 @@ __all__ = [
|
|||
]
|
||||
|
||||
# Built-in triggers self-register at import time.
|
||||
from . import schedule # noqa: F401
|
||||
from . import builtin # noqa: F401
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
"""Built-in trigger types — each in its own subpackage, self-registering at import."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from . import event, schedule # noqa: F401
|
||||
|
|
@ -3,14 +3,12 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from .cron import InvalidCronError, compute_next_fire_at, validate_cron
|
||||
from .dispatch import dispatch_schedule_run
|
||||
from .params import ScheduleTriggerParams
|
||||
|
||||
__all__ = [
|
||||
"InvalidCronError",
|
||||
"ScheduleTriggerParams",
|
||||
"compute_next_fire_at",
|
||||
"dispatch_schedule_run",
|
||||
"validate_cron",
|
||||
]
|
||||
|
||||
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from ..store import register_trigger
|
||||
from ..types import TriggerDefinition
|
||||
from app.automations.triggers.store import register_trigger
|
||||
from app.automations.triggers.types import TriggerDefinition
|
||||
|
||||
from .params import ScheduleTriggerParams
|
||||
|
||||
SCHEDULE_TRIGGER = TriggerDefinition(
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
"""Schedule dispatch adapter: load + guard, then call generic dispatch."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
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,
|
||||
fired_at: datetime,
|
||||
scheduled_for: datetime,
|
||||
previous_last_fired_at: datetime | None,
|
||||
) -> AutomationRun:
|
||||
"""Fire one scheduled run for ``trigger``.
|
||||
|
||||
Emits calendar context as runtime inputs:
|
||||
|
||||
- ``fired_at`` — actual fire time
|
||||
- ``scheduled_for`` — cron-derived target time for this fire
|
||||
- ``last_fired_at`` — fire time of the previous run, or null on first fire
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
runtime_inputs = {
|
||||
"fired_at": fired_at.isoformat(),
|
||||
"scheduled_for": scheduled_for.isoformat(),
|
||||
"last_fired_at": (
|
||||
previous_last_fired_at.isoformat() if previous_last_fired_at else None
|
||||
),
|
||||
}
|
||||
|
||||
return await dispatch_run(
|
||||
session=session,
|
||||
automation=automation,
|
||||
trigger=trigger,
|
||||
runtime_inputs=runtime_inputs,
|
||||
)
|
||||
|
||||
|
||||
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