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

Make workflow instance.status() return output equal to production workflows #7575

Merged
merged 6 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 6 additions & 0 deletions .changeset/great-flowers-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@cloudflare/workflows-shared": patch
"miniflare": patch
---

Make `Instance.status()` return type the same as production
4 changes: 2 additions & 2 deletions fixtures/workflow-multiple/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Demo extends WorkflowEntrypoint<{}, Params> {
};
});

return [result, result2, timestamp, payload, "workflow1"];
return "i'm workflow1";
}
}

Expand All @@ -53,7 +53,7 @@ export class Demo2 extends WorkflowEntrypoint<{}, Params> {
};
});

return [result, result2, timestamp, payload, "workflow2"];
return "i'm workflow2";
}
}

Expand Down
12 changes: 8 additions & 4 deletions fixtures/workflow-multiple/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ describe("Workflows", () => {
id: "test",
status: {
status: "running",
output: [],
__LOCAL_DEV_STEP_OUTPUTS: [],
output: null,
},
};

Expand All @@ -65,7 +66,8 @@ describe("Workflows", () => {
id: "test",
status: {
status: "running",
output: [{ output: "First step result" }],
__LOCAL_DEV_STEP_OUTPUTS: [{ output: "First step result" }],
output: null,
},
};
await Promise.all([
Expand Down Expand Up @@ -96,10 +98,11 @@ describe("Workflows", () => {
id: "test",
status: {
status: "complete",
output: [
__LOCAL_DEV_STEP_OUTPUTS: [
{ output: "First step result" },
{ output: "workflow1" },
],
output: "i'm workflow1",
},
});
},
Expand All @@ -113,10 +116,11 @@ describe("Workflows", () => {
id: "test",
status: {
status: "complete",
output: [
__LOCAL_DEV_STEP_OUTPUTS: [
{ output: "First step result" },
{ output: "workflow2" },
],
output: "i'm workflow2",
},
});
},
Expand Down
2 changes: 1 addition & 1 deletion fixtures/workflow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Demo extends WorkflowEntrypoint<{}, Params> {
};
});

return [result, result2, timestamp, payload];
return "i'm a workflow output";
}
}

Expand Down
12 changes: 8 additions & 4 deletions fixtures/workflow/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ describe("Workflows", () => {
fetchJson(`http://${ip}:${port}/create?workflowName=test`)
).resolves.toEqual({
status: "running",
output: [],
__LOCAL_DEV_STEP_OUTPUTS: [],
output: null,
});

await vi.waitFor(
Expand All @@ -55,7 +56,8 @@ describe("Workflows", () => {
fetchJson(`http://${ip}:${port}/status?workflowName=test`)
).resolves.toEqual({
status: "running",
output: [{ output: "First step result" }],
__LOCAL_DEV_STEP_OUTPUTS: [{ output: "First step result" }],
output: null,
});
},
{ timeout: 5000 }
Expand All @@ -67,10 +69,11 @@ describe("Workflows", () => {
fetchJson(`http://${ip}:${port}/status?workflowName=test`)
).resolves.toEqual({
status: "complete",
output: [
__LOCAL_DEV_STEP_OUTPUTS: [
{ output: "First step result" },
{ output: "Second step result" },
],
output: "i'm a workflow output",
});
},
{ timeout: 5000 }
Expand All @@ -80,7 +83,8 @@ describe("Workflows", () => {
it("creates a workflow without id", async ({ expect }) => {
await expect(fetchJson(`http://${ip}:${port}/create`)).resolves.toEqual({
status: "running",
output: [],
__LOCAL_DEV_STEP_OUTPUTS: [],
output: null,
});
});

Expand Down
15 changes: 12 additions & 3 deletions packages/miniflare/test/plugins/workflows/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ test("persists Workflow data on file-system between runs", async (t) => {
t.teardown(() => mf.dispose());

let res = await mf.dispatchFetch("http://localhost");
t.is(await res.text(), '{"status":"running","output":[]}');
t.is(
await res.text(),
'{"status":"running","__LOCAL_DEV_STEP_OUTPUTS":[],"output":null}'
);

// there's no waitUntil in ava haha
const begin = performance.now();
Expand All @@ -50,7 +53,10 @@ test("persists Workflow data on file-system between runs", async (t) => {
const res = await mf.dispatchFetch("http://localhost");
console.log(test);
test = await res.text();
if (test === '{"status":"complete","output":["yes you are"]}') {
if (
test ===
'{"status":"complete","__LOCAL_DEV_STEP_OUTPUTS":["yes you are"],"output":"I\'m a output string"}'
) {
success = true;
break;
}
Expand All @@ -68,5 +74,8 @@ test("persists Workflow data on file-system between runs", async (t) => {

// state should be persisted now
res = await mf.dispatchFetch("http://localhost");
t.is(await res.text(), '{"status":"complete","output":["yes you are"]}');
t.is(
await res.text(),
'{"status":"complete","__LOCAL_DEV_STEP_OUTPUTS":["yes you are"],"output":"I\'m a output string"}'
);
});
24 changes: 19 additions & 5 deletions packages/workflows-shared/src/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,30 @@ export class WorkflowHandle extends RpcTarget implements WorkflowInstance {
throw new Error("Not implemented yet");
}

public async status(): Promise<InstanceStatus> {
public async status(): Promise<
InstanceStatus & { __LOCAL_DEV_STEP_OUTPUTS: unknown[] }
> {
const status = await this.stub.getStatus(0, this.id);
const { logs } = await this.stub.readLogs();
// @ts-expect-error TODO: Fix this
const { logs } = (await this.stub.readLogs()) as { logs: unknown[] };
LuisDuarte1 marked this conversation as resolved.
Show resolved Hide resolved

const workflowOutput = logs
.filter(
// @ts-expect-error TODO: Fix this
(log) => log.event === InstanceEvent.WORKFLOW_SUCCESS
)
.at(0);
const filteredLogs = logs.filter(
// @ts-expect-error TODO: Fix this
(log) => log.event === InstanceEvent.STEP_SUCCESS
);
// @ts-expect-error TODO: Fix this
const output = filteredLogs.map((log) => log.metadata.result);
return { status: instanceStatusName(status), output }; // output, error
const stepOutputs = filteredLogs.map((log) => log.metadata.result);
return {
status: instanceStatusName(status),
__LOCAL_DEV_STEP_OUTPUTS: stepOutputs,
output:
// @ts-expect-error TODO: Fix this
workflowOutput !== undefined ? workflowOutput.metadata.result : null,
}; // output, error
}
}
Loading