-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create workflows-shared package with initial implementation for local dev * update workflows-shared implementation * plumb into miniflare * plumb into wrangler * fixup fixture * fix plugin * fixture tests * add ownership * Fix types * Reduce sleep * fixup * Update packages/workflows-shared/package.json Co-authored-by: Carmen Popoviciu <cpopoviciu@cloudflare.com> * Clean up config files * Update README for MiniflareOptions, use constants in plugin * Apply suggestions from code review * Update packages/workflows-shared/package.json * Review comments * Fix CI * Add changeset * add noop deploy script * update snapshots * fix type checks * fix lints * fix lint * run prettier --------- Co-authored-by: Samuel Macleod <smacleod@cloudflare.com> Co-authored-by: Sid Chatterjee <sid@cloudflare.com> Co-authored-by: Carmen Popoviciu <cpopoviciu@cloudflare.com>
- Loading branch information
1 parent
80e5bc6
commit 5ef6231
Showing
39 changed files
with
2,016 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@cloudflare/workflows-shared": patch | ||
"miniflare": patch | ||
"wrangler": patch | ||
--- | ||
|
||
Add preliminary support for Workflows in wrangler dev |
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,55 @@ | ||
import { | ||
WorkerEntrypoint, | ||
Workflow, | ||
WorkflowEntrypoint, | ||
WorkflowEvent, | ||
WorkflowStep, | ||
} from "cloudflare:workers"; | ||
|
||
type Params = { | ||
name: string; | ||
}; | ||
export class Demo extends Workflow<{}, Params> { | ||
async run(events: Array<WorkflowEvent<Params>>, step: WorkflowStep) { | ||
const { timestamp, payload } = events[0]; | ||
|
||
export class Demo extends WorkflowEntrypoint<{}, Params> { | ||
async run(event: WorkflowEvent<Params>, step: WorkflowStep) { | ||
const { timestamp, payload } = event; | ||
|
||
const result = await step.do("First step", async function () { | ||
return { | ||
output: "First step result", | ||
}; | ||
}); | ||
|
||
await step.sleep("Wait", "1 minute"); | ||
await step.sleep("Wait", "1 second"); | ||
|
||
const result2 = await step.do("Second step", async function () { | ||
return { | ||
output: "Second step result", | ||
}; | ||
}); | ||
|
||
return { | ||
result, | ||
result2, | ||
timestamp, | ||
payload, | ||
}; | ||
return [result, result2, timestamp, payload]; | ||
} | ||
} | ||
|
||
type Env = { | ||
WORKFLOW: { | ||
create: (id: string) => { | ||
pause: () => {}; | ||
}; | ||
}; | ||
WORKFLOW: Workflow; | ||
}; | ||
export default class extends WorkerEntrypoint<Env> { | ||
async fetch() { | ||
const handle = await this.env.WORKFLOW.create(crypto.randomUUID()); | ||
await handle.pause(); | ||
return new Response(); | ||
async fetch(req: Request) { | ||
const url = new URL(req.url); | ||
const id = url.searchParams.get("workflowName"); | ||
|
||
if (url.pathname === "/favicon.ico") { | ||
return new Response(null, { status: 404 }); | ||
} | ||
|
||
let handle: WorkflowInstance; | ||
if (url.pathname === "/create") { | ||
handle = await this.env.WORKFLOW.create({ id }); | ||
} else { | ||
handle = await this.env.WORKFLOW.get(id); | ||
} | ||
|
||
return Response.json(await handle.status()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { resolve } from "path"; | ||
import { fetch } from "undici"; | ||
import { afterAll, beforeAll, describe, it, vi } from "vitest"; | ||
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; | ||
|
||
describe("Workflows", () => { | ||
let ip: string, | ||
port: number, | ||
stop: (() => Promise<unknown>) | undefined, | ||
getOutput: () => string; | ||
|
||
beforeAll(async () => { | ||
({ ip, port, stop, getOutput } = await runWranglerDev( | ||
resolve(__dirname, ".."), | ||
[ | ||
"--port=0", | ||
"--inspector-port=0", | ||
"--upstream-protocol=https", | ||
"--host=prod.example.org", | ||
] | ||
)); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stop?.(); | ||
}); | ||
|
||
async function fetchJson(url: string) { | ||
const response = await fetch(url, { | ||
headers: { | ||
"MF-Disable-Pretty-Error": "1", | ||
}, | ||
}); | ||
const text = await response.text(); | ||
|
||
try { | ||
return JSON.parse(text); | ||
} catch (err) { | ||
throw new Error(`Couldn't parse JSON:\n\n${text}`); | ||
} | ||
} | ||
|
||
it("creates a workflow", async ({ expect }) => { | ||
await expect( | ||
fetchJson(`http://${ip}:${port}/create?workflowName=test`) | ||
).resolves.toEqual({ | ||
status: "running", | ||
output: [], | ||
}); | ||
|
||
await vi.waitFor( | ||
async () => { | ||
await expect( | ||
fetchJson(`http://${ip}:${port}/status?workflowName=test`) | ||
).resolves.toEqual({ | ||
status: "running", | ||
output: [{ output: "First step result" }], | ||
}); | ||
}, | ||
{ timeout: 5000 } | ||
); | ||
|
||
await vi.waitFor( | ||
async () => { | ||
await expect( | ||
fetchJson(`http://${ip}:${port}/status?workflowName=test`) | ||
).resolves.toEqual({ | ||
status: "complete", | ||
output: [ | ||
{ output: "First step result" }, | ||
{ output: "Second step result" }, | ||
], | ||
}); | ||
}, | ||
{ timeout: 5000 } | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["node"] | ||
}, | ||
"include": ["**/*.ts", "../../../node-types.d.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2021", | ||
"lib": ["es2021"], | ||
"module": "es2022", | ||
"types": ["@cloudflare/workers-types/experimental"], | ||
"target": "ES2020", | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["@cloudflare/workers-types"], | ||
"moduleResolution": "node", | ||
"noEmit": true, | ||
"isolatedModules": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"skipLibCheck": true | ||
} | ||
}, | ||
"include": ["**/*.ts"], | ||
"exclude": ["tests"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineProject, mergeConfig } from "vitest/config"; | ||
import configShared from "../../vitest.shared"; | ||
|
||
export default mergeConfig( | ||
configShared, | ||
defineProject({ | ||
test: {}, | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.