Skip to content

Commit 9f64900

Browse files
committed
finalize types
1 parent 8d7f168 commit 9f64900

File tree

4 files changed

+239
-67
lines changed

4 files changed

+239
-67
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ Once stopped, the Workflow instance *cannot* be resumed.
155155

156156
**Known issue**: Restarting a Workflow via the `restart()` method is not currently supported ad will throw an exception (error).
157157

158+
:::
159+
158160
```ts
159161
let instance = await env.MY_WORKFLOW.get("abc-123")
160162
await instance.restart() // Returns Promise<void>

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

Lines changed: 188 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,20 @@ sidebar:
66

77
---
88

9-
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.
10-
119
This guide details the Workflows API within Cloudflare Workers, including methods, types and usage examples.
1210

13-
## Bind to a Workflow
14-
15-
:::note[Workflows beta]
16-
17-
Workflows currently requires you to bind to a Workflow via `wrangler.toml` and does not yet support bindings via the Workers dashboard.
18-
19-
:::
11+
## WorkflowEntrypoint
2012

21-
You can bind to a Workflow by defining a `[[workflows]]` binding within your `wrangler.toml` configuration.
22-
23-
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:
24-
25-
```toml title="wrangler.toml"
26-
#:schema node_modules/wrangler/config-schema.json
27-
name = "workflows-starter"
28-
main = "src/index.ts"
29-
compatibility_date = "2024-10-16"
13+
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.
3014

31-
[[workflows]]
32-
# name of your workflow
33-
name = "workflows-starter"
34-
# binding name env.MYWORKFLOW
35-
binding = "MY_WORKFLOW"
36-
# this is class that extends the Workflow class in src/index.ts
37-
class_name = "MyWorkflow"
38-
# script_name is required during for the beta.
39-
# Must match the "name" of your Worker at the top of wrangler.toml
40-
script_name = "workflows-starter"
15+
```ts
16+
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
17+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
18+
// Steps here
19+
}
20+
};
4121
```
4222

43-
## Workflow
44-
4523
* <code>run(event: WorkflowEvent&lt;T&gt;, step: WorkflowStep): Promise&lt;T&gt;</code>
4624

