Skip to content

Commit

Permalink
Fix workflows binding to create a workflow without arguments & miss m…
Browse files Browse the repository at this point in the history
…atch 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]>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent e2e6912 commit bb17205
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-monkeys-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@cloudflare/workflows-shared": patch
---

- Fix workflows binding to create a workflow without arguments
- Fix workflows instance.id not working the same way in wrangler local dev as it does in production
6 changes: 5 additions & 1 deletion fixtures/workflow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export default class extends WorkerEntrypoint<Env> {

let handle: WorkflowInstance;
if (url.pathname === "/create") {
handle = await this.env.WORKFLOW.create({ id });
if (id === null) {
handle = await this.env.WORKFLOW.create();
} else {
handle = await this.env.WORKFLOW.create({ id });
}
} else {
handle = await this.env.WORKFLOW.get(id);
}
Expand Down
9 changes: 8 additions & 1 deletion fixtures/workflow/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Workflows", () => {
}
}

it("creates a workflow", async ({ expect }) => {
it("creates a workflow with id", async ({ expect }) => {
await expect(
fetchJson(`http://${ip}:${port}/create?workflowName=test`)
).resolves.toEqual({
Expand Down Expand Up @@ -75,4 +75,11 @@ describe("Workflows", () => {
{ timeout: 5000 }
);
});

it("creates a workflow without id", async ({ expect }) => {
await expect(fetchJson(`http://${ip}:${port}/create`)).resolves.toEqual({
status: "running",
output: [],
});
});
});
41 changes: 31 additions & 10 deletions packages/workflows-shared/src/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ type Env = {
// this.env.WORKFLOW is WorkflowBinding
export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
public async create({
id,
params,
}: WorkflowInstanceCreateOptions): Promise<WorkflowInstance> {
if (!id) {
id = crypto.randomUUID();
}
id = crypto.randomUUID(),
params = {},
}: WorkflowInstanceCreateOptions = {}): Promise<WorkflowInstance> {
const stubId = this.env.ENGINE.idFromName(id);
const stub = this.env.ENGINE.get(stubId);

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

return new WorkflowHandle(id, stub);
const handle = new WorkflowHandle(id, stub);
return {
id: id,
pause: handle.pause.bind(handle),
resume: handle.resume.bind(handle),
terminate: handle.terminate.bind(handle),
restart: handle.restart.bind(handle),
status: handle.status.bind(handle),
};
}

public async get(id: string): Promise<WorkflowInstance> {
const stubId = this.env.ENGINE.idFromName(id);
const stub = this.env.ENGINE.get(stubId);
return new WorkflowHandle(id, stub);
const engineStubId = this.env.ENGINE.idFromName(id);
const engineStub = this.env.ENGINE.get(engineStubId);

const handle = new WorkflowHandle(id, engineStub);

try {
await handle.status();
} catch (e) {
throw new Error("instance.not_found");
}

return {
id: id,
pause: handle.pause.bind(handle),
resume: handle.resume.bind(handle),
terminate: handle.terminate.bind(handle),
restart: handle.restart.bind(handle),
status: handle.status.bind(handle),
};
}
}

Expand Down

0 comments on commit bb17205

Please sign in to comment.