Skip to content

Commit 545afe5

Browse files
authored
[Workflows] Fix Workflows failure with redirect after creation (#10813)
* Add waitUntil of init promise of create call * Add tests
1 parent a6cfc06 commit 545afe5

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

.changeset/polite-paths-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/workflows-shared": patch
3+
---
4+
5+
Workflows are now created if the Request gets redirected after creation

fixtures/workflow/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,15 @@ export default class extends WorkerEntrypoint<Env> {
146146
handle = await this.env.WORKFLOW2.get(id);
147147
} else if (url.pathname === "/get3") {
148148
handle = await this.env.WORKFLOW3.get(id);
149-
} else {
149+
} else if (url.pathname === "/createWithRedirect") {
150+
console.log("create with redirect called");
151+
handle = await this.env.WORKFLOW.create();
152+
153+
return Response.redirect(
154+
new URL(`/status?workflowName=${handle.id}`, url).toString(),
155+
302
156+
);
157+
} else if (url.pathname === "/status") {
150158
handle = await this.env.WORKFLOW.get(id);
151159
}
152160

fixtures/workflow/tests/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,23 @@ describe("Workflows", () => {
257257
);
258258
});
259259

260+
it("should create an instance after immediate redirect", async ({
261+
expect,
262+
}) => {
263+
await expect(fetchJson(`http://${ip}:${port}/createWithRedirect`)).resolves
264+
.toMatchInlineSnapshot(`
265+
{
266+
"__LOCAL_DEV_STEP_OUTPUTS": [
267+
{
268+
"output": "First step result",
269+
},
270+
],
271+
"output": null,
272+
"status": "running",
273+
}
274+
`);
275+
});
276+
260277
it("should persist instances across lifetimes", async ({ expect }) => {
261278
await fetchJson(`http://${ip}:${port}/create?workflowName=something`);
262279

packages/workflows-shared/src/binding.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
2828
const stubId = this.env.ENGINE.idFromName(id);
2929
const stub = this.env.ENGINE.get(stubId);
3030

31-
void stub.init(
31+
const initPromise = stub.init(
3232
0, // accountId: number,
3333
{} as DatabaseWorkflow, // workflow: DatabaseWorkflow,
3434
{} as DatabaseVersion, // version: DatabaseVersion,
@@ -40,6 +40,8 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
4040
}
4141
);
4242

43+
this.ctx.waitUntil(initPromise);
44+
4345
const handle = new WorkflowHandle(id, stub);
4446
return {
4547
id: id,
@@ -55,7 +57,6 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
5557
public async get(id: string): Promise<WorkflowInstance> {
5658
const engineStubId = this.env.ENGINE.idFromName(id);
5759
const engineStub = this.env.ENGINE.get(engineStubId);
58-
5960
const handle = new WorkflowHandle(id, engineStub);
6061

6162
try {

0 commit comments

Comments
 (0)