You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/durable-objects/api/alarms.mdx
+45-61Lines changed: 45 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,7 @@
2
2
title: Alarms
3
3
pcx_content_type: concept
4
4
sidebar:
5
-
order: 1
6
-
5
+
order: 4
7
6
---
8
7
9
8
import { Type } from"~/components";
@@ -20,18 +19,16 @@ Durable Objects alarms allow you to schedule the Durable Object to be woken up a
20
19
21
20
Notably:
22
21
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.
26
25
27
26
:::note[How are alarms different from Cron Triggers?]
28
27
29
-
30
28
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.
31
29
32
30
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.
33
31
34
-
35
32
:::
36
33
37
34
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
40
37
41
38
### getAlarm
42
39
40
+
-`getAlarm()`: <Typetext='number | null' />
43
41
44
-
45
-
*`getAlarm()`: <Typetext='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`.
* 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.
60
52
61
53
### deleteAlarm
62
54
55
+
-`deleteAlarm()`: <Typetext='void' />
63
56
57
+
- Unset the alarm if there is a currently set alarm.
64
58
65
-
*`deleteAlarm()`: <Typetext='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.
72
60
73
61
## Handler methods
74
62
75
63
### alarm
76
64
65
+
-`alarm()`: <Typetext='void' />
77
66
67
+
- Called by the system when a scheduled alarm time is reached.
78
68
79
-
*`alarm()`: <Typetext='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.
87
70
71
+
- This method can be `async`.
88
72
89
73
## Example
90
74
91
75
This example shows how to both set alarms with the `setAlarm(timestamp)` method and handle alarms with the `alarm()` handler within your Durable Object.
92
76
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.
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
+
constid=env.MY_DURABLE_OBJECT.newUniqueId();
33
+
// Save the unique ID elsewhere, e.g. a session cookie via id.toString()
`equals` is used to compare equality between two instances of `DurableObjectId`.
52
+
53
+
```js
54
+
constid1=env.MY_DURABLE_OBJECT.newUniqueId();
55
+
constid2=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
+
constid1=env.MY_DURABLE_OBJECT.newUniqueId();
77
+
constid2=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");
0 commit comments