Skip to content

Commit ad0fb1a

Browse files
committed
types
1 parent 43c4b99 commit ad0fb1a

File tree

2 files changed

+81
-27
lines changed

2 files changed

+81
-27
lines changed

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/workers-api.mdx

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,109 @@ sidebar:
66

77
---
88

9-
TODO
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.
1010

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

13-
TODO
13+
## Bind to a Workflow
1414

15-
## Interact with Workflows
15+
:::note[Workflows beta]
1616

17-
* Running a Workflow with the `run()` method
18-
* `StepConfig`
19-
* `NonRetriableError`
20-
* TODO on TypeScript type params
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+
:::
20+
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"
30+
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"
41+
```
2142

2243
## Workflow
2344

24-
* <code>run(events: WorkflowEvent&lt;T&gt;[], step: WorkflowStep): Promise&lt;T&gt;</code>
45+
* <code>run(event: WorkflowEvent&lt;T&gt;, step: WorkflowStep): Promise&lt;T&gt;</code>
46+
47+
* `event` - the event passed to the Workflow, including an optional `payload` containing data (parameters)
48+
* `step` - the `WorkflowStep` type that provides the step methods for your Workflow
49+
50+
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`.
51+
52+
Refer to the [events and parameters](/workflows/build/events-and-parameters/) documentation for how to handle events within yur Workflow code.
2553

26-
TODO
54+
## WorkflowEvent
2755

2856
```ts
29-
export class MyWorkflow extends Workflow<Env, Params> {
30-
async run(events: WorkflowEvent[], step: WorkflowStep) {
31-
// TODO
32-
}
33-
}
57+
export type WorkflowEvent<T> = {
58+
payload: Readonly<T>;
59+
timestamp: Date;
60+
};
3461
```
3562

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

3770
## WorkflowStep
3871

3972
### step
4073

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

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

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

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

51-
* <code>step.sleep(name: string, duration: WorkflowDuration): Promise&lt;void&gt;</code>
87+
* <code>step.sleepUntil(name: string, timestamp: Date | number): Promise&lt;void&gt;</code>
5288

53-
* TODO -
89+
* `name` - the name of the step.
90+
* `timestamp` - a JavaScript `Date` object or seconds from the Unix epoch to sleep the Workflow instance until.
91+
92+
## WorkflowStepConfig
93+
94+
```ts
95+
export type WorkflowStepConfig = {
96+
retries?: {
97+
limit: number;
98+
delay: string | number;
99+
backoff?: WorkflowBackoff;
100+
};
101+
timeout?: string | number;
102+
};
103+
```
54104

55-
TODO - show an example
105+
* 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.
56106

107+
Refer to the [documentation on sleeping and retrying](/workflows/build/sleeping-and-retrying/) to learn more about how how Workflows are retried.
57108

58-
## StepConfig
109+
## NonRetryableError
59110

60-
TODO
111+
* <code>throw new NonRetryableError(message: <Type text='string' />, name <Type text='string' /> <MetaInfo text='optional' />)</code>: <Type text='NonRetryableError' />
61112

62-
## NonRetriableError
113+
* Throws an error that forces the current Workflow instance to fail and not be retried.
114+
* Refer to the [documentation on sleeping and retrying](/workflows/build/sleeping-and-retrying/) to learn more about how how Workflows are retried.

0 commit comments

Comments
 (0)