Skip to content

Commit bb17205

Browse files
bruxodasilvapenalosaSkye-31
authored
Fix workflows binding to create a workflow without arguments & miss match instance.id between wrangler local dev and production (#7225)
* fix workflows binding to create a workflow without arguments * prettify cleanup * added changeset * Update packages/workflows-shared/src/binding.ts Accepted setting default values Co-authored-by: Somhairle MacLeòid <[email protected]> * pretttify run * Update packages/workflows-shared/src/binding.ts Co-authored-by: Skye <[email protected]> * try to get instance.id * new try * pretty * - ensure .id is available on handle, syncronously * prettify * updated changeset * reverted workflows fixture * better changeset * reverted constructor sugar as per code review * added regression test to the workflows fixture test * prettify --------- Co-authored-by: Somhairle MacLeòid <[email protected]> Co-authored-by: Skye <[email protected]>
1 parent e2e6912 commit bb17205

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

.changeset/nasty-monkeys-heal.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@cloudflare/workflows-shared": patch
3+
---
4+
5+
- Fix workflows binding to create a workflow without arguments
6+
- Fix workflows instance.id not working the same way in wrangler local dev as it does in production

fixtures/workflow/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export default class extends WorkerEntrypoint<Env> {
4545

4646
let handle: WorkflowInstance;
4747
if (url.pathname === "/create") {
48-
handle = await this.env.WORKFLOW.create({ id });
48+
if (id === null) {
49+
handle = await this.env.WORKFLOW.create();
50+
} else {
51+
handle = await this.env.WORKFLOW.create({ id });
52+
}
4953
} else {
5054
handle = await this.env.WORKFLOW.get(id);
5155
}

fixtures/workflow/tests/index.test.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("Workflows", () => {
4040
}
4141
}
4242

43-
it("creates a workflow", async ({ expect }) => {
43+
it("creates a workflow with id", async ({ expect }) => {
4444
await expect(
4545
fetchJson(`http://${ip}:${port}/create?workflowName=test`)
4646
).resolves.toEqual({
@@ -75,4 +75,11 @@ describe("Workflows", () => {
7575
{ timeout: 5000 }
7676
);
7777
});
78+
79+
it("creates a workflow without id", async ({ expect }) => {
80+
await expect(fetchJson(`http://${ip}:${port}/create`)).resolves.toEqual({
81+
status: "running",
82+
output: [],
83+
});
84+
});
7885
});

packages/workflows-shared/src/binding.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ type Env = {
1414
// this.env.WORKFLOW is WorkflowBinding
1515
export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
1616
public async create({
17-
id,
18-
params,
19-
}: WorkflowInstanceCreateOptions): Promise<WorkflowInstance> {
20-
if (!id) {
21-
id = crypto.randomUUID();
22-
}
17+
id = crypto.randomUUID(),
18+
params = {},
19+
}: WorkflowInstanceCreateOptions = {}): Promise<WorkflowInstance> {
2320
const stubId = this.env.ENGINE.idFromName(id);
2421
const stub = this.env.ENGINE.get(stubId);
2522

@@ -34,13 +31,37 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
3431
}
3532
);
3633

37-
return new WorkflowHandle(id, stub);
34+
const handle = new WorkflowHandle(id, stub);
35+
return {
36+
id: id,
37+
pause: handle.pause.bind(handle),
38+
resume: handle.resume.bind(handle),
39+
terminate: handle.terminate.bind(handle),
40+
restart: handle.restart.bind(handle),
41+
status: handle.status.bind(handle),
42+
};
3843
}
3944

4045
public async get(id: string): Promise<WorkflowInstance> {
41-
const stubId = this.env.ENGINE.idFromName(id);
42-
const stub = this.env.ENGINE.get(stubId);
43-
return new WorkflowHandle(id, stub);
46+
const engineStubId = this.env.ENGINE.idFromName(id);
47+
const engineStub = this.env.ENGINE.get(engineStubId);
48+
49+
const handle = new WorkflowHandle(id, engineStub);
50+
51+
try {
52+
await handle.status();
53+
} catch (e) {
54+
throw new Error("instance.not_found");
55+
}
56+
57+
return {
58+
id: id,
59+
pause: handle.pause.bind(handle),
60+
resume: handle.resume.bind(handle),
61+
terminate: handle.terminate.bind(handle),
62+
restart: handle.restart.bind(handle),
63+
status: handle.status.bind(handle),
64+
};
4465
}
4566
}
4667

0 commit comments

Comments
 (0)