Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore wf canceled error in partial parallel execution #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/context/auto-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LazyCallStep, type BaseLazyStep } from "./steps";
import { getHeaders } from "../workflow-requests";
import type { WorkflowLogger } from "../logger";
import { NO_CONCURRENCY } from "../constants";
import { QstashError } from "@upstash/qstash";

export class AutoExecutor {
private context: WorkflowContext;
Expand Down Expand Up @@ -218,7 +219,10 @@ export class AutoExecutor {
);
await this.submitStepsToQStash([resultStep], [parallelStep]);
} catch (error) {
if (error instanceof WorkflowAbort) {
if (
error instanceof WorkflowAbort ||
(error instanceof QstashError && error.status === 400)
) {
throw error;
}
throw new WorkflowError(
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export type WorkflowServeOptions<
failureFunction?: (failureData: {
context: Omit<
WorkflowContext<TInitialPayload>,
"run" | "sleepUntil" | "sleep" | "call" | "waitForEvent" | "notify"
"run" | "sleepUntil" | "sleep" | "call" | "waitForEvent" | "notify" | "cancel" | "api"
>;
failStatus: number;
failResponse: string;
Expand Down
66 changes: 66 additions & 0 deletions src/workflow-requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,72 @@ describe("Workflow Requests", () => {
}
);

test(
"should omit if triggerRouteFunction (with partial parallel step execution) gets can't publish to canceled workflow error",
async () => {
const workflowRunId = `wfr-${nanoid()}`;
const context = new WorkflowContext({
qstashClient,
workflowRunId: workflowRunId,
initialPayload: undefined,
headers: new Headers({}) as Headers,
steps: [
{
stepId: 0,
concurrent: 1,
stepName: "init",
stepType: "Initial",
targetStep: 1,
},
{
stepId: 0,
concurrent: 2,
stepName: "sleeping",
stepType: "SleepFor",
targetStep: 1,
},
],
url: WORKFLOW_ENDPOINT,
});

const debug = new WorkflowLogger({ logLevel: "INFO", logOutput: "console" });
const spy = spyOn(debug, "log");

await triggerFirstInvocation(context, 3, false, debug);
expect(spy).toHaveBeenCalledTimes(1);

await workflowClient.cancel({ ids: [workflowRunId] });

const result = await triggerRouteFunction({
onStep: async () => {
await Promise.all([context.sleep("sleeping", 10), context.sleep("sleeping", 10)]);
},
onCleanup: async () => {
throw new Error("shouldn't come here.");
},
onCancel: async () => {
throw new Error("shouldn't come here.");
},
debug,
});

expect(result.isOk()).toBeTrue();
// @ts-expect-error value will be set since stepFinish isOk
expect(result.value).toBe("workflow-was-finished");

expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenLastCalledWith("WARN", "RESPONSE_WORKFLOW", {
message: "tried to append to a cancelled workflow. exiting without publishing.",
name: "QstashError",
errorMessage:
'[{"error":"failed to publish to url: can not append to a a cancelled workflow"}]',
});
},
{
timeout: 10000,
}
);

test(
"should omit the error if the workflow is created with the same id",
async () => {
Expand Down
Loading