-
Notifications
You must be signed in to change notification settings - Fork 10.4k
workers: fix typo #16220
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
Merged
Merged
workers: fix typo #16220
Changes from 49 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
2cb3bf2
commit
elithrar a93db6e
icon
elithrar 1e77cce
fix link
elithrar 2e6f4fa
rename to workflows-starter
elithrar 61869e6
fix limit
elithrar 26db15e
mebibyte
elithrar 7badf0d
events and params
elithrar e0c3786
npx -> npm
elithrar 0fc5105
more
elithrar a21c2a0
wrangler
elithrar 1072186
cron triggers
elithrar 3f6db1d
more docs
elithrar 6d2439f
rules of steps
elithrar 9c054b8
fix
elithrar 7654b8c
CLI guide
elithrar 05a635a
finish CLI starter
elithrar 09df5cf
update landing page
elithrar a6c5e73
fix numbering
elithrar 211b552
fix link formatting
elithrar 1fdb278
fix
elithrar 08afc80
sleeping / retrying
elithrar e11c6f3
trigger
elithrar f5b3aaf
wording
elithrar 2535e0a
wrap get-started guide
elithrar 9533bfd
rules of workflows
elithrar f9eccb3
trigger done
elithrar 883e24a
sleep
elithrar 57d467c
NonRetryableError
elithrar 1c4c29f
better header for nonretryableerror
elithrar 51b3d24
npm create cloudflare@latest workflows-starter -- --template 'cloudfl…
elithrar ffb3894
Apply suggestions from code review
elithrar 477972b
@workflows -> @latest
elithrar f8997ec
remove output from CLI guide
elithrar e0e4248
fix missing link
elithrar af6c6e4
fix links
elithrar 86ba2a8
workflows: review
elithrar dcdf9ee
Adds Workflows GraphQL examples and docs
celso cc2f3c2
Merge branch 'silverlock/workflows/new' of github.com:cloudflare/clou…
celso 43c4b99
Apply suggestions from code review
elithrar ad0fb1a
types
elithrar acd1169
changelog + dates
elithrar 5ddce18
Apply suggestions from code review
Oxyjun a8de669
workflows: Apply suggestions from code review
elithrar 6fb0e14
compat date
elithrar 8d7f168
remove how-workflows-works for now
elithrar 9f64900
finalize types
elithrar 4fdb2b0
wrangler version note
elithrar a7f2553
Importing Type and MetaInfo component
Oxyjun 5ae5439
Fixing REST API links
Oxyjun a10ae6a
Apply suggestions from code review
elithrar 151eb2e
fix API URL
elithrar af8d02c
comment out pause
elithrar 336d863
fix bad class naming
elithrar 925dd82
edits
elithrar e858bb1
naming steps
elithrar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,15 @@ | ||
| --- | ||
| link: "/workflows/reference/changelog/" | ||
| productName: Workflows | ||
| productLink: "/workflows/" | ||
| productArea: Developer platform | ||
| productAreaLink: /workers/platform/changelog/platform/ | ||
| entries: | ||
| - publish_date: "2024-10-23" | ||
| title: "Workflows is now in public beta!" | ||
| description: |- | ||
| Workflows, a new product for building reliable, multi-step workflows using Cloudflare Workers, is now in public beta. The public beta is available to any user with a [free or paid Workers plan](/workers/platform/pricing/). | ||
|
|
||
| A Workflow allows you to define multiple, independent steps that encapsulate errors, automatically retry, persist state, and can run for seconds, minutes, hours or even days. A Workflow can be useful for post-processing data from R2 buckets before querying it, automating a Workers AI RAG pipeline, or managing user signup flows and lifecycle emails. | ||
|
|
||
| You can learn more about Workflows in [our announcement blog](https://blog.cloudflare.com/building-workflows-durable-execution-on-workers/), or start building in our [get started guide](/workflows/get-started/guide/). |
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
src/content/docs/workers/runtime-apis/bindings/workflows.mdx
This file contains hidden or 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,10 @@ | ||
| --- | ||
| pcx_content_type: navigation | ||
| title: Workflows | ||
| external_link: /workflows/ | ||
| head: [] | ||
| description: APIs available in Cloudflare Workers to interact with | ||
| Workflows. Workflows allow you to build durable, multi-step applications | ||
| using Workers. | ||
|
|
||
| --- |
This file contains hidden or 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 hidden or 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,35 @@ | ||
| --- | ||
| title: Dynamic steps | ||
| pcx_content_type: concept | ||
| sidebar: | ||
| order: 5 | ||
|
|
||
| --- | ||
|
|
||
| A Workflow does not have to define all of its steps statically: steps can be created programmatically and/or conditionally. | ||
|
|
||
| This allows you to not only trigger steps based on specific input parameters, but to also name steps dynamically and set the retry configuration for a single step. | ||
|
|
||
|
|
||
| ## Example | ||
|
|
||
| You can create steps on-the-fly. You can create a step for each parameter passed to your Workflow, for each file you want to read from storage, or for calls to third-party APIs. | ||
|
|
||
| For example, you can loop over each event, label the step dynamically, and have the step operate only over that `event`: | ||
|
|
||
| ```ts | ||
| export class MyWorkflow extends Workflow<Env, Params> { | ||
| async run(events: WorkflowEvent[], step: WorkflowStep) { | ||
elithrar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Dynamically create a step for each event passed to our workflow | ||
| // ... or for every file we want to read from R2 storage | ||
| // ... or each API call we need to make based on an incoming request | ||
| for (const event of events) { | ||
| await step.do(`processing ${event.id}`, async () => { | ||
| // Step logic for one event goes here | ||
| // You can also specify a StepConfig for each step, just as you | ||
| // would for any other step | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
104 changes: 104 additions & 0 deletions
104
src/content/docs/workflows/build/events-and-parameters.mdx
This file contains hidden or 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,104 @@ | ||
| --- | ||
| title: Events and parameters | ||
| pcx_content_type: concept | ||
| sidebar: | ||
| order: 10 | ||
|
|
||
| --- | ||
|
|
||
| When a Workflow is triggered, it can receive an optional event. This event can include data that your Workflow can act on, including request details, user data fetched from your database (such as D1 or KV) or from a webhook, or messages from a Queue consumer. | ||
|
|
||
| Events are a powerful part of a Workflow, as you often want a Workflow to act on data. Because a given Workflow instance executes durably, events are a useful way to provide a Workflow with data that should be immutable (not changing) and/or represents data the Workflow needs to operate on at that point in time. | ||
Oxyjun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Pass parameters to a Workflow | ||
|
|
||
| You can pass parameters to a Workflow in two ways: | ||
|
|
||
| * As an optional argument to the `create` method on a [Workflow binding](/workers/wrangler/commands/#trigger) when triggering a Workflow from a Worker. | ||
| * Via the `--params` flag when using the `wrangler` CLI to trigger a Workflow. | ||
|
|
||
| You can pass any JSON-serializable object as a parameter. | ||
|
|
||
| :::caution | ||
|
|
||
| A `WorkflowEvent` and its associated `payload` property are effectively _immutable_: any changes to an event are not persisted across the steps of a Workflow. This includes both cases when a Workflow is progressing normally, and in cases where a Workflow has to be restarted due to a failure. | ||
|
|
||
| Store state durably by returning it from your `step.do` callbacks. | ||
|
|
||
| ::: | ||
|
|
||
| ```ts | ||
| export default { | ||
| async fetch(req: Request, env: Env) { | ||
elithrar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let someEvent = { url: req.url, createdTimestamp: Date.now() } | ||
| // Trigger our Workflow | ||
| // Pass our event as the second parameter to the `create` method | ||
| // on our Workflow binding. | ||
| let instance = await env.MYWORKFLOW.create(await crypto.randomUUID(), someEvent); | ||
|
|
||
| return Response.json({ | ||
| id: instance.id, | ||
| details: await instance.status(), | ||
| }); | ||
|
|
||
| return Response.json({ result }); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| To pass parameters via the `wrangler` command-line interface, pass a JSON string as the second parameter to the `workflows trigger` sub-command: | ||
|
|
||
| ```sh | ||
| npx wrangler@latest workflows trigger workflows-starter '{"some":"data"}' | ||
| ``` | ||
| ```sh output | ||
| 🚀 Workflow instance "57c7913b-8e1d-4a78-a0dd-dce5a0b7aa30" has been queued successfully | ||
| ``` | ||
|
|
||
| ## TypeScript and type parameters | ||
|
|
||
| By default, the `WorkflowEvent` passed to the `run` method of your Workflow definition has a type that conforms to the following, with `payload` (your data) and `timestamp` properties: | ||
|
|
||
| ```ts | ||
| export type WorkflowEvent<T> = { | ||
| // The data passed as the parameter when the Workflow instance was triggered | ||
| payload: T; | ||
| // The timestamp that the Workflow was triggered | ||
| timestamp: Date; | ||
| }; | ||
| ``` | ||
|
|
||
| You can optionally type these events by defining your own type and passing it as a [type parameter](https://www.typescriptlang.org/docs/handbook/2/generics.html#working-with-generic-type-variables) to the `WorkflowEvent`: | ||
|
|
||
| ```ts | ||
| // Define a type that conforms to the events your Workflow instance is | ||
| // instantiated with | ||
| interface YourEventType { | ||
| userEmail: string; | ||
| createdTimestamp: number; | ||
| metadata?: Record<string, string>; | ||
| } | ||
| ``` | ||
|
|
||
| When you pass your `YourEventType` to `WorkflowEvent` as a type parameter, the `event.payload` property now has the type `YourEventType` throughout your workflow definition: | ||
|
|
||
| ```ts title="src/index.ts" | ||
| // Import the Workflow definition | ||
| import { Workflow, WorkflowEvent, WorkflowStep } from "cloudflare:workflows" | ||
elithrar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| export class MyWorkflow implements Workflow { | ||
elithrar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Pass your type as a type parameter to WorkflowEvent | ||
| // The 'payload' property will have the type of your parameter. | ||
| async run(event: WorkflowEvent<YourEventType>, step: WorkflowStep) { | ||
| let state = step.do("my first step", async () => { | ||
| // Access your properties via event.payload | ||
| let userEmail = event.payload.userEmail | ||
| let createdTimestamp = event.payload.createdTimestamp | ||
| }) | ||
|
|
||
| step.do("my second step", async () => { /* your code here */ ) | ||
| } | ||
| } | ||
| ``` | ||
| Note that type parameters do not _validate_ that the incoming event matches your type definition. Properties (fields) that do not exist or conform to the type you provided will be dropped. If you need to validate incoming events, we recommend a library such as [zod](https://zod.dev/) or your own validator logic. | ||
This file contains hidden or 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,13 @@ | ||
| --- | ||
| title: Build with Workflows | ||
| pcx_content_type: navigation | ||
| sidebar: | ||
| order: 2 | ||
elithrar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| group: | ||
| hideIndex: true | ||
|
|
||
| --- | ||
|
|
||
| import { DirectoryListing } from "~/components" | ||
|
|
||
| <DirectoryListing /> | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.