Files
dify/api/tasks/human_input_timeout_tasks.py

267 lines
10 KiB
Python

import logging
from datetime import timedelta
from itertools import starmap
from typing import NamedTuple
from celery import shared_task
from kombu.exceptions import OperationalError as BrokerOperationalError
from sqlalchemy import and_, or_, select
from sqlalchemy.orm import sessionmaker
from configs import dify_config
from core.db.session_factory import session_factory as v2_session_factory
from core.human_input_v2.shared.values import TenantId
from core.repositories.human_input_repository import HumanInputFormSubmissionRepository
from core.workflow.nodes.human_input.enums import HumanInputFormKind, HumanInputFormStatus
from extensions.ext_database import db
from extensions.ext_storage import storage
from graphon.enums import WorkflowExecutionStatus
from libs.datetime_utils import ensure_naive_utc, naive_utc_now
from models.human_input import HumanInputForm
from models.workflow import WorkflowPause, WorkflowPauseReason, WorkflowRun
from repositories.human_input_v2.sqlalchemy_form_repository import SQLAlchemyFormExpirationRepository
from services.human_input_service import HumanInputService
from services.human_input_v2.form_service import handle_form_timeouts
from tasks.app_generate.workflow_execute_task import WORKFLOW_BASED_APP_EXECUTION_QUEUE
logger = logging.getLogger(__name__)
class _TerminalPause(NamedTuple):
workflow_run_id: str
pause_id: str
form_id: str
status: HumanInputFormStatus
node_id: str
@shared_task(name="human_input_form_timeout.check_and_resume", queue="schedule_executor")
def check_and_handle_human_input_timeouts(limit: int = 100) -> None:
"""Recover legacy terminal pauses and fan out independent v2 timeout tasks."""
_handle_legacy_human_input_timeouts(limit)
_enqueue_human_input_v2_timeouts(limit)
def _enqueue_human_input_v2_timeouts(limit: int) -> None:
"""Discover candidate runs without settling forms in the periodic task."""
now = naive_utc_now()
with v2_session_factory.create_session() as session:
runs = SQLAlchemyFormExpirationRepository(session).list_expired_runs(now=now, limit=limit)
for run in runs:
try:
handle_human_input_v2_timeout.apply_async(
kwargs={"tenant_id": run.tenant_id, "app_id": run.app_id, "workflow_run_id": run.workflow_run_id}
)
except BrokerOperationalError:
logger.exception(
"Failed to enqueue v2 timeout: tenant_id=%s app_id=%s workflow_run_id=%s",
run.tenant_id,
run.app_id,
run.workflow_run_id,
)
@shared_task(name="human_input_v2_form_timeout.handle", queue=WORKFLOW_BASED_APP_EXECUTION_QUEUE)
def handle_human_input_v2_timeout(tenant_id: str, app_id: str, workflow_run_id: str) -> None:
"""Settle one run using current persisted state, then enqueue resume if due."""
logger.info("Handling v2 timeout: tenant_id=%s app_id=%s workflow_run_id=%s", tenant_id, app_id, workflow_run_id)
handle_form_timeouts(
session_factory=v2_session_factory.get_session_maker(),
tenant_id=TenantId(tenant_id),
app_id=app_id,
workflow_run_id=workflow_run_id,
)
def _is_global_timeout(form_model: HumanInputForm, global_timeout_seconds: int, *, now) -> bool:
if global_timeout_seconds <= 0:
return False
if form_model.workflow_run_id is None:
return False
created_at = ensure_naive_utc(form_model.created_at)
global_deadline = created_at + timedelta(seconds=global_timeout_seconds)
return global_deadline <= now
def _handle_global_timeout(
*,
form_id: str,
workflow_run_id: str,
pause_id: str,
node_id: str,
session_factory: sessionmaker,
) -> bool:
now = naive_utc_now()
state_object_key: str | None = None
with session_factory() as session, session.begin():
workflow_run = session.scalar(
select(WorkflowRun).where(WorkflowRun.id == workflow_run_id).with_for_update(of=WorkflowRun)
)
if workflow_run is None or workflow_run.status != WorkflowExecutionStatus.PAUSED:
return False
pause_model = session.scalar(
select(WorkflowPause).where(
WorkflowPause.id == pause_id,
WorkflowPause.workflow_run_id == workflow_run_id,
WorkflowPause.resumed_at.is_(None),
)
)
if pause_model is None:
return False
current_expired_form = session.scalar(
select(HumanInputForm.id)
.join(
WorkflowPauseReason,
(WorkflowPauseReason.form_id == HumanInputForm.id) & (WorkflowPauseReason.pause_id == pause_model.id),
)
.where(
HumanInputForm.id == form_id,
HumanInputForm.workflow_run_id == workflow_run_id,
HumanInputForm.status == HumanInputFormStatus.EXPIRED,
)
)
if current_expired_form is None:
return False
workflow_run.status = WorkflowExecutionStatus.STOPPED
workflow_run.error = f"Human input global timeout at node {node_id}"
workflow_run.finished_at = now
workflow_run.stop_requested_at = workflow_run.stop_requested_at or now
state_object_key = pause_model.state_object_key
pause_model.resumed_at = now
session.add_all([workflow_run, pause_model])
if state_object_key is not None:
try:
storage.delete(state_object_key)
except Exception:
logger.exception("Failed to delete pause state object for workflow_run_id=%s", workflow_run_id)
return True
def _terminal_pauses(
session_factory: sessionmaker,
*,
after: tuple[str, str] | None,
limit: int,
) -> tuple[_TerminalPause, ...]:
statement = (
select(
WorkflowRun.id,
WorkflowPause.id,
HumanInputForm.id,
HumanInputForm.status,
HumanInputForm.node_id,
)
.join(HumanInputForm, HumanInputForm.workflow_run_id == WorkflowRun.id)
.join(WorkflowPause, WorkflowPause.workflow_run_id == WorkflowRun.id)
.join(
WorkflowPauseReason,
(WorkflowPauseReason.pause_id == WorkflowPause.id) & (WorkflowPauseReason.form_id == HumanInputForm.id),
)
.where(
HumanInputForm.form_kind == HumanInputFormKind.RUNTIME,
HumanInputForm.status.in_(
(
HumanInputFormStatus.SUBMITTED,
HumanInputFormStatus.TIMEOUT,
HumanInputFormStatus.EXPIRED,
)
),
WorkflowRun.status == WorkflowExecutionStatus.PAUSED,
WorkflowPause.resumed_at.is_(None),
)
.distinct()
.order_by(WorkflowRun.id.asc(), HumanInputForm.id.asc())
.limit(limit)
)
if after is not None:
after_run_id, after_form_id = after
statement = statement.where(
or_(
WorkflowRun.id > after_run_id,
and_(WorkflowRun.id == after_run_id, HumanInputForm.id > after_form_id),
)
)
with session_factory() as session:
return tuple(starmap(_TerminalPause, session.execute(statement).tuples()))
def _handle_legacy_human_input_timeouts(limit: int) -> None:
"""Mark newly expired forms and recover terminal runtime pauses."""
session_factory = sessionmaker(bind=db.engine, expire_on_commit=False)
form_repo = HumanInputFormSubmissionRepository()
service = HumanInputService(session_factory, form_repository=form_repo)
now = naive_utc_now()
global_timeout_seconds = dify_config.HUMAN_INPUT_GLOBAL_TIMEOUT_SECONDS
with session_factory() as session:
global_deadline = now - timedelta(seconds=global_timeout_seconds) if global_timeout_seconds > 0 else None
timeout_filter = HumanInputForm.expiration_time <= now
if global_deadline is not None:
timeout_filter = or_(timeout_filter, HumanInputForm.created_at <= global_deadline)
stmt = (
select(HumanInputForm)
.where(
HumanInputForm.status == HumanInputFormStatus.WAITING,
timeout_filter,
)
.order_by(HumanInputForm.id.asc())
.limit(limit)
)
expired_forms = session.scalars(stmt).all()
for form_model in expired_forms:
try:
if form_model.form_kind == HumanInputFormKind.DELIVERY_TEST:
form_repo.mark_timeout(
form_id=form_model.id,
timeout_status=HumanInputFormStatus.TIMEOUT,
reason="delivery_test_timeout",
)
continue
is_global = _is_global_timeout(form_model, global_timeout_seconds, now=now)
record = form_repo.mark_timeout(
form_id=form_model.id,
timeout_status=HumanInputFormStatus.EXPIRED if is_global else HumanInputFormStatus.TIMEOUT,
reason="global_timeout" if is_global else "node_timeout",
)
if record.workflow_run_id is None:
logger.warning(
"Timed-out form %s has no workflow_run_id; skipping resume",
record.form_id,
)
except Exception:
logger.exception(
"Failed to handle timeout for form_id=%s workflow_run_id=%s",
form_model.id,
form_model.workflow_run_id,
)
after: tuple[str, str] | None = None
while terminal_pauses := _terminal_pauses(session_factory, after=after, limit=limit):
for terminal in terminal_pauses:
try:
if terminal.status == HumanInputFormStatus.EXPIRED:
_handle_global_timeout(
form_id=terminal.form_id,
workflow_run_id=terminal.workflow_run_id,
pause_id=terminal.pause_id,
node_id=terminal.node_id,
session_factory=session_factory,
)
else:
service.enqueue_resume(terminal.workflow_run_id)
except Exception:
logger.exception(
"Failed to recover terminal human input for form_id=%s workflow_run_id=%s",
terminal.form_id,
terminal.workflow_run_id,
)
last = terminal_pauses[-1]
after = (last.workflow_run_id, last.form_id)