Fixed issue with workflow runs not completing when the run function returned undefined or null

This commit is contained in:
Eric Allam
2023-01-18 18:09:03 -08:00
parent 765e8a4816
commit 5de2a1aec2
6 changed files with 26 additions and 16 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
Fixed issue with workflow runs not completing when the run function returned undefined or null
+6 -3
View File
@@ -80,10 +80,10 @@ export async function failWorkflowRun(
}
export async function completeWorkflowRun(
output: string,
runId: string,
apiKey: string,
timestamp: string
timestamp: string,
output?: string | null
) {
const workflowRun = await findWorkflowRunScopedToApiKey(runId, apiKey);
@@ -104,6 +104,9 @@ export async function completeWorkflowRun(
},
});
const parsedOutput =
typeof output === "string" ? JSON.parse(output) : undefined;
await tx.workflowRunStep.upsert({
where: {
runId_idempotencyKey: {
@@ -115,7 +118,7 @@ export async function completeWorkflowRun(
runId,
idempotencyKey: "output",
type: "OUTPUT",
output: JSON.parse(output),
output: parsedOutput === null ? undefined : parsedOutput,
context: {},
startedAt: new Date(),
finishedAt: new Date(),
@@ -187,10 +187,10 @@ function createCommandSubscriber() {
},
WORKFLOW_RUN_COMPLETE: async (id, data, properties) => {
await completeWorkflowRun(
data.output,
properties["x-workflow-run-id"],
properties["x-api-key"],
properties["x-timestamp"]
properties["x-timestamp"],
data.output
);
return true;
@@ -81,7 +81,7 @@ export const ServerRPCSchema = {
COMPLETE_WORKFLOW_RUN: {
request: z.object({
runId: z.string(),
output: z.string(),
output: z.string().optional(),
timestamp: z.string(),
}),
response: z.boolean(),
+11 -9
View File
@@ -1,6 +1,7 @@
import { z, ZodError } from "zod";
import { createHash } from "node:crypto";
import { IConnection } from "./types";
import { Logger } from "./logger";
export const RPCMessageSchema = z.object({
id: z.string(),
@@ -45,6 +46,7 @@ export class ZodRPC<
#receiver: ReceiverSchema;
#handlers: ZodRPCHandlers<ReceiverSchema>;
#pendingCalls = new Map<string, onResponseCallback>();
#logger: Logger = new Logger("ZodRPC");
constructor(options: ZodRPCOptions<SenderSchema, ReceiverSchema>) {
this.#connection = options.connection;
@@ -73,7 +75,7 @@ export class ZodRPC<
await this.#onResponse(data);
}
} catch (err) {
console.error(err);
this.#logger.error(err);
}
}
@@ -82,14 +84,12 @@ export class ZodRPC<
await this.#handleCall(message);
} catch (callError) {
if (callError instanceof ZodError) {
console.error(
`[ZodRPC][foobar] Received invalid call:\n${JSON.stringify(
message
)}: `,
this.#logger.error(
`[ZodRPC] Received invalid call:\n${JSON.stringify(message)}: `,
callError.errors
);
} else {
console.error(
this.#logger.error(
`[ZodRPC] Error handling call:\n${JSON.stringify(message)}: `,
callError
);
@@ -102,12 +102,12 @@ export class ZodRPC<
await this.#handleResponse(message);
} catch (callError) {
if (callError instanceof ZodError) {
console.error(
this.#logger.error(
`[ZodRPC] Received invalid response\n\n${JSON.stringify(message)}: `,
callError.flatten()
);
} else {
console.error(
this.#logger.error(
`[ZodRPC] Error handling response\n\n${JSON.stringify(message)}: `,
callError
);
@@ -153,6 +153,8 @@ export class ZodRPC<
throw new Error(`There is no method for ${message.methodName}`);
}
this.#logger.debug("Received call", { message });
// struggling to get real inference here
const inputs = method.request.parse(message.data);
@@ -169,7 +171,7 @@ export class ZodRPC<
try {
await this.#connection.send(preparedResponseText);
} catch (err) {
console.error("Failed sending response", preparedResponseText, err);
this.#logger.error("Failed sending response", preparedResponseText, err);
}
return;
@@ -5,7 +5,7 @@ import { WorkflowSendRunEventPropertiesSchema } from "../sharedSchemas";
export const commands = {
WORKFLOW_RUN_COMPLETE: {
data: z.object({
output: z.string(),
output: z.string().optional(),
}),
properties: WorkflowSendRunEventPropertiesSchema,
},