mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-31 19:45:15 +02:00
refactor(automations): move agent_task to builtin and restructure dispatch
This commit is contained in:
parent
f356e304e8
commit
30fff9e52f
22 changed files with 142 additions and 133 deletions
|
|
@ -3,7 +3,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from .errors import DispatchError
|
||||
from .run import dispatch_run
|
||||
from .start import start_run
|
||||
from .launch import launch_run
|
||||
|
||||
__all__ = ["DispatchError", "dispatch_run", "start_run"]
|
||||
__all__ = ["DispatchError", "launch_run"]
|
||||
|
|
|
|||
43
surfsense_backend/app/automations/dispatch/inputs.py
Normal file
43
surfsense_backend/app/automations/dispatch/inputs.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
"""Merge and validate the inputs a run starts with."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import jsonschema
|
||||
|
||||
from app.automations.persistence.models.trigger import AutomationTrigger
|
||||
from app.automations.schemas.definition.envelope import AutomationDefinition
|
||||
|
||||
from .errors import DispatchError
|
||||
|
||||
|
||||
def prepare_inputs(
|
||||
definition: AutomationDefinition,
|
||||
trigger: AutomationTrigger,
|
||||
runtime_inputs: dict[str, Any] | None,
|
||||
) -> dict[str, Any]:
|
||||
"""Merge ``trigger.static_inputs`` over ``runtime_inputs``, then validate.
|
||||
|
||||
Static inputs win on key collision.
|
||||
"""
|
||||
merged = {**(runtime_inputs or {}), **(trigger.static_inputs or {})}
|
||||
return validate_inputs(definition, merged)
|
||||
|
||||
|
||||
def validate_inputs(
|
||||
definition: AutomationDefinition, inputs: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
"""Validate ``inputs`` against the definition's optional declared schema.
|
||||
|
||||
No declared schema → pass through unchanged so runtime keys (``fired_at``,
|
||||
``last_fired_at``, ...) still reach the template context. A declared schema
|
||||
that the inputs violate is surfaced as ``DispatchError``.
|
||||
"""
|
||||
if definition.inputs is None or not definition.inputs.schema_:
|
||||
return inputs
|
||||
try:
|
||||
jsonschema.validate(instance=inputs, schema=definition.inputs.schema_)
|
||||
except jsonschema.ValidationError as exc:
|
||||
raise DispatchError(f"inputs: {exc.message}") from exc
|
||||
return inputs
|
||||
60
surfsense_backend/app/automations/dispatch/launch.py
Normal file
60
surfsense_backend/app/automations/dispatch/launch.py
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
"""Launch a run for a trigger that fired: resolve, validate, persist, enqueue.
|
||||
|
||||
The trigger-facing entry every selector calls. A selector builds the runtime
|
||||
inputs and hands one trigger row here; this resolves and guards its automation,
|
||||
snapshots the definition onto a PENDING run, and enqueues execution. The
|
||||
snapshot makes the run immune to later edits of the automation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.automations.persistence.enums.run_status import RunStatus
|
||||
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
|
||||
|
||||
from .errors import DispatchError
|
||||
from .inputs import prepare_inputs
|
||||
from .resolve import resolve_active_automation
|
||||
|
||||
|
||||
async def launch_run(
|
||||
*,
|
||||
session: AsyncSession,
|
||||
trigger: AutomationTrigger,
|
||||
runtime_inputs: dict[str, Any] | None = None,
|
||||
) -> AutomationRun:
|
||||
"""Resolve ``trigger``'s active automation and enqueue a PENDING run for it."""
|
||||
automation = await resolve_active_automation(session, trigger)
|
||||
|
||||
try:
|
||||
definition = AutomationDefinition.model_validate(automation.definition)
|
||||
except Exception as exc:
|
||||
raise DispatchError(f"invalid automation definition: {exc}") from exc
|
||||
|
||||
inputs = prepare_inputs(definition, trigger, runtime_inputs)
|
||||
snapshot = definition.model_dump(mode="json", by_alias=True)
|
||||
|
||||
run = AutomationRun(
|
||||
automation_id=automation.id,
|
||||
trigger_id=trigger.id,
|
||||
status=RunStatus.PENDING,
|
||||
definition_snapshot=snapshot,
|
||||
inputs=inputs,
|
||||
step_results=[],
|
||||
artifacts=[],
|
||||
)
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
await session.refresh(run)
|
||||
|
||||
automation_run_execute.apply_async(
|
||||
args=[run.id],
|
||||
time_limit=definition.execution.timeout_seconds,
|
||||
)
|
||||
return run
|
||||
|
|
@ -1,33 +1,24 @@
|
|||
"""Start one run for a trigger: resolve its automation, guard ``ACTIVE``, dispatch.
|
||||
|
||||
Shared by every trigger type. A type's selector builds the runtime inputs and
|
||||
hands one trigger row here; this resolves and guards the automation, then calls
|
||||
the generic ``dispatch_run``.
|
||||
"""
|
||||
"""Resolve the automation behind a trigger and guard that it may run."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
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
|
||||
|
||||
from .errors import DispatchError
|
||||
from .run import dispatch_run
|
||||
|
||||
|
||||
async def start_run(
|
||||
*,
|
||||
session: AsyncSession,
|
||||
trigger: AutomationTrigger,
|
||||
runtime_inputs: dict[str, Any] | None = None,
|
||||
) -> AutomationRun:
|
||||
"""Resolve ``trigger``'s automation, require it ``ACTIVE``, dispatch a run."""
|
||||
async def resolve_active_automation(
|
||||
session: AsyncSession, trigger: AutomationTrigger
|
||||
) -> Automation:
|
||||
"""Load ``trigger``'s automation and require it ``ACTIVE``.
|
||||
|
||||
Raises ``DispatchError`` if the automation is missing or not active.
|
||||
"""
|
||||
automation = await _load_automation(session, trigger.automation_id)
|
||||
if automation is None:
|
||||
raise DispatchError(
|
||||
|
|
@ -39,12 +30,7 @@ async def start_run(
|
|||
f"automation {trigger.automation_id} is {automation.status.value}, not active"
|
||||
)
|
||||
|
||||
return await dispatch_run(
|
||||
session=session,
|
||||
automation=automation,
|
||||
trigger=trigger,
|
||||
runtime_inputs=runtime_inputs,
|
||||
)
|
||||
return automation
|
||||
|
||||
|
||||
async def _load_automation(
|
||||
|
|
@ -1,83 +0,0 @@
|
|||
"""Generic run dispatch: validate, snapshot, persist, enqueue. Shared by every trigger."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import jsonschema
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.automations.persistence.enums.run_status import RunStatus
|
||||
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
|
||||
|
||||
from .errors import DispatchError
|
||||
|
||||
|
||||
async def dispatch_run(
|
||||
*,
|
||||
session: AsyncSession,
|
||||
automation: Automation,
|
||||
trigger: AutomationTrigger,
|
||||
runtime_inputs: dict[str, Any] | None = None,
|
||||
) -> AutomationRun:
|
||||
"""Validate, snapshot the definition, persist an ``AutomationRun``, enqueue execution.
|
||||
|
||||
Final inputs = ``trigger.static_inputs`` merged with ``runtime_inputs``,
|
||||
static winning on key collision. The merged dict is validated against
|
||||
``automation.definition.inputs.schema_`` and stored on the run.
|
||||
|
||||
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
|
||||
|
||||
merged_inputs = {**(runtime_inputs or {}), **(trigger.static_inputs or {})}
|
||||
validated_inputs = _validate_inputs(definition, merged_inputs)
|
||||
snapshot = definition.model_dump(mode="json", by_alias=True)
|
||||
|
||||
run = AutomationRun(
|
||||
automation_id=automation.id,
|
||||
trigger_id=trigger.id,
|
||||
status=RunStatus.PENDING,
|
||||
definition_snapshot=snapshot,
|
||||
inputs=validated_inputs,
|
||||
step_results=[],
|
||||
artifacts=[],
|
||||
)
|
||||
session.add(run)
|
||||
await session.commit()
|
||||
await session.refresh(run)
|
||||
|
||||
automation_run_execute.apply_async(
|
||||
args=[run.id],
|
||||
time_limit=definition.execution.timeout_seconds,
|
||||
)
|
||||
return run
|
||||
|
||||
|
||||
def _validate_inputs(
|
||||
definition: AutomationDefinition, inputs: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
"""Validate merged inputs against the optional declared schema.
|
||||
|
||||
No declared schema → pass through (runtime inputs like ``fired_at`` /
|
||||
``last_fired_at`` and trigger ``static_inputs`` must still reach the
|
||||
template context). Returning ``{}`` here strips them and makes Jinja
|
||||
blow up on any ``{{ inputs.* }}`` reference.
|
||||
"""
|
||||
if definition.inputs is None or not definition.inputs.schema_:
|
||||
return inputs
|
||||
try:
|
||||
jsonschema.validate(instance=inputs, schema=definition.inputs.schema_)
|
||||
except jsonschema.ValidationError as exc:
|
||||
raise DispatchError(f"inputs: {exc.message}") from exc
|
||||
return inputs
|
||||
Loading…
Add table
Add a link
Reference in a new issue