Skip to content

Commit 1003595

Browse files
committed
Add runtime API reference docs for Worker accessible interfaces
1 parent a891d45 commit 1003595

File tree

5 files changed

+422
-62
lines changed

5 files changed

+422
-62
lines changed

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

Lines changed: 45 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: 4
76
---
87

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

2120
Notably:
2221

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

2726
:::note[How are alarms different from Cron Triggers?]
2827

29-
3028
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.
3129

3230
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.
3331

34-
3532
:::
3633

3734
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/).
@@ -40,94 +37,81 @@ Alarms can be used to build distributed primitives, like queues or batching of w
4037

4138
### getAlarm
4239

40+
- `getAlarm()`: <Type text ='number | null' />
4341

44-
45-
* `getAlarm()`: <Type text ='number | null' />
46-
47-
* 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`.
48-
49-
42+
- 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`.
5043

5144
### setAlarm
5245

46+
- <code>
47+
setAlarm(scheduledTimeMs <Type text="number" />)
48+
</code>
49+
: <Type text="void" />
5350

54-
55-
* <code>setAlarm(scheduledTimeMs <Type text='number'/>)</code>: <Type text='void' />
56-
57-
* Set the time for the alarm to run at in number of milliseconds elapsed since the UNIX epoch.
58-
59-
51+
- Set the time for the alarm to run at in number of milliseconds elapsed since the UNIX epoch.
6052

6153
### deleteAlarm
6254

55+
- `deleteAlarm()`: <Type text='void' />
6356

57+
- Unset the alarm if there is a currently set alarm.
6458

65-
* `deleteAlarm()`: <Type text='void' />
66-
67-
* Unset the alarm if there is a currently set alarm.
68-
69-
* Calling `deleteAlarm()` inside the `alarm()` handler may prevent retries on a best-effort basis, but is not guaranteed.
70-
71-
59+
- Calling `deleteAlarm()` inside the `alarm()` handler may prevent retries on a best-effort basis, but is not guaranteed.
7260

7361
## Handler methods
7462

7563
### alarm
7664

65+
- `alarm()`: <Type text='void' />
7766

67+
- Called by the system when a scheduled alarm time is reached.
7868

79-
* `alarm()`: <Type text='void' />
80-
81-
* Called by the system when a scheduled alarm time is reached.
82-
83-
* 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.
84-
85-
* This method can be `async`.
86-
69+
- 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.
8770

71+
- This method can be `async`.
8872

8973
## Example
9074

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

93-
* The `alarm()` handler will be called once every time an alarm fires.
94-
* If an unexpected error terminates the Durable Object, the `alarm()` handler will be re-instantiated on another machine.
95-
* Following a short delay, the `alarm()` handler will run from the beginning on the other machine.
77+
- The `alarm()` handler will be called once every time an alarm fires.
78+
- If an unexpected error terminates the Durable Object, the `alarm()` handler will be re-instantiated on another machine.
79+
- Following a short delay, the `alarm()` handler will run from the beginning on the other machine.
9680

9781
```js
98-
import { DurableObject } from 'cloudflare:workers';
82+
import { DurableObject } from "cloudflare:workers";
9983

10084
export default {
101-
async fetch(request, env) {
102-
let id = env.ALARM_EXAMPLE.idFromName("foo");
103-
return await env.ALARM_EXAMPLE.get(id).fetch(request);
104-
},
85+
async fetch(request, env) {
86+
let id = env.ALARM_EXAMPLE.idFromName("foo");
87+
return await env.ALARM_EXAMPLE.get(id).fetch(request);
88+
},
10589
};
10690

10791
const SECONDS = 1000;
10892

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

129113
## Related resources
130114

131-
* Understand how to [use the Alarms API](/durable-objects/examples/alarms-api/) in an end-to-end example.
132-
* Read the [Durable Objects alarms announcement blog post](https://blog.cloudflare.com/durable-objects-alarms/).
133-
* Review the [Storage API](/durable-objects/api/storage-api/) documentation for Durable Objects.
115+
- Understand how to [use the Alarms API](/durable-objects/examples/alarms-api/) in an end-to-end example.
116+
- Read the [Durable Objects alarms announcement blog post](https://blog.cloudflare.com/durable-objects-alarms/).
117+
- Review the [Storage API](/durable-objects/api/storage-api/) documentation for Durable Objects.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 to by [`DurableObjectNamespace::get`](./namespace.mdx) to obtain a stub for submitting requests to the Durable Object class.
13+
14+
Note that creating an ID for a Durable Object instance does not invoke the constructor of the Durable Object. This is done lazily when calling [`DurableObjectNamespace::get`](../namespace/#get) to create a [`DurableObjectStub`](../stub) from a `DurableObjectId`. This ensures that objects are not constructed unless their methods can be directly invoked.
15+
16+
:::note[`DurableObjectId`]
17+
18+
If you are experiencing an issue with a particular Durable Object instance the `DurableObjectId` corresponding to this particular instance is very helpful for obtaining a resolution through Cloudflare support. It is advisable to log a `DurableObjectId` from a Worker and include it in support requests where applicable.
19+
20+
:::
21+
22+
## Methods
23+
24+
### `toString`
25+
26+
#### Description
27+
28+
`toString` converts a `DurableObjectId` to a 64 digit hex string. This string is useful for logging purposes or storing the `DurableObjectId` elsewhere, e.g. in a session cookie. This string can be used to reconstruct a `DurableObjectId` via `DurableObjectNamespace::idFromString`.
29+
30+
```js
31+
// Create a new unique ID
32+
const id = env.MY_DURABLE_OBJECT.newUniqueId();
33+
// Save the unique ID elsewhere, e.g. a session cookie via id.toString()
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 value
44+
45+
A 64 digit hex string.
46+
47+
### `equals`
48+
49+
#### Description
50+
51+
`equals` is used to compare equality between two instances of `DurableObjectId`.
52+
53+
```js
54+
const id1 = env.MY_DURABLE_OBJECT.newUniqueId();
55+
const id2 = env.MY_DURABLE_OBJECT.newUniqueId();
56+
console.assert(!id1.equals(id2), "This should always be true");
57+
```
58+
59+
#### Parameters
60+
61+
A required `DurableObjectId` to compare against.
62+
63+
#### Return value
64+
65+
A boolean. True if equal and false otherwise.
66+
67+
## Properties
68+
69+
### `name`
70+
71+
#### Description
72+
73+
`name` is an optional property of a `DurableObjectId`, which returns the name that was used to create the `DurableObjectId` via [`DurableObjectNamespace::idFromName`](../namespace/#idfromname). This value is undefined if the `DurableObjectId` was constructed using [`DurableObjectNamespace::newUniqueId`](../namespace/#newuniqueid).
74+
75+
```js
76+
const id1 = env.MY_DURABLE_OBJECT.newUniqueId();
77+
const id2 = env.MY_DURABLE_OBJECT.idFromName("foo");
78+
console.assert(id1.name === undefined, "This should always be true");
79+
console.assert(id2.name === "foo", "This should always be true");
80+
```
81+
82+
## Related resources
83+
84+
- [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)