Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/content/docs/workflows/build/sleeping-and-retrying.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ You can also provide a UNIX timestamp (milliseconds since the UNIX epoch) direct

## Retry steps

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

If you do not provide your own retry configuration, Workflows applies the following defaults:

Expand All @@ -69,12 +69,13 @@ const defaultConfig: WorkflowStepConfig = {
};
```

When providing your own `StepConfig`, you can configure:
When providing your own `WorkflowStepConfig`, you can configure:

- The total number of attempts to make for a step (limited to 10,000 retries per step)
- The delay between attempts (accepts both `number` (ms) or a human-readable format)
- What backoff algorithm to apply between each attempt: any of `constant`, `linear`, or `exponential`
- When to timeout (in duration) before considering the step as failed (including during a retry attempt, as the timeout is set per attempt)
- Whether to redact step output

For example, to limit a step to 10 retries and have it apply an exponential delay (starting at 10 seconds) between each attempt, you would pass the following configuration as an optional object to `step.do`:

Expand All @@ -88,13 +89,16 @@ let someState = await step.do(
backoff: "exponential", // Any of "constant" | "linear" | "exponential";
},
timeout: "30 minutes",
sensitive: "output",
},
async () => {
/* Step code goes here */
},
);
```

In this example, `sensitive: "output"` redacts the step output itself from APIs and the dashboard.

## Force a Workflow instance to fail

You can also force a Workflow instance to fail and _not_ retry by throwing a `NonRetryableError` from within the step.
Expand Down
4 changes: 3 additions & 1 deletion src/content/docs/workflows/build/step-context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type WorkflowStepContext = {
| `step.name` | `string` | The name you passed to `step.do`. |
| `step.count` | `number` | How many times `step.do` has been called with this name so far in the current Workflow run. Starts at `1` for the first call with a given name. |
| `attempt` | `number` | The current attempt number (1-indexed). `1` on the first try, `2` on the first retry, and so on. |
| `config` | [`WorkflowStepConfig`](/workflows/build/workers-api/#workflowstepconfig) | The resolved retry and timeout configuration for this step, including any defaults applied by the runtime. |
| `config` | [`WorkflowStepConfig`](/workflows/build/workers-api/#workflowstepconfig) | The resolved retry, timeout, and output redaction for this step, including any defaults applied by the runtime. |

## Access the context

Expand All @@ -57,10 +57,12 @@ await step.do(
backoff: "exponential",
},
timeout: "30 minutes",
sensitive: "output",
},
async (ctx) => {
console.log(ctx.config.retries.limit); // 10
console.log(ctx.config.timeout); // "30 minutes"
console.log(ctx.config.sensitive); // "output"
},
);
```
Expand Down
8 changes: 6 additions & 2 deletions src/content/docs/workflows/build/workers-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Refer to the [events and parameters](/workflows/build/events-and-parameters/) do
- <code>step.do(name: string, config?: WorkflowStepConfig, callback: (ctx: WorkflowStepContext):
RpcSerializable): Promise&lt;T&gt;</code>
- `name` - the name of the step, up to 256 characters.
- `config` (optional) - an optional `WorkflowStepConfig` for configuring [step specific retry behaviour](/workflows/build/sleeping-and-retrying/).
- `config` (optional) - an optional `WorkflowStepConfig` for configuring [step-specific retry behaviour](/workflows/build/sleeping-and-retrying/) and step output redaction.
- `callback` - an asynchronous function that receives a [`WorkflowStepContext`](/workflows/build/step-context/) and optionally returns serializable state for the Workflow to persist. In JavaScript Workflows, this includes a fresh, unlocked `ReadableStream<Uint8Array>` for large binary output.

:::note[Returning state]
Expand Down Expand Up @@ -186,10 +186,14 @@ export type WorkflowStepConfig = {
backoff?: WorkflowBackoff;
};
timeout?: string | number;
sensitive?: "output";
};
```

- 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.
- A `WorkflowStepConfig` is an optional argument to the `do` method of a `WorkflowStep` and defines properties that allow you to configure the retry behaviour, timeout, and step output redaction for that step.
- Use `sensitive: "output"` to redact step output from the Workers API, REST API, and Workflows dashboard.

Redaction only affects step output returned by these APIs and the dashboard. Your Workflow can still use the step output in later code.

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

Expand Down
16 changes: 12 additions & 4 deletions src/content/docs/workflows/python/python-workers-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MyWorkflow(WorkflowEntrypoint):
- <code>step.do(name=None, *, concurrent=False, config=None)</code> — a decorator that allows you to define a step in a workflow.
- `name` — an optional name for the step. If omitted, the function name (`func.__name__`) is used.
- `concurrent` — an optional boolean that indicates whether dependencies for this step can run concurrently.
- `config` — an optional [`WorkflowStepConfig`](/workflows/build/workers-api/#workflowstepconfig) for configuring [step specific retry behaviour](/workflows/build/sleeping-and-retrying/). This is passed as a Python dictionary and then type translated into a `WorkflowStepConfig` object.
- `config` — an optional [`WorkflowStepConfig`](/workflows/build/workers-api/#workflowstepconfig) for configuring [step-specific retry behaviour](/workflows/build/sleeping-and-retrying/) and step output redaction. This is passed as a Python dictionary and then type translated into a `WorkflowStepConfig` object.

All parameters except `name` are keyword-only.

Expand Down Expand Up @@ -138,20 +138,28 @@ raise NonRetryableError(message)

## Configure a workflow instance

You can bind a step to a specific retry policy by passing a `WorkflowStepConfig` object to the `config` parameter of the `step.do` decorator.
You can bind a step to a specific retry policy or step output redaction policy by passing a `WorkflowStepConfig` object to the `config` parameter of the `step.do` decorator.
With Python Workflows, you need to make sure that your `dict` respects the [`WorkflowStepConfig`](/workflows/build/workers-api/#workflowstepconfig) type.

```python
from workers import WorkflowEntrypoint

class DemoWorkflowClass(WorkflowEntrypoint):
async def run(self, event, step):
@step.do('step-name', config={"retries": {"limit": 1, "delay": "10 seconds"}})
@step.do(
"step-name",
config={
"retries": {"limit": 1, "delay": "10 seconds"},
"sensitive": "output",
},
)
async def first_step():
# do some work
pass
```

Use `"output"` to redact the step output itself from the Workers API, REST API, and Workflows dashboard.

### Access step context (`ctx`)

If you define a `ctx` parameter, the [step context](/workflows/build/step-context/) is injected into that argument. The context is a dictionary with the following keys:
Expand All @@ -160,7 +168,7 @@ If you define a `ctx` parameter, the [step context](/workflows/build/step-contex
| --------- | ------ | ------------------------------------------------------------------------------------------------------ |
| `step` | `dict` | Contains `name` (the step name) and `count` (how many times `step.do` has been called with this name). |
| `attempt` | `int` | The current attempt number (1-indexed). |
| `config` | `dict` | The resolved retry and timeout configuration for this step. |
| `config` | `dict` | The resolved retry, timeout, and step output redaction configuration for this step. |

```python
from workers import WorkflowEntrypoint
Expand Down
Loading