Skip to content

Commit

Permalink
feat(engine): Add workflow error handler to schema and API (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
daryllimyt authored Jan 2, 2025
1 parent 5fffb7b commit ba114d7
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 0 deletions.
34 changes: 34 additions & 0 deletions alembic/versions/f2e840c9fb2c_add_workflow_error_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Add workflow error handler
Revision ID: f2e840c9fb2c
Revises: 8be6393b2ee0
Create Date: 2024-12-31 15:39:57.045107
"""
from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel.sql.sqltypes

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "f2e840c9fb2c"
down_revision: str | None = "8be6393b2ee0"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"workflow",
sa.Column("error_handler", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("workflow", "error_handler")
# ### end Alembic commands ###
45 changes: 45 additions & 0 deletions frontend/src/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,18 @@ export const $DSLInput = {
],
title: 'Returns',
description: 'The action ref or value to return.'
},
error_handler: {
anyOf: [
{
type: 'string'
},
{
type: 'null'
}
],
title: 'Error Handler',
description: 'The action ref to handle errors.'
}
},
type: 'object',
Expand Down Expand Up @@ -3672,6 +3684,17 @@ export const $WorkflowRead = {
}
],
title: 'Alias'
},
error_handler: {
anyOf: [
{
type: 'string'
},
{
type: 'null'
}
],
title: 'Error Handler'
}
},
type: 'object',
Expand Down Expand Up @@ -3754,6 +3777,17 @@ export const $WorkflowReadMinimal = {
}
],
title: 'Alias'
},
error_handler: {
anyOf: [
{
type: 'string'
},
{
type: 'null'
}
],
title: 'Error Handler'
}
},
type: 'object',
Expand Down Expand Up @@ -3908,6 +3942,17 @@ export const $WorkflowUpdate = {
}
],
title: 'Alias'
},
error_handler: {
anyOf: [
{
type: 'string'
},
{
type: 'null'
}
],
title: 'Error Handler'
}
},
type: 'object',
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ export type DSLInput = {
* The action ref or value to return.
*/
returns?: unknown | null;
/**
* The action ref to handle errors.
*/
error_handler?: string | null;
};

export type DSLRunArgs = {
Expand Down Expand Up @@ -1128,6 +1132,7 @@ export type WorkflowRead = {
returns: unknown;
config: DSLConfig_Output | null;
alias?: string | null;
error_handler?: string | null;
};

export type WorkflowReadMinimal = {
Expand All @@ -1141,6 +1146,7 @@ export type WorkflowReadMinimal = {
version: number | null;
tags?: Array<TagRead> | null;
alias?: string | null;
error_handler?: string | null;
};

export type WorkflowTagCreate = {
Expand All @@ -1166,6 +1172,7 @@ export type WorkflowUpdate = {
returns?: unknown | null;
config?: DSLConfig_Input | null;
alias?: string | null;
error_handler?: string | null;
};

export type WorkspaceMember = {
Expand Down
4 changes: 4 additions & 0 deletions tracecat/db/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ class Workflow(Resource, table=True):
alias: str | None = Field(
default=None, description="Alias for the workflow", index=True
)
error_handler: str | None = Field(
default=None,
description="Workflow alias or ID for the workflow to run when this fails.",
)
icon_url: str | None = None
# Owner
owner_id: OwnerID = Field(
Expand Down
3 changes: 3 additions & 0 deletions tracecat/dsl/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class DSLInput(BaseModel):
returns: Any | None = Field(
default=None, description="The action ref or value to return."
)
error_handler: str | None = Field(
default=None, description="The action ref to handle errors."
)

@model_validator(mode="after")
def validate_structure(self) -> Self:
Expand Down
1 change: 1 addition & 0 deletions tracecat/workflow/management/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ async def build_dsl_from_workflow(self, workflow: Workflow) -> DSLInput:
inputs=workflow.static_inputs,
config=DSLConfig(**workflow.config),
returns=workflow.returns,
error_handler=workflow.error_handler,
)

async def create_workflow_from_external_definition(
Expand Down
3 changes: 3 additions & 0 deletions tracecat/workflow/management/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WorkflowRead(BaseModel):
returns: Any
config: DSLConfig | None
alias: str | None = None
error_handler: str | None = None


class WorkflowReadMinimal(BaseModel):
Expand All @@ -48,6 +49,7 @@ class WorkflowReadMinimal(BaseModel):
version: int | None
tags: list[TagRead] | None = None
alias: str | None = None
error_handler: str | None = None


class WorkflowUpdate(BaseModel):
Expand All @@ -63,6 +65,7 @@ class WorkflowUpdate(BaseModel):
returns: Any | None = None
config: DSLConfig | None = None
alias: str | None = None
error_handler: str | None = None


class WorkflowCreate(BaseModel):
Expand Down
3 changes: 3 additions & 0 deletions tracecat/workflow/management/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async def list_workflows(
version=workflow.version,
tags=tags,
alias=workflow.alias,
error_handler=workflow.error_handler,
)
)
return res
Expand Down Expand Up @@ -165,6 +166,7 @@ async def create_workflow(
created_at=workflow.created_at,
updated_at=workflow.updated_at,
version=workflow.version,
error_handler=workflow.error_handler,
)


Expand Down Expand Up @@ -205,6 +207,7 @@ async def get_workflow(
webhook=WebhookResponse(**workflow.webhook.model_dump()),
schedules=workflow.schedules or [],
alias=workflow.alias,
error_handler=workflow.error_handler,
)


Expand Down

0 comments on commit ba114d7

Please sign in to comment.