Skip to content

Commit 9cc0ab6

Browse files
committed
Merge branch 'silverlock/workflows/new' of github.com:cloudflare/cloudflare-docs into silverlock/workflows/new
2 parents 38f8d88 + 5ae5439 commit 9cc0ab6

File tree

11 files changed

+336
-131
lines changed

11 files changed

+336
-131
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
2-
link: "/workflows/platform/changelog/"
2+
link: "/workflows/reference/changelog/"
33
productName: Workflows
44
productLink: "/workflows/"
55
productArea: Developer platform
6-
productAreaLink: /workflows/
6+
productAreaLink: /workers/platform/changelog/platform/
77
entries:
8-
- publish_date: "2038-01-19"
9-
title: Workflows is now in public beta.
8+
- publish_date: "2024-10-23"
9+
title: "Workflows is now in public beta!"
1010
description: |-
1111
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/).
1212
13-
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](/workers-ai/tutorials/build-a-retrieval-augmented-generation-ai/), or managing user signup flows and lifecycle emails.
13+
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.
1414
15-
You can learn more about Workflows in [our announcement blog], or start building in our [get started guide].
15+
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/).

src/content/docs/workers/wrangler/commands.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,12 @@ Finished processing secrets JSON file:
12471247

12481248
## `workflows`
12491249

1250+
:::note
1251+
1252+
`wrangler workflows` commands are available in `wrangler` version `3.82.0` or later.
1253+
1254+
:::
1255+
12501256
Manage and configure [Workflows](/workflows/).
12511257

12521258
### `list`

src/content/docs/workflows/build/sleeping-and-retrying.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const workflowsLaunchDate = Date.parse("24 Oct 2024 13:00:00 UTC");
4848
await step.sleepUntil("sleep until X times out", workflowsLaunchDate)
4949
```
5050

51+
You can also provide a Unix timestamp (seconds since the Unix epoch) directly to `sleepUntil`.
52+
5153
## Retry steps
5254

5355
Each call to `step.do` in a Workflow accepts an optional `StepConfig`, which allows you define the retry behaviour for that step.

src/content/docs/workflows/build/trigger-workflows.mdx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ sidebar:
99
You can trigger Workflows both programmatically and via the Workflows APIs, including:
1010

1111
1. With [Workers](/workers) via HTTP requests in a `fetch` handler, or bindings from a `queue` or `scheduled` handler
12-
2. Using the [Workflows REST API](/api/paths/accounts-account_id--workflows/get)
12+
2. Using the [Workflows REST API](/api/operations/wor-list-workflows)
1313
2. Via the [wrangler CLI](/workers/wrangler/commands/#workflows) in your terminal
1414

1515
## Workers API (Bindings)
@@ -151,7 +151,11 @@ Once stopped, the Workflow instance *cannot* be resumed.
151151

152152
### Restart a Workflow
153153

154-
You can restart a Workflow instance by calling `restart` against a specific instance ID.
154+
:::caution
155+
156+
**Known issue**: Restarting a Workflow via the `restart()` method is not currently supported ad will throw an exception (error).
157+
158+
:::
155159

156160
```ts
157161
let instance = await env.MY_WORKFLOW.get("abc-123")
@@ -162,7 +166,7 @@ Restarting an instance will immediately cancel any in-progress steps, erase any
162166

163167
## REST API (HTTP)
164168

165-
Refer to the [Workflows REST API documentation](/api/paths/accounts-account_id--workflows--workflow_name--instances/post).
169+
Refer to the [Workflows REST API documentation](/api/operations/wor-create-new-workflow-instance).
166170

167171
## Command line (CLI)
168172

src/content/docs/workflows/build/workers-api.mdx

Lines changed: 240 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,269 @@ sidebar:
66

77
---
88

9-
TODO
9+
import { MetaInfo, Type } from "~/components";
1010

11-
## Binding
11+
This guide details the Workflows API within Cloudflare Workers, including methods, types and usage examples.
1212

13-
TODO
13+
## WorkflowEntrypoint
1414

15-
## Interact with Workflows
15+
The `WorkflowEntrypoint` class is the core element of a Workflow definition. A Workflow must extend this class and define a `run` method with at least one `step` call to be considered a valid Workflow.
1616

17-
* Running a Workflow with the `run()` method
18-
* `StepConfig`
19-
* `NonRetriableError`
20-
* TODO on TypeScript type params
17+
```ts
18+
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
19+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
20+
// Steps here
21+
}
22+
};
23+
```
2124

22-
## Workflow
25+
* <code>run(event: WorkflowEvent&lt;T&gt;, step: WorkflowStep): Promise&lt;T&gt;</code>
2326

24-
* <code>run(events: WorkflowEvent&lt;T&gt;[], step: WorkflowStep): Promise&lt;T&gt;</code>
27+
* `event` - the event passed to the Workflow, including an optional `payload` containing data (parameters)
28+
* `step` - the `WorkflowStep` type that provides the step methods for your Workflow
2529

