Add durable workflow triggers and waits

This commit is contained in:
2026-08-01 20:57:26 +02:00
parent 55f98d1b65
commit a1cea1d162
12 changed files with 2213 additions and 275 deletions
@@ -382,9 +382,7 @@ class WorkflowInstance(Base, TimestampMixin):
index=True,
)
definition: Mapped[WorkflowDefinition] = relationship(
back_populates="instances"
)
definition: Mapped[WorkflowDefinition] = relationship(back_populates="instances")
steps: Mapped[list["WorkflowInstanceStep"]] = relationship(
back_populates="instance",
cascade="all, delete-orphan",
@@ -518,11 +516,185 @@ class WorkflowInstanceEvent(Base):
instance: Mapped[WorkflowInstance] = relationship(back_populates="events")
class WorkflowTrigger(Base, TimestampMixin):
__tablename__ = "workflow_triggers"
__table_args__ = (
UniqueConstraint(
"definition_id",
"node_id",
name="uq_workflow_trigger_definition_node",
),
Index("ix_workflow_triggers_due", "status", "next_fire_at"),
Index(
"ix_workflow_triggers_event",
"tenant_id",
"status",
"event_type",
),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
definition_id: Mapped[str] = mapped_column(
ForeignKey("workflow_definitions.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
definition_revision_id: Mapped[str] = mapped_column(
ForeignKey("workflow_definition_revisions.id", ondelete="RESTRICT"),
nullable=False,
index=True,
)
node_id: Mapped[str] = mapped_column(String(120), nullable=False)
kind: Mapped[str] = mapped_column(String(30), nullable=False, index=True)
status: Mapped[str] = mapped_column(
String(30), default="disabled", nullable=False, index=True
)
config_: Mapped[dict[str, Any]] = mapped_column(
"config", JSON, default=dict, nullable=False
)
event_type: Mapped[str | None] = mapped_column(
String(120), nullable=True, index=True
)
next_fire_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True, index=True
)
last_fire_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True
)
last_status: Mapped[str | None] = mapped_column(String(30), nullable=True)
last_error: Mapped[str | None] = mapped_column(Text, nullable=True)
authorization_subject_kind: Mapped[str] = mapped_column(String(30), nullable=False)
authorization_account_id: Mapped[str | None] = mapped_column(
String(36), nullable=True
)
authorization_membership_id: Mapped[str | None] = mapped_column(
String(36), nullable=True
)
authorization_service_account_id: Mapped[str | None] = mapped_column(
String(36), nullable=True
)
authorization_ref: Mapped[str] = mapped_column(
String(255), nullable=False, unique=True
)
grant_scopes: Mapped[list[str]] = mapped_column(JSON, default=list, nullable=False)
created_by: Mapped[str | None] = mapped_column(
String(255), nullable=True, index=True
)
updated_by: Mapped[str | None] = mapped_column(
String(255), nullable=True, index=True
)
class WorkflowTriggerDelivery(Base, TimestampMixin):
__tablename__ = "workflow_trigger_deliveries"
__table_args__ = (
UniqueConstraint(
"trigger_id",
"source_key",
name="uq_workflow_trigger_delivery_source",
),
Index(
"ix_workflow_trigger_deliveries_queue",
"status",
"created_at",
),
Index(
"ix_workflow_trigger_deliveries_tenant_trigger",
"tenant_id",
"trigger_id",
),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
trigger_id: Mapped[str] = mapped_column(
ForeignKey("workflow_triggers.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
definition_id: Mapped[str] = mapped_column(
ForeignKey("workflow_definitions.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
definition_revision_id: Mapped[str] = mapped_column(
ForeignKey("workflow_definition_revisions.id", ondelete="RESTRICT"),
nullable=False,
index=True,
)
source_key: Mapped[str] = mapped_column(String(255), nullable=False)
invocation_kind: Mapped[str] = mapped_column(String(30), nullable=False)
status: Mapped[str] = mapped_column(
String(30), default="queued", nullable=False, index=True
)
scheduled_for: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True
)
event_: Mapped[dict[str, Any] | None] = mapped_column("event", JSON, nullable=True)
instance_id: Mapped[str | None] = mapped_column(
ForeignKey("workflow_instances.id", ondelete="SET NULL"),
nullable=True,
index=True,
)
attempts: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
error: Mapped[str | None] = mapped_column(Text, nullable=True)
class WorkflowWaitState(Base, TimestampMixin):
__tablename__ = "workflow_wait_states"
__table_args__ = (
UniqueConstraint("step_id", name="uq_workflow_wait_state_step"),
Index("ix_workflow_wait_states_due", "status", "due_at"),
Index(
"ix_workflow_wait_states_event",
"tenant_id",
"status",
"event_type",
),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
instance_id: Mapped[str] = mapped_column(
ForeignKey("workflow_instances.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
step_id: Mapped[str] = mapped_column(
ForeignKey("workflow_instance_steps.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
mode: Mapped[str] = mapped_column(String(30), nullable=False, index=True)
status: Mapped[str] = mapped_column(
String(30), default="waiting", nullable=False, index=True
)
due_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True, index=True
)
event_type: Mapped[str | None] = mapped_column(
String(120), nullable=True, index=True
)
config_: Mapped[dict[str, Any]] = mapped_column(
"config", JSON, default=dict, nullable=False
)
source_event_id: Mapped[str | None] = mapped_column(
String(128), nullable=True, index=True
)
event_: Mapped[dict[str, Any] | None] = mapped_column("event", JSON, nullable=True)
error: Mapped[str | None] = mapped_column(Text, nullable=True)
revision: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
__all__ = [
"WorkflowDefinition",
"WorkflowDefinitionRevision",
"WorkflowInstance",
"WorkflowInstanceEvent",
"WorkflowInstanceStep",
"WorkflowTrigger",
"WorkflowTriggerDelivery",
"WorkflowWaitState",
"new_uuid",
]