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

Named entrypoints #13861

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
85 changes: 84 additions & 1 deletion content/workers/runtime-apis/bindings/service-bindings/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,90 @@ export default class extends WorkerEntrypoint {

## Named entrypoints

TODO: Example
Additionally, it is also possible to export any number of named `WorkerEntrypoint` classes from within a single Worker, in addition to the default export. These can also be bound directly to with a Service binding.
irvinebroque marked this conversation as resolved.
Show resolved Hide resolved

This can be useful if you have multiple pieces of compute that are grouped together in some respect. For example, you might create different `WorkerEntrypoint`s containing a set of operations available for each permission role in your application.
irvinebroque marked this conversation as resolved.
Show resolved Hide resolved

```toml
---
filename: wrangler.toml
---
name = "todo-app"

[[d1_databases]]
binding = "D1"
database_name = "todo-app-db"
database_id = "<unique-ID-for-your-database>"
```

```js
import { WorkerEntrypoint } from "cloudflare:workers";

export class AdminEntrypoint extends WorkerEntrypoint {
async createUser(username) {
await this.env.D1.prepare("INSERT INTO users (username) VALUES (?)")
.bind(username)
.run();
}

async deleteUser(username) {
await this.env.D1.prepare("DELETE FROM users WHERE username = ?")
.bind(username)
.run();
}
}

export class UserEntrypoint extends WorkerEntrypoint {
async getTasks(userId) {
return await this.env.D1.prepare(
"SELECT title FROM tasks WHERE user_id = ?"
)
.bind(userId)
.all();
}

async createTask(userId, title) {
await this.env.D1.prepare(
"INSERT INTO tasks (user_id, title) VALUES (?, ?)"
)
.bind(userId, title)
.run();
}
}

export default class extends WorkerEntrypoint {
async fetch(request, env) {
return new Response("Hello from my to do app");
}
}
```

You can bind to `AdminEntrypoint` and `UserEntrypoint` with a Service binding:
irvinebroque marked this conversation as resolved.
Show resolved Hide resolved

```toml
---
filename: wrangler.toml
---
name = "admin-app"

[[services]]
binding = "ADMIN"
service = "todo-app"
entrypoint = "AdminEntrypoint"
```

```js
export default {
async fetch(request, env) {
await env.ADMIN.createUser("aNewUser");
return new Response("Hello from admin app");
},
};
```

More information about configuring [D1](/workers/wrangler/configuration/#d1-databases) and [Service bindings](/workers/wrangler/configuration/#service-bindings) can be found in the Wrangler documentation.
irvinebroque marked this conversation as resolved.
Show resolved Hide resolved

Additionally, a complete example of this to do app, as well as another example of a Discord bot built with named entrypoints, can be found in our [js-rpc-and-entrypoints-demo repository on GitHub](https://github.com/cloudflare/js-rpc-and-entrypoints-demo).
irvinebroque marked this conversation as resolved.
Show resolved Hide resolved

## Further reading

Expand Down
11 changes: 8 additions & 3 deletions content/workers/wrangler/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ To bind Queues to your producer Worker, assign an array of the below object to t
- `binding` {{<type>}}string{{</type>}} {{<prop-meta>}}required{{</prop-meta>}}

- The binding name used to refer to the queue in your Worker. The binding must be [a valid JavaScript variable name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#variables). For example, `binding = "MY_QUEUE"` or `binding = "productionQueue"` would both be valid names for the binding.

- `delivery_delay` {{<type>}}number{{</type>}} {{<prop-meta>}}optional{{</prop-meta>}}

- The number of seconds to [delay messages sent to a queue](/queues/reference/batching-retries/#delay-messages) for by default. This can be overridden on a per-message or per-batch basis.
Expand Down Expand Up @@ -762,7 +762,7 @@ To bind Queues to your consumer Worker, assign an array of the below object to t

- The maximum number of concurrent consumers allowed to run at once. Leaving this unset will mean that the number of invocations will scale to the [currently supported maximum](/queues/platform/limits/).
- Refer to [Consumer concurrency](/queues/reference/consumer-concurrency/) for more information on how consumers autoscale, particularly when messages are retried.

- `retry_delay` {{<type>}}number{{</type>}} {{<prop-meta>}}optional{{</prop-meta>}}

- The number of seconds to [delay retried messages](/queues/reference/batching-retries/#delay-messages) for by default, before they are re-delivered to the consumer. This can be overridden on a per-message or per-batch basis [when retrying messages](/queues/reference/batching-retries/#explicit-acknowledgement-and-retries).
Expand Down Expand Up @@ -889,6 +889,10 @@ To bind other Workers to your Worker, assign an array of the below object to the

- The name of the Worker.

- `entrypoint` {{<type>}}string{{</type>}} {{<prop-meta>}}optional{{</prop-meta>}}

- The name of the entrypoint to bind to, if not the default export.
irvinebroque marked this conversation as resolved.
Show resolved Hide resolved

{{</definitions>}}

Example:
Expand All @@ -898,14 +902,15 @@ Example:
header: wrangler.toml
---
services = [
{ binding = "<BINDING_NAME>", service = "<WORKER_NAME>" }
{ binding = "<BINDING_NAME>", service = "<WORKER_NAME>", entrypoint = "<ENTRYPOINT_NAME>" }
]

# or

[[services]]
binding = "<BINDING_NAME>"
service = "<WORKER_NAME>"
entrypoint = "<ENTRYPOINT_NAME>"
```

### Analytics Engine Datasets
Expand Down
Loading