mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-29 19:35:20 +02:00
refactor(automations): extract dispatch_run; move manual adapter under triggers/manual/dispatch.py
This commit is contained in:
parent
8c32455818
commit
861b91004d
6 changed files with 97 additions and 56 deletions
|
|
@ -1,8 +1,8 @@
|
|||
"""Public dispatch surface for firing automations."""
|
||||
"""Generic dispatch primitives shared across trigger types."""
|
||||
|
||||
from .manual import DispatchError, dispatch_manual_run
|
||||
from __future__ import annotations
|
||||
|
||||
__all__ = [
|
||||
"DispatchError",
|
||||
"dispatch_manual_run",
|
||||
]
|
||||
from .errors import DispatchError
|
||||
from .run import dispatch_run
|
||||
|
||||
__all__ = ["DispatchError", "dispatch_run"]
|
||||
|
|
|
|||
7
surfsense_backend/app/automations/dispatch/errors.py
Normal file
7
surfsense_backend/app/automations/dispatch/errors.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
"""Dispatch errors raised when a fire request cannot be turned into a run."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class DispatchError(Exception):
|
||||
"""A dispatch could not proceed (missing trigger, invalid inputs, ...)."""
|
||||
|
|
@ -1,59 +1,46 @@
|
|||
"""Manual ``Run now`` dispatch: validate inputs, snapshot the definition, enqueue."""
|
||||
"""Generic run dispatch: validate, snapshot, persist, enqueue. Shared by every trigger."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import jsonschema
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.automations.persistence.enums.automation_status import AutomationStatus
|
||||
from app.automations.persistence.enums.run_status import RunStatus
|
||||
from app.automations.persistence.enums.trigger_type import TriggerType
|
||||
from app.automations.persistence.models.automation import Automation
|
||||
from app.automations.persistence.models.run import AutomationRun
|
||||
from app.automations.persistence.models.trigger import AutomationTrigger
|
||||
from app.automations.schemas.definition.envelope import AutomationDefinition
|
||||
from app.automations.tasks.execute_run import automation_run_execute
|
||||
|
||||
|
||||
class DispatchError(Exception):
|
||||
"""A manual dispatch could not proceed (missing trigger, invalid inputs, ...)."""
|
||||
from .errors import DispatchError
|
||||
|
||||
|
||||
async def dispatch_manual_run(
|
||||
async def dispatch_run(
|
||||
*,
|
||||
session: AsyncSession,
|
||||
automation_id: int,
|
||||
automation: Automation,
|
||||
trigger: AutomationTrigger,
|
||||
payload: dict[str, Any] | None,
|
||||
) -> AutomationRun:
|
||||
"""Validate, snapshot, persist, and enqueue an ``AutomationRun``."""
|
||||
automation = await _load_automation(session, automation_id)
|
||||
if automation is None:
|
||||
raise DispatchError(f"automation {automation_id} not found")
|
||||
|
||||
if automation.status != AutomationStatus.ACTIVE:
|
||||
raise DispatchError(
|
||||
f"automation {automation_id} is {automation.status.value}, not active"
|
||||
)
|
||||
"""Validate, snapshot the definition, persist an ``AutomationRun``, enqueue execution.
|
||||
|
||||
Callers (trigger-specific adapters) are responsible for resolving
|
||||
``automation`` and ``trigger`` and for the trigger-side ``ACTIVE`` /
|
||||
``enabled`` guards. This function only handles what's identical across
|
||||
every trigger type.
|
||||
"""
|
||||
try:
|
||||
definition = AutomationDefinition.model_validate(automation.definition)
|
||||
except Exception as exc:
|
||||
raise DispatchError(f"invalid automation definition: {exc}") from exc
|
||||
|
||||
trigger = await _find_manual_trigger(session, automation_id)
|
||||
if trigger is None:
|
||||
raise DispatchError(
|
||||
f"automation {automation_id} has no enabled manual trigger"
|
||||
)
|
||||
|
||||
resolved_inputs = _validate_inputs(definition, payload or {})
|
||||
snapshot = definition.model_dump(mode="json", by_alias=True)
|
||||
|
||||
run = AutomationRun(
|
||||
automation_id=automation_id,
|
||||
automation_id=automation.id,
|
||||
trigger_id=trigger.id,
|
||||
status=RunStatus.PENDING,
|
||||
definition_snapshot=snapshot,
|
||||
|
|
@ -73,28 +60,6 @@ async def dispatch_manual_run(
|
|||
return run
|
||||
|
||||
|
||||
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()
|
||||
|
||||
|
||||
async def _find_manual_trigger(
|
||||
session: AsyncSession, automation_id: int
|
||||
) -> AutomationTrigger | None:
|
||||
stmt = (
|
||||
select(AutomationTrigger)
|
||||
.where(
|
||||
AutomationTrigger.automation_id == automation_id,
|
||||
AutomationTrigger.type == TriggerType.MANUAL,
|
||||
AutomationTrigger.enabled.is_(True),
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
return (await session.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
|
||||
def _validate_inputs(
|
||||
definition: AutomationDefinition, payload: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
Loading…
Add table
Add a link
Reference in a new issue