Skip to content

Commit 52c9f24

Browse files
joshthowardOxyjunhyperlint-ai[bot]justin-mp
authored andcommitted
Update reference documentation for Worker accessible DO interfaces (#17411)
* Add runtime API reference docs for Worker accessible interfaces * Update data location docs to account for docs for Worker accessible interfaces * Update src/content/docs/durable-objects/api/namespace.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/docs/durable-objects/api/id.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Jun/PCX edits part 1 * Replacing relative paths to absolute paths. * Fixing more invalid relative paths. * Apply suggestions from code review Adding immediately resolvable edits. Co-authored-by: justin-mp <[email protected]> * Adding suggestion. * Apply suggestions from code review * Ensuring consistent wording for description of DurableObjectNamespace. * Update src/content/docs/durable-objects/api/alarms.mdx Co-authored-by: justin-mp <[email protected]> * Update src/content/docs/durable-objects/api/alarms.mdx Co-authored-by: justin-mp <[email protected]> * Update src/content/docs/durable-objects/api/storage-api.mdx Co-authored-by: justin-mp <[email protected]> * "Two seconds" -> "2 seconds", "six seconds" -> "6 seconds" * Adding link to best practices section. * Address remaining comments --------- Co-authored-by: Jun Lee <[email protected]> Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> Co-authored-by: justin-mp <[email protected]>
1 parent 22fc03d commit 52c9f24

File tree

10 files changed

+453
-142
lines changed

10 files changed

+453
-142
lines changed

src/content/docs/durable-objects/api/alarms.mdx

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
title: Alarms
33
pcx_content_type: concept
44
sidebar:
5-
order: 1
6-
5+
order: 6
76
---
87

98
import { Type } from "~/components";
@@ -14,18 +13,16 @@ Durable Objects alarms allow you to schedule the Durable Object to be woken up a
1413

1514
Notably:
1615

17-
* Each Durable Object instance is able to schedule a single alarm at a time by calling `setAlarm()`.
18-
* Alarms have guaranteed at-least-once execution and are retried automatically when the `alarm()` handler throws.
19-
* Retries are performed using exponential backoff starting at a two second delay from the first failure with up to six retries allowed.
16+
- Each Durable Object instance is able to schedule a single alarm at a time by calling `setAlarm()`.
17+
- Alarms have guaranteed at-least-once execution and are retried automatically when the `alarm()` handler throws.
18+
- Retries are performed using exponential backoff starting at a 2 second delay from the first failure with up to 6 retries allowed.
2019

2120
:::note[How are alarms different from Cron Triggers?]
2221

23-
2422
Alarms are more fine grained than [Cron Triggers](/workers/configuration/cron-triggers/#cron-triggers). A Worker can have up to three Cron Triggers configured at once, but it can have an unlimited amount of Durable Objects, each of which can have an alarm set.
2523

2624
Alarms are directly scheduled from within your Durable Object. Cron Triggers, on the other hand, are not programmatic. [Cron Triggers](/workers/configuration/cron-triggers/#cron-triggers) execute based on their schedules, which have to be configured through the Cloudflare dashboard or API.
2725

28-
2926
:::
3027

3128
Alarms can be used to build distributed primitives, like queues or batching of work atop Durable Objects. Alarms also provide a mechanism to guarantee that operations within a Durable Object will complete without relying on incoming requests to keep the Durable Object alive. For a complete example, refer to [Use the Alarms API](/durable-objects/examples/alarms-api/).
@@ -34,94 +31,82 @@ Alarms can be used to build distributed primitives, like queues or batching of w
3431

3532
### getAlarm
3633

34+
- <code>getAlarm()</code>: <Type text="number | null" />
3735

38-
39-
* `getAlarm()`: <Type text ='number | null' />
40-
41-
* If there is an alarm set, then return the currently set alarm time in number of milliseconds elapsed since the UNIX epoch. Otherwise, return `null`.
42-
43-
36+
- If there is an alarm set, then return the currently set alarm time as the number of milliseconds elapsed since the UNIX epoch. Otherwise, return `null`.
4437

4538
### setAlarm
4639

40+
- <code>
41+
{" "}
42+
setAlarm(scheduledTimeMs <Type text="number" />)
43+
</code>
44+
: <Type text="void" />
4745

48-
49-
* <code>setAlarm(scheduledTimeMs <Type text='number'/>)</code>: <Type text='void' />
50-
51-
* Set the time for the alarm to run at in number of milliseconds elapsed since the UNIX epoch.
52-
53-
46+
- Set the time for the alarm to run. Specify the time as the number of milliseconds elapsed since the UNIX epoch.
5447

5548
### deleteAlarm
5649

50+
- `deleteAlarm()`: <Type text='void' />
5751

52+
- Unset the alarm if there is a currently set alarm.
5853

59-
* `deleteAlarm()`: <Type text='void' />
60-
61-
* Unset the alarm if there is a currently set alarm.
62-
63-
* Calling `deleteAlarm()` inside the `alarm()` handler may prevent retries on a best-effort basis, but is not guaranteed.
64-
65-
54+
- Calling `deleteAlarm()` inside the `alarm()` handler may prevent retries on a best-effort basis, but is not guaranteed.
6655

6756
## Handler methods
6857

6958
### alarm
7059

60+
- `alarm()`: <Type text='void' />
7161

62+
- Called by the system when a scheduled alarm time is reached.
7263

73-
* `alarm()`: <Type text='void' />
74-
75-
* Called by the system when a scheduled alarm time is reached.
76-
77-
* The `alarm()` handler has guaranteed at-least-once execution and will be retried upon failure using exponential backoff, starting at two second delays for up to six retries. Retries will be performed if the method fails with an uncaught exception.
78-
79-
* This method can be `async`.
80-
64+
- The `alarm()` handler has guaranteed at-least-once execution and will be retried upon failure using exponential backoff, starting at 2 second delays for up to 6 retries. Retries will be performed if the method fails with an uncaught exception.
8165

66+
- This method can be `async`.
8267

8368
## Example
8469

8570
This example shows how to both set alarms with the `setAlarm(timestamp)` method and handle alarms with the `alarm()` handler within your Durable Object.
8671

87-
* The `alarm()` handler will be called once every time an alarm fires.
88-
* If an unexpected error terminates the Durable Object, the `alarm()` handler will be re-instantiated on another machine.
89-
* Following a short delay, the `alarm()` handler will run from the beginning on the other machine.
72+
- The `alarm()` handler will be called once every time an alarm fires.
73+
- If an unexpected error terminates the Durable Object, the `alarm()` handler may be re-instantiated on another machine.
74+
- Following a short delay, the `alarm()` handler will run from the beginning on the other machine.
9075

9176
```js
92-
import { DurableObject } from 'cloudflare:workers';
77+
import { DurableObject } from "cloudflare:workers";
9378

9479
export default {
95-
async fetch(request, env) {
96-
let id = env.ALARM_EXAMPLE.idFromName("foo");
97-
return await env.ALARM_EXAMPLE.get(id).fetch(request);
98-
},
80+
async fetch(request, env) {
81+
let id = env.ALARM_EXAMPLE.idFromName("foo");
82+
return await env.ALARM_EXAMPLE.get(id).fetch(request);
83+
},
9984
};
10085

10186
const SECONDS = 1000;
10287

10388
export class AlarmExample extends DurableObject {
104-
constructor(ctx, env) {
105-
this.ctx = ctx;
106-
this.storage = ctx.storage;
107-
}
108-
async fetch(request) {
109-
// If there is no alarm currently set, set one for 10 seconds from now
110-
let currentAlarm = await this.storage.getAlarm();
111-
if (currentAlarm == null) {
112-
this.storage.setAlarm(Date.now() + 10 * SECONDS);
113-
}
114-
}
115-
async alarm() {
116-
// The alarm handler will be invoked whenever an alarm fires.
117-
// You can use this to do work, read from the Storage API, make HTTP calls
118-
// and set future alarms to run using this.storage.setAlarm() from within this handler.
119-
}
89+
constructor(ctx, env) {
90+
this.ctx = ctx;
91+
this.storage = ctx.storage;
92+
}
93+
async fetch(request) {
94+
// If there is no alarm currently set, set one for 10 seconds from now
95+
let currentAlarm = await this.storage.getAlarm();
96+
if (currentAlarm == null) {
97+
this.storage.setAlarm(Date.now() + 10 * SECONDS);
98+
}
99+
}
100+
async alarm() {
101+
// The alarm handler will be invoked whenever an alarm fires.
102+
// You can use this to do work, read from the Storage API, make HTTP calls
103+
// and set future alarms to run using this.storage.setAlarm() from within this handler.
104+
}
120105
}
121106
```
122107

123108
## Related resources
124109

125-
* Understand how to [use the Alarms API](/durable-objects/examples/alarms-api/) in an end-to-end example.
126-
* Read the [Durable Objects alarms announcement blog post](https://blog.cloudflare.com/durable-objects-alarms/).
127-
* Review the [Storage API](/durable-objects/api/storage-api/) documentation for Durable Objects.
110+
- Understand how to [use the Alarms API](/durable-objects/examples/alarms-api/) in an end-to-end example.
111+
- Read the [Durable Objects alarms announcement blog post](https://blog.cloudflare.com/durable-objects-alarms/).
112+
- Review the [Storage API](/durable-objects/api/storage-api/) documentation for Durable Objects.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: Durable Object ID
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 2
6+
---
7+
8+
import { Tabs, TabItem } from "~/components";
9+
10+
## Description
11+
12+
The `DurableObjectId` interface refers to a new or existing Durable Object instance. This interface is most frequently used by [`DurableObjectNamespace::get`](/durable-objects/api/namespace/#get) to obtain a stub for submitting requests to a Durable Object instance.
13+
14+
Note that creating an ID for a Durable Object instance does not create the Durable Object. The Durable Object is created lazily after calling [`DurableObjectNamespace::get`](/durable-objects/api/namespace/#get) to create a [`DurableObjectStub`](/durable-objects/api/stub) from a `DurableObjectId`. This ensures that objects are not constructed until they are actually accessed.
15+
16+
:::note[`DurableObjectId`]
17+
18+
If you are experiencing an issue with a particular Durable Object instance, you may wish to log the `DurableObjectId` from your Worker and include it in your Cloudflare support request.
19+
20+
:::
21+
22+
## Methods
23+
24+
### `toString`
25+
26+
`toString` converts a `DurableObjectId` to a 64 digit hex string. This string is useful for logging purposes or storing the `DurableObjectId` elsewhere, for example, in a session cookie. This string can be used to reconstruct a `DurableObjectId` via `DurableObjectNamespace::idFromString`.
27+
28+
```js
29+
// Create a new unique ID
30+
const id = env.MY_DURABLE_OBJECT.newUniqueId();
31+
// Convert the ID to a string to be saved elsewhere, e.g. a session cookie
32+
const session_id = id.toString();
33+
34+
...
35+
// Recreate the ID from the string
36+
const id = env.MY_DURABLE_OBJECT.idFromString(session_id);
37+
```
38+
39+
#### Parameters
40+
41+
- None.
42+
43+
#### Return values
44+
45+
- A 64 digit hex string.
46+
47+
### `equals`
48+
49+
`equals` is used to compare equality between two instances of `DurableObjectId`.
50+
51+
```js
52+
const id1 = env.MY_DURABLE_OBJECT.newUniqueId();
53+
const id2 = env.MY_DURABLE_OBJECT.newUniqueId();
54+
console.assert(!id1.equals(id2), "Different unique ids should never be equal.");
55+
```
56+
57+
#### Parameters
58+
59+
- A required `DurableObjectId` to compare against.
60+
61+
#### Return values
62+
63+
- A boolean. True if equal and false otherwise.
64+
65+
## Properties
66+
67+
### `name`
68+
69+
`name` is an optional property of a `DurableObjectId`, which returns the name that was used to create the `DurableObjectId` via [`DurableObjectNamespace::idFromName`](/durable-objects/api/namespace/#idfromname). This value is undefined if the `DurableObjectId` was constructed using [`DurableObjectNamespace::newUniqueId`](/durable-objects/api/namespace/#newuniqueid).
70+
71+
```js
72+
const uniqueId = env.MY_DURABLE_OBJECT.newUniqueId();
73+
const fromNameId = env.MY_DURABLE_OBJECT.idFromName("foo");
74+
console.assert(uniqueId.name === undefined, "unique ids have no name");
75+
console.assert(
76+
fromNameId.name === "foo",
77+
"name matches parameter to idFromName",
78+
);
79+
```
80+
81+
## Related resources
82+
83+
- [Durable Objects: Easy, Fast, Correct – Choose Three](https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/).

src/content/docs/durable-objects/api/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: API
2+
title: Runtime APIs
33
pcx_content_type: navigation
44
sidebar:
55
order: 4

0 commit comments

Comments
 (0)