4725
* `event` - the event passed to the Workflow, including an optional `payload` containing data (parameters)
@@ -112,3 +90,183 @@ Refer to the [documentation on sleeping and retrying](/workflows/build/sleeping-
11290

11391
* Throws an error that forces the current Workflow instance to fail and not be retried.
11492
* Refer to the [documentation on sleeping and retrying](/workflows/build/sleeping-and-retrying/) to learn more about how how Workflows are retried.
93+
94+
## Call Workflows from Workers
95+
96+
:::note[Workflows beta]
97+
98+
Workflows currently requires you to bind to a Workflow via `wrangler.toml` and does not yet support bindings via the Workers dashboard.
99+
100+
:::
101+
102+
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.
103+
104+
You can bind to a Workflow by defining a `[[workflows]]` binding within your `wrangler.toml` configuration.
105+
106+
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:
107+
108+
```toml title="wrangler.toml"
109+
#:schema node_modules/wrangler/config-schema.json
110+
name = "workflows-starter"
111+
main = "src/index.ts"
112+
compatibility_date = "2024-10-16"
113+
114+
[[workflows]]
115+
# name of your workflow
116+
name = "workflows-starter"
117+
# binding name env.MYWORKFLOW
118+
binding = "MY_WORKFLOW"
119+
# this is class that extends the Workflow class in src/index.ts
120+
class_name = "MyWorkflow"
121+
# script_name is required during for the beta.
122+
# Must match the "name" of your Worker at the top of wrangler.toml
123+
script_name = "workflows-starter"
124+
```
125+
126+
## Workflow
127+
128+
:::note
129+
130+
Ensure you have `@cloudflare/workers-types` version `4.20241022.0` or later installed when binding to Workflows from within a Workers project.
131+
132+
:::
133+
134+
The `Workflow` type provides methods that allow you to create, inspect the status and manage running Workflow instances from within a Worker script.
135+
136+
```ts
137+
interface Env {
138+
// The 'MY_WORKFLOW' variable should match the "binding" value set in wrangler.toml
139+
MY_WORKFLOW: Workflow;
140+
}
141+
```
142+
143+
The `Workflow` type exports the following methods:
144+
145+
### create
146+
147+
Create (trigger) a new instance of the given Workflow.
148+
149+
* <code>create(options?: WorkflowInstanceCreateOptions): Promise&lt;WorkflowInstance&gt;</code>
150+
151+
* `options` - optional properties to pass when creating an instance.
152+
153+
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.
154+
155+
```ts
156+
// Create a new Workflow instance with your own ID:
157+
let instance = await env.MY_WORKFLOW.create({ id: myIdDefinedFromOtherSystem })
158+
return Response.json({
159+
id: instance.id,
160+
details: await instance.status(),
161+
});
162+
```
163+
164+
Returns a `WorkflowInstance`.
165+
166+
### get
167+
168+
Get a specific Workflow instance by ID.
169+
170+
* <code>get(id: string): Promise&lt;WorkflowInstance&gt;</code>
171+
172+
* `id` - the ID of the Workflow instance.
173+
174+
Returns a `WorkflowInstance`.
175+
176+
## WorkflowInstanceCreateOptions
177+
178+
Optional properties to pass when creating an instance.
179+
180+
```ts
181+
interface WorkflowInstanceCreateOptions {
182+
/**
183+
* An id for your Workflow instance. Must be unique within the Workflow.
184+
*/
185+
id?: string;
186+
/**
187+
* The event payload the Workflow instance is triggered with
188+
*/
189+
params?: unknown;
190+
}
191+
```
192+
193+
## WorkflowInstance
194+
195+
Represents a specific instance of a Workflow, and provides methods to manage the instance.
196+
197+
```ts
198+
declare abstract class WorkflowInstance {
199+
public id: string;
200+
/**
201+
* Pause the instance.
202+
*/
203+
public pause(): Promise<void>;
204+
/**
205+
* Resume the instance. If it is already running, an error will be thrown.
206+
*/
207+
public resume(): Promise<void>;
208+
/**
209+
* Terminate the instance. If it is errored, terminated or complete, an error will be thrown.
210+
*/
211+
public terminate(): Promise<void>;
212+
/**
213+
* Restart the instance.
214+
*/
215+
public restart(): Promise<void>;
216+
/**
217+
* Returns the current status of the instance.
218+
*/
219+
public status(): Promise<InstanceStatus>;
220+
}
221+
```
222+
223+
### id
224+
225+
Return the id of a Workflow.
226+
227+
* <code>id: string</code>
228+
229+
### status
230+
231+
Pause a running Workflow instance.
232+
233+
* <code>status(): Promise&lt;void&gt;</code>
234+
235+
### pause
236+
237+
Pause a running Workflow instance.
238+
239+
* <code>pause(): Promise&lt;void&gt;</code>
240+
241+
### restart
242+
243+
Restart a Workflow instance.
244+
245+
* <code>restart(): Promise&lt;void&gt;</code>
246+
247+
### terminate
248+
249+
Terminate a Workflow instance.
250+
251+
* <code>terminate(): Promise&lt;void&gt;</code>
252+
253+
### InstanceStatus
254+
255+
Details the status of a Workflow instance.
256+
257+
```ts
258+
type InstanceStatus = {
259+
status:
260+
| "queued" // means that instance is waiting to be started (see concurrency limits)
261+
| "running"
262+
| "paused"
263+
| "errored"
264+
| "terminated" // user terminated the instance while it was running
265+
| "complete"
266+
| "waiting" // instance is hibernating and waiting for sleep or event to finish
267+
| "waitingForPause" // instance is finishing the current work to pause
268+
| "unknown";
269+
error?: string;
270+
output?: object;
271+
};
272+
```

src/content/docs/workflows/observability/metrics-analytics.mdx

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,54 @@ The metrics displayed in the [Cloudflare dashboard](https://dash.cloudflare.com/
1212

1313
## Metrics
1414

15-
Workflows currently export the below metrics:
15+
Workflows currently export the below metrics within the `workflowsAdaptiveGroups` GraphQL dataset
1616

1717
| Metric | GraphQL Field Name | Description |
1818
| ---------------------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
1919
| Read Queries (qps) | `readQueries` | The number of read queries issued against a database. This is the raw number of read queries, and is not used for billing. |
2020

2121
Metrics can be queried (and are retained) for the past 31 days.
2222

23+
### Labels and dimensions
24+
25+
The `workflowsAdaptiveGroups` dataset provides the following dimensions for filtering and grouping query results:
26+
27+
* `workflowName` - Workflow name - e.g. `my-workflow`
28+
* `instanceId` - Instance ID.
29+
* `stepName` - Step name
30+
* `eventType` - Event type (see [event types](#event-types))
31+
* `stepCount` - Step number within a given instance
32+
* `date` - The date when trigger was triggered
33+
* `datetimeFifteenMinutes` - The date and time truncated to fifteen minutes
34+
* `datetimeFiveMinutes` - The date and time truncated to five minutes
35+
* `datetimeHour` - The date and time truncated to the hour
36+
* `datetimeMinute` - The date and time truncated to the minute
37+
38+
### Event types
39+
40+
The `eventType` metric allows you to filter (or groupBy) Workflows and steps based on their last observed status.
41+
42+
The possible values for `eventType` are documented below:
43+
44+
#### Workflows-level status labels
45+
46+
* `WORKFLOW_QUEUED` - the Workflow is queued, but not currently running. This can happen when you are at the [concurrency limit](/workflows/reference/limits/) and new instances are waiting for currently running instances to complete.
47+
* `WORKFLOW_START` - the Workflow has started and is running.
48+
* `WORKFLOW_SUCCESS` - the Workflow finished without errors.
49+
* `WORKFLOW_FAILURE` - the Workflow failed due to errors (exhausting retries, errors thrown, etc)
50+
* `WORKFLOW_TERMINATED` - the Workflow was explicitly terminated.
51+
52+
#### Step-level status labels
53+
54+
* `STEP_START` - the step has started and is running.
55+
* `STEP_SUCCESS` - the step finished without errors.
56+
* `STEP_FAILURE` - the step failed due to an error.
57+
* `SLEEP_START` - the step is sleeping.
58+
* `SLEEP_COMPLETE` - the step last finished sleeping.
59+
* `ATTEMPT_START` - a step is retrying.
60+
* `ATTEMPT_SUCCESS` - the retry succeeded.
61+
* `ATTEMPT_FAILURE` - the retry attempt failed.
62+
2363
## View metrics in the dashboard
2464

2565
Per-Workflow and instance analytics for Workflows are available in the Cloudflare dashboard. To view current and historical metrics for a database:
@@ -34,7 +74,7 @@ You can optionally select a time window to query. This defaults to the last 24 h
3474

3575
You can programmatically query analytics for your Workflows via the [GraphQL Analytics API](/analytics/graphql-api/). This API queries the same datasets as the Cloudflare dashboard, and supports GraphQL [introspection](/analytics/graphql-api/features/discovery/introspection/).
3676

37-
Workflows GraphQL datasets require an `accountTag` filter with your Cloudflare account ID, and includes the `workflowsAdaptive` and `workflowsAdaptiveGroups` datasets.
77+
Workflows GraphQL datasets require an `accountTag` filter with your Cloudflare account ID, and includes the `workflowsAdaptiveGroups` dataset.
3878

3979
### Examples
4080

@@ -126,7 +166,7 @@ Here we are doing the same for `wallTime`, `instanceRuns` and `stepCount` in the
126166

127167
Here lets query `workflowsAdaptive` for raw data about `$instanceId` between `$datetimeStart` and `$datetimeEnd`:
128168

129-
```
169+
```graphql
130170
{
131171
viewer {
132172
accounts(filter: { accountTag: $accountTag }) {
@@ -165,32 +205,3 @@ Example values for the query variables:
165205
"instanceId": "ecc48200-11c4-22a3-b05f-88a3c1c1db81"
166206
}
167207
```
168-
169-
As said above, you can use [introspection](/analytics/graphql-api/features/discovery/introspection/) with your favorite GraphQL client to discover the available fields and types of the Workflows datasets, but here are some of our `workflowsAdaptiveGroups` dimensions:
170-
171-
* date - The date when trigger was triggered
172-
* datetimeFifteenMinutes - The date and time truncated to fifteen minutes
173-
* datetimeFiveMinutes - The date and time truncated to five minutes
174-
* datetimeHour - The date and time truncated to the hour
175-
* datetimeMinute - The date and time truncated to the minute
176-
* eventType - Event type
177-
* instanceId - Instance Id
178-
* stepCount - Step number
179-
* stepName - Step name
180-
* workflowName - Workflow Name
181-
182-
And here's a list of `eventType`s you can filter on:
183-
184-
* WORKFLOW_QUEUED
185-
* ORKFLOW_START
186-
* WORKFLOW_SUCCESS
187-
* WORKFLOW_FAILURE
188-
* WORKFLOW_TERMINATED
189-
* STEP_START
190-
* STEP_SUCCESS
191-
* STEP_FAILURE
192-
* SLEEP_START
193-
* SLEEP_COMPLETE
194-
* ATTEMPT_START
195-
* ATTEMPT_SUCCESS
196-
* ATTEMPT_FAILURE

src/content/docs/workflows/reference/limits.mdx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ Many limits are inherited from those applied to Workers scripts and as documente
1818
| Total scripts per account | 100 | 500 (shared with [Worker script limits](/workers/platform/limits/#account-plan-limits) |
1919
| Compute time per Workflow | 10 seconds | 30 seconds of [active CPU time](/workers/platform/limits/#cpu-time) |
2020
| Duration (wall clock) per `step` | Unlimited | Unlimited - e.g. waiting on network I/O calls or querying a database |
21-
| Maximum persisted state per step | 1MiB (2^20 bytes) | 1MiB (2^20 bytes) |
22-
| Maximum state that can be persisted per Workflow instance | 100MB | 1GB |
21+
| Maximum persisted state per step | 1MiB (2^20 bytes) | 1MiB (2^20 bytes) |
22+
| Maximum state that can be persisted per Workflow instance | 100MB | 1GB |
2323
| Maximum `step.sleep` duration | 365 days (1 year) [^1] |
2424
| Maximum steps per Workflow | 256 [^1] |
2525
| Maximum Workflow executions | 100,000 per day [shared with Workers daily limit](/workers/platform/limits/#worker-limits) | Unlimited |
26-
| Concurrent Workflow instances (executions) | 25 | 1,000 [^1] |
26+
| Concurrent Workflow instances (executions) | 25 | 100 [^1] |
2727
| Retention limit for completed Workflow state | 3 days | 30 days [^2] |
28+
| Maximum length of a Workflow ID | 64 bytes |
2829

29-
[^1]: This limit will be reviewed and revised during the open beta for Workflows, and we'll update this page and our changelog.
30+
[^1]: This limit will be reviewed and revised during the open beta for Workflows. Follow the [Workflows changelog](/workflows/reference/changelog/) for updates.
3031

31-
[^2]: Workflow state and logs will be retained for 3 days on the Workers Free plan and 30 days on the Workers Paid plan.
32+
[^2]: Workflow state and logs will be retained for 3 days on the Workers Free plan and for 7 days on the Workers Paid plan.
3233

3334
<Render file="limits_increase" product="workers" />

0 commit comments

Comments
 (0)