26-
TODO
30+
The `WorkflowEvent` type accepts an optional [type parameter](https://www.typescriptlang.org/docs/handbook/2/generics.html#working-with-generic-type-variables) that allows you to provide a type for the `payload` property within the `WorkflowEvent`.
31+
32+
Refer to the [events and parameters](/workflows/build/events-and-parameters/) documentation for how to handle events within yur Workflow code.
33+
34+
## WorkflowEvent
2735

2836
```ts
29-
export class MyWorkflow extends Workflow<Env, Params> {
30-
async run(events: WorkflowEvent[], step: WorkflowStep) {
31-
// TODO
32-
}
33-
}
37+
export type WorkflowEvent<T> = {
38+
payload: Readonly<T>;
39+
timestamp: Date;
40+
};
3441
```
3542

43+
* The `WorkflowEvent` is the first argument to a Workflow's `run` method, and includes an optional `payload` parameter and a `timestamp` property.
44+
45+
* `payload` - a default type of `any` or type `T` if a type parameter is provided.
46+
* `timestamp` - a `Date` object set to the time the Workflow instance was created (triggered).
47+
48+
Refer to the [events and parameters](/workflows/build/events-and-parameters/) documentation for how to handle events within yur Workflow code.
3649

3750
## WorkflowStep
3851

3952
### step
4053

41-
TODO - into to steps
54+
* <code>step.do(name: string, callback: (): RpcSerializable): Promise&lt;T&gt;</code>
55+
* <code>step.do(name: string, config?: WorkflowStepConfig, callback: (): RpcSerializable): Promise&lt;T&gt;</code>
4256

43-
* <code>step.do(name: string, callback: (): RpcSerializable, config?: StepConfig): Promise&lt;T&gt;</code>
57+
* `name` - the name of the step.
58+
* `config` (optional) - an optional `WorkflowStepConfig` for configuring [step specific retry behaviour](/workflows/build/sleeping-and-retrying/)
59+
* `callback` - an asynchronous function that optionally returns serializable state for the Workflow to persist.
4460

45-
* TODO - describe each param
46-
* TODO -
61+
* <code>step.sleep(name: string, duration: WorkflowDuration): Promise&lt;void&gt;</code>
4762

48-
TODO - show an example
49-
TODO - show an example of dynamically creating a step
63+
* `name` - the name of the step.
64+
* `duration` - the duration to sleep until, in either seconds or as a `WorkflowDuration` compatible string.
65+
* Refer to the [documentation on sleeping and retrying](/workflows/build/sleeping-and-retrying/) to learn more about how how Workflows are retried.
5066

51-
* <code>step.sleep(name: string, duration: WorkflowDuration): Promise&lt;void&gt;</code>
67+
* <code>step.sleepUntil(name: string, timestamp: Date | number): Promise&lt;void&gt;</code>
68+
69+
* `name` - the name of the step.
70+
* `timestamp` - a JavaScript `Date` object or seconds from the Unix epoch to sleep the Workflow instance until.
71+
72+
## WorkflowStepConfig
73+
74+
```ts
75+
export type WorkflowStepConfig = {
76+
retries?: {
77+
limit: number;
78+
delay: string | number;
79+
backoff?: WorkflowBackoff;
80+
};
81+
timeout?: string | number;
82+
};
83+
```
84+
85+
* A `WorkflowStepConfig` is an optional argument to the `do` method of a `WorkflowStep` and defines properties that allow you to configure the retry behaviour of that step.
86+
87+
Refer to the [documentation on sleeping and retrying](/workflows/build/sleeping-and-retrying/) to learn more about how how Workflows are retried.
88+
89+
## NonRetryableError
90+
91+
* <code>throw new NonRetryableError(message: <Type text='string' />, name <Type text='string' /> <MetaInfo text='optional' />)</code>: <Type text='NonRetryableError' />
92+
93+
* Throws an error that forces the current Workflow instance to fail and not be retried.
94+
* Refer to the [documentation on sleeping and retrying](/workflows/build/sleeping-and-retrying/) to learn more about how how Workflows are retried.
95+
96+
## Call Workflows from Workers
97+
98+
:::note[Workflows beta]
99+
100+
Workflows currently requires you to bind to a Workflow via `wrangler.toml` and does not yet support bindings via the Workers dashboard.
101+
102+
:::
103+
104+
Workflows exposes a API directly to your Workers scripts via the [bindings](/workers/runtime-apis/bindings/#what-is-a-binding) concept. Bindings allow you to securely call a Workflow without having to manage API keys or clients.
105+
106+
You can bind to a Workflow by defining a `[[workflows]]` binding within your `wrangler.toml` configuration.
107+
108+
For example, to bind to a Workflow called `workflows-starter` and to make it available on the `MY_WORKFLOW` variable to your Worker script, you would configure the following fields within the `[[workflows]]` binding definition:
109+
110+
```toml title="wrangler.toml"
111+
#:schema node_modules/wrangler/config-schema.json
112+
name = "workflows-starter"
113+
main = "src/index.ts"
114+
compatibility_date = "2024-10-16"
115+
116+
[[workflows]]
117+
# name of your workflow
118+
name = "workflows-starter"
119+
# binding name env.MYWORKFLOW
120+
binding = "MY_WORKFLOW"
121+
# this is class that extends the Workflow class in src/index.ts
122+
class_name = "MyWorkflow"
123+
# script_name is required during for the beta.
124+
# Must match the "name" of your Worker at the top of wrangler.toml
125+
script_name = "workflows-starter"
126+
```
127+
128+
## Workflow
129+
130+
:::note
131+
132+
Ensure you have `@cloudflare/workers-types` version `4.20241022.0` or later installed when binding to Workflows from within a Workers project.
133+
134+
:::
135+
136+
The `Workflow` type provides methods that allow you to create, inspect the status and manage running Workflow instances from within a Worker script.
137+
138+
```ts
139+
interface Env {
140+
// The 'MY_WORKFLOW' variable should match the "binding" value set in wrangler.toml
141+
MY_WORKFLOW: Workflow;
142+
}
143+
```
144+
145+
The `Workflow` type exports the following methods:
52146

53-
* TODO -
147+
### create
54148

55-
TODO - show an example
149+
Create (trigger) a new instance of the given Workflow.
56150

151+
* <code>create(options?: WorkflowInstanceCreateOptions): Promise&lt;WorkflowInstance&gt;</code>
57152

58-
## StepConfig
153+
* `options` - optional properties to pass when creating an instance.
59154

60-
TODO
155+
An ID is automatically generated, but a user-provided ID can be specified. This can be useful when mapping Workflows to users, merchants or other identifiers in your system.
61156

62-
## NonRetriableError
157+
```ts
158+
// Create a new Workflow instance with your own ID:
159+
let instance = await env.MY_WORKFLOW.create({ id: myIdDefinedFromOtherSystem })
160+
return Response.json({
161+
id: instance.id,
162+
details: await instance.status(),
163+
});
164+
```
165+
166+
Returns a `WorkflowInstance`.
167+
168+
### get
169+
170+
Get a specific Workflow instance by ID.
171+
172+
* <code>get(id: string): Promise&lt;WorkflowInstance&gt;</code>
173+
174+
* `id` - the ID of the Workflow instance.
175+
176+
Returns a `WorkflowInstance`.
177+
178+
## WorkflowInstanceCreateOptions
179+
180+
Optional properties to pass when creating an instance.
181+
182+
```ts
183+
interface WorkflowInstanceCreateOptions {
184+
/**
185+
* An id for your Workflow instance. Must be unique within the Workflow.
186+
*/
187+
id?: string;
188+
/**
189+
* The event payload the Workflow instance is triggered with
190+
*/
191+
params?: unknown;
192+
}
193+
```
194+
195+
## WorkflowInstance
196+
197+
Represents a specific instance of a Workflow, and provides methods to manage the instance.
198+
199+
```ts
200+
declare abstract class WorkflowInstance {
201+
public id: string;
202+
/**
203+
* Pause the instance.
204+
*/
205+
public pause(): Promise<void>;
206+
/**
207+
* Resume the instance. If it is already running, an error will be thrown.
208+
*/
209+
public resume(): Promise<void>;
210+
/**
211+
* Terminate the instance. If it is errored, terminated or complete, an error will be thrown.
212+
*/
213+
public terminate(): Promise<void>;
214+
/**
215+
* Restart the instance.
216+
*/
217+
public restart(): Promise<void>;
218+
/**
219+
* Returns the current status of the instance.
220+
*/
221+
public status(): Promise<InstanceStatus>;
222+
}
223+
```
224+
225+
### id
226+
227+
Return the id of a Workflow.
228+
229+
* <code>id: string</code>
230+
231+
### status
232+
233+
Pause a running Workflow instance.
234+
235+
* <code>status(): Promise&lt;void&gt;</code>
236+
237+
### pause
238+
239+
Pause a running Workflow instance.
240+
241+
* <code>pause(): Promise&lt;void&gt;</code>
242+
243+
### restart
244+
245+
Restart a Workflow instance.
246+
247+
* <code>restart(): Promise&lt;void&gt;</code>
248+
249+
### terminate
250+
251+
Terminate a Workflow instance.
252+
253+
* <code>terminate(): Promise&lt;void&gt;</code>
254+
255+
### InstanceStatus
256+
257+
Details the status of a Workflow instance.
258+
259+
```ts
260+
type InstanceStatus = {
261+
status:
262+
| "queued" // means that instance is waiting to be started (see concurrency limits)
263+
| "running"
264+
| "paused"
265+
| "errored"
266+
| "terminated" // user terminated the instance while it was running
267+
| "complete"
268+
| "waiting" // instance is hibernating and waiting for sleep or event to finish
269+
| "waitingForPause" // instance is finishing the current work to pause
270+
| "unknown";
271+
error?: string;
272+
output?: object;
273+
};
274+
```

0 commit comments

Comments
 (0)