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
40
surfsense_backend/app/automations/dispatch/resolve.py
Normal file
40
surfsense_backend/app/automations/dispatch/resolve.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
"""Resolve the automation behind a trigger and guard that it may run."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
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.trigger import AutomationTrigger
|
||||
|
||||
from .errors import DispatchError
|
||||
|
||||
|
||||
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(
|
||||
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 automation
|
||||
|
||||
|
||||
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