fix(): Workflow cancellation + gracefully handle non serializable state (#10674)

FIXES FRMW-2852

**What**
A workflow distributed transaction expect any response and error to be serializable. When it is not the case, the distributed transaction might fail during the save checkpoint that occurs for async steps. This can lead to unexpected behaviour.

With this pr, we introduce a way to handle non serialazable object in a more sustainable manner, this means the following:

- If a workflow throw any non serialazable error (e.g AWS error that contains full IncomingMessage object that related to network communication, think of req/res) then we identify that this object is not serialzable and we clean up the object to make it serializable without loosing the main information, add a new error to the workflow to informed of this issue and can be handled by the user.
- If a response is not serializable (which should not happen at this point because it is handled before by the value resolver), in that case, we wont be able to reuse that response to continue the workflow which means that the workflow is in a non runnable state. In that case we throw a specific error stating that a non serializable context is being provided

**second what**
This pr refactor the `runAsStep` to add better support for workflow cancelation, especially async ones
This commit is contained in:
Adrien de Peretti
2025-01-05 14:30:17 +01:00
committed by GitHub
parent 152a94e1e1
commit 7d8f6cf39f
9 changed files with 343 additions and 16 deletions

View File

@@ -4,3 +4,4 @@ export * from "./workflow_async"
export * from "./workflow_step_timeout"
export * from "./workflow_transaction_timeout"
export * from "./workflow_when"
export * from "./workflow_async_compensate"

View File

@@ -0,0 +1,51 @@
import {
createStep,
createWorkflow,
parallelize,
StepResponse,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
const step_1_background = createStep(
{
name: "step_1_background_fail",
async: true,
},
jest.fn(async (input) => {
return new StepResponse(input)
})
)
const nestedWorkflow = createWorkflow(
{
name: "nested_sub_flow_async_fail",
},
function (input) {
const resp = step_1_background(input)
return resp
}
)
const step_2 = createStep(
{
name: "step_2_fail",
},
jest.fn(async () => {
throw new Error("step_2_fail")
})
)
createWorkflow(
{
name: "workflow_async_background_fail",
},
function (input) {
const ret = nestedWorkflow.runAsStep({
input,
})
step_2()
return new WorkflowResponse(ret)
}
)

View File

@@ -1,5 +1,6 @@
import {
DistributedTransactionType,
TransactionStep,
TransactionStepTimeoutError,
TransactionTimeoutError,
WorkflowManager,
@@ -473,6 +474,44 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
failTrap(done)
})
it("should cancel an async sub workflow when compensating", (done) => {
const workflowId = "workflow_async_background_fail"
void workflowOrcModule.run(workflowId, {
input: {
callSubFlow: true,
},
transactionId: "trx_123_compensate_async_sub_workflow",
throwOnError: false,
logOnError: false,
})
let onCompensateStepSuccess: { step: TransactionStep } | null = null
void workflowOrcModule.subscribe({
workflowId,
subscriber: (event) => {
if (event.eventType === "onCompensateStepSuccess") {
onCompensateStepSuccess = event
}
if (event.eventType === "onFinish") {
expect(onCompensateStepSuccess).toBeDefined()
expect(onCompensateStepSuccess!.step.id).toEqual(
"_root.nested_sub_flow_async_fail-as-step" // The workflow as step
)
expect(onCompensateStepSuccess!.step.compensate).toEqual({
state: "reverted",
status: "ok",
})
done()
}
},
})
failTrap(done)
})
})
// Note: These tests depend on actual Redis instance and waiting for the scheduled jobs to run, which isn't great.