Skip to content
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

workers: fix typo #16220

Merged
merged 55 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
2cb3bf2
commit
elithrar Oct 16, 2024
a93db6e
icon
elithrar Oct 16, 2024
1e77cce
fix link
elithrar Oct 16, 2024
2e6f4fa
rename to workflows-starter
elithrar Oct 16, 2024
61869e6
fix limit
elithrar Oct 16, 2024
26db15e
mebibyte
elithrar Oct 16, 2024
7badf0d
events and params
elithrar Oct 16, 2024
e0c3786
npx -> npm
elithrar Oct 16, 2024
0fc5105
more
elithrar Oct 16, 2024
a21c2a0
wrangler
elithrar Oct 17, 2024
1072186
cron triggers
elithrar Oct 17, 2024
3f6db1d
more docs
elithrar Oct 17, 2024
6d2439f
rules of steps
elithrar Oct 19, 2024
9c054b8
fix
elithrar Oct 19, 2024
7654b8c
CLI guide
elithrar Oct 21, 2024
05a635a
finish CLI starter
elithrar Oct 21, 2024
09df5cf
update landing page
elithrar Oct 21, 2024
a6c5e73
fix numbering
elithrar Oct 21, 2024
211b552
fix link formatting
elithrar Oct 21, 2024
1fdb278
fix
elithrar Oct 21, 2024
08afc80
sleeping / retrying
elithrar Oct 22, 2024
e11c6f3
trigger
elithrar Oct 22, 2024
f5b3aaf
wording
elithrar Oct 22, 2024
2535e0a
wrap get-started guide
elithrar Oct 23, 2024
9533bfd
rules of workflows
elithrar Oct 23, 2024
f9eccb3
trigger done
elithrar Oct 23, 2024
883e24a
sleep
elithrar Oct 23, 2024
57d467c
NonRetryableError
elithrar Oct 23, 2024
1c4c29f
better header for nonretryableerror
elithrar Oct 23, 2024
51b3d24
npm create cloudflare@latest workflows-starter -- --template 'cloudfl…
elithrar Oct 23, 2024
ffb3894
Apply suggestions from code review
elithrar Oct 23, 2024
477972b
@workflows -> @latest
elithrar Oct 23, 2024
f8997ec
remove output from CLI guide
elithrar Oct 23, 2024
e0e4248
fix missing link
elithrar Oct 23, 2024
af6c6e4
fix links
elithrar Oct 23, 2024
86ba2a8
workflows: review
elithrar Oct 23, 2024
dcdf9ee
Adds Workflows GraphQL examples and docs
Oct 23, 2024
cc2f3c2
Merge branch 'silverlock/workflows/new' of github.com:cloudflare/clou…
Oct 23, 2024
43c4b99
Apply suggestions from code review
elithrar Oct 23, 2024
ad0fb1a
types
elithrar Oct 23, 2024
acd1169
changelog + dates
elithrar Oct 23, 2024
5ddce18
Apply suggestions from code review
Oxyjun Oct 23, 2024
a8de669
workflows: Apply suggestions from code review
elithrar Oct 23, 2024
6fb0e14
compat date
elithrar Oct 23, 2024
8d7f168
remove how-workflows-works for now
elithrar Oct 23, 2024
9f64900
finalize types
elithrar Oct 24, 2024
4fdb2b0
wrangler version note
elithrar Oct 24, 2024
a7f2553
Importing Type and MetaInfo component
Oxyjun Oct 24, 2024
5ae5439
Fixing REST API links
Oxyjun Oct 24, 2024
a10ae6a
Apply suggestions from code review
elithrar Oct 24, 2024
151eb2e
fix API URL
elithrar Oct 24, 2024
af8d02c
comment out pause
elithrar Oct 24, 2024
336d863
fix bad class naming
elithrar Oct 24, 2024
925dd82
edits
elithrar Oct 24, 2024
e858bb1
naming steps
elithrar Oct 24, 2024
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
15 changes: 15 additions & 0 deletions src/content/changelogs/workflows.yaml
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/).
6 changes: 6 additions & 0 deletions src/content/docs/workers/configuration/cron-triggers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Cron Triggers allow users to map a cron expression to a Worker using a [`schedul

Cron Triggers are ideal for running periodic jobs, such as for maintenance or calling third-party APIs to collect up-to-date data. Workers scheduled by Cron Triggers will run on underutilized machines to make the best use of Cloudflare's capacity and route traffic efficiently.

:::note

Cron Triggers can also be combined with [Workflows](/workflows/) to trigger multi-step, long-running tasks. You can [bind to a Workflow](/workflows/build/workers-api/) from directly from your Cron Trigger to execute a Workflow on a schedule.
elithrar marked this conversation as resolved.
Show resolved Hide resolved

:::

Cron Triggers execute on UTC time.

## Add a Cron Trigger
Expand Down
10 changes: 10 additions & 0 deletions src/content/docs/workers/runtime-apis/bindings/workflows.mdx
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.

---
153 changes: 151 additions & 2 deletions src/content/docs/workers/wrangler/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Wrangler offers a number of commands to manage your Cloudflare Workers.
- [`r2 object`](#r2-object) - Manage Workers R2 objects.
- [`secret`](#secret) - Manage the secret variables for a Worker.
- [`secret:bulk`](#secretbulk) - Manage multiple secret variables for a Worker.
- [`workflows`](#workflows) - Manage and configure Workflows.
- [`tail`](#tail) - Start a session to livestream logs from a deployed Worker.
- [`pages`](#pages) - Configure Cloudflare Pages.
- [`queues`](#queues) - Configure Workers Queues.
Expand Down Expand Up @@ -138,10 +139,10 @@ wrangler docs [<COMMAND>]

## `init`

:::note
:::note


The `init` command will be removed in a future version. Please use `npm create cloudflare@latest` instead.
The `init` command will be removed in a future version. Please use `npm create cloudflare@latest` instead.


:::
Expand Down Expand Up @@ -1244,6 +1245,154 @@ Finished processing secrets JSON file:
🚨 1 secrets failed to upload
```

## `workflows`

:::note

The `wrangler workflows` command requires Wrangler version `3.83.0` or greater. Use `npx wrangler@latest` to always use the latest Wrangler version when invoking commands.

:::

Manage and configure [Workflows](/workflows/).

### `list`

Lists the registered Workflows for this account.

```sh
wrangler workflows list
```

- `--page` <Type text="number" /> <MetaInfo text="optional" />
- Show a specific page from the listing. You can configure page size using "per-page".
- `--per-page` <Type text="number" /> <MetaInfo text="optional" />
- Configure the maximum number of Workflows to show per page.

### `instances`

Manage and interact with specific instances of a Workflow.

### `instances list`

List Workflow instances.

```sh
wrangler workflows instances list <WORKFLOW_NAME> [OPTIONS]
```

- `WORKFLOW_NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of a registered Workflow.

### `instances describe`

Describe a specific instance of a Workflow, including its current status, any persisted state, and per-step outputs.

```sh
wrangler workflows instances describe <WORKFLOW_NAME> <ID> [OPTIONS]
```

- `WORKFLOW_NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of a registered Workflow.
- `ID` <Type text="string" /> <MetaInfo text="required" />
- The ID of a Workflow instance. You can optionally provide `latest` to refer to the most recently created instance of a Workflow.

```sh
# Passing `latest` instead of an explicit ID will describe the most recently queued instance
wrangler workflows instances describe my-workflow latest
```
```sh output
Workflow Name: my-workflow
Instance Id: 51c73fc8-7fd5-47d9-bd82-9e301506ee72
Version Id: cedc33a0-11fa-4c26-8a8e-7d28d381a291
Status: ✅ Completed
Trigger: 🌎 API
Queued: 10/16/2024, 2:00:39 PM
Success: ✅ Yes
Start: 10/16/2024, 2:00:39 PM
End: 10/16/2024, 2:01:40 PM
Duration: 1 minute
# Remaining output truncated
```

### `instances terminate`

Terminate (permanently stop) a Workflow instance.

```sh
wrangler workflows instances terminate <WORKFLOW_NAME> <ID> [OPTIONS]
```

- `WORKFLOW_NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of a registered Workflow.
- `ID` <Type text="string" /> <MetaInfo text="required" />
- The ID of a Workflow instance.

{/*
### `instances pause`
elithrar marked this conversation as resolved.
Show resolved Hide resolved

Pause (until resumed) a Workflow instance.

```sh
wrangler workflows instances pause <WORKFLOW_NAME> <ID> [OPTIONS]
```

- `WORKFLOW_NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of a registered Workflow.
- `ID` <Type text="string" /> <MetaInfo text="required" />
- The ID of a Workflow instance.

### `instances resume`
elithrar marked this conversation as resolved.
Show resolved Hide resolved

Resume a paused Workflow instance.

```sh
wrangler workflows instances resume <WORKFLOW_NAME> <ID> [OPTIONS]
```

- `WORKFLOW_NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of a registered Workflow.
- `ID` <Type text="string" /> <MetaInfo text="required" />
- The ID of a Workflow instance.

*/}

### `describe`

```sh
wrangler workflows describe <WORKFLOW_NAME> [OPTIONS]
```

- `WORKFLOW_NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of a registered Workflow.

### `trigger`

Trigger (create) a Workflow instance.
```sh
wrangler workflows describe <WORKFLOW_NAME> <PARAMS> [OPTIONS]
```

- `WORKFLOW_NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of a registered Workflow.
- `PARAMS` <Type text="string" /> <MetaInfo text="optional" />
- The parameters to pass to the Workflow as an event. Must be a JSON-encoded string.

```sh
# Pass optional params to the Workflow.
wrangler workflows instances trigger my-workflow '{"hello":"world"}'
```

### `delete`

Delete (unregister) a Workflow.

```sh
wrangler workflows delete <WORKFLOW_NAME> [OPTIONS]
```

- `WORKFLOW_NAME` <Type text="string" /> <MetaInfo text="required" />
- The name of a registered Workflow.

## `tail`

Start a session to livestream logs from a deployed Worker.
Expand Down
104 changes: 104 additions & 0 deletions src/content/docs/workflows/build/events-and-parameters.mdx
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 { WorkflowEntrypoint, WorkflowStep, WorkflowEvent} from 'cloudflare:workers';

export class MyWorkflow extends WorkflowEntrypoint {
// 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.
13 changes: 13 additions & 0 deletions src/content/docs/workflows/build/index.mdx
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 />
Loading
Loading