From dfb41127c98969d2f7e051065b103cfe7c81393b Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Thu, 29 May 2025 14:22:07 +0100 Subject: [PATCH 1/2] Adding warning for calling setAlarm from inside the DO constructor. --- src/content/docs/durable-objects/api/alarms.mdx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/content/docs/durable-objects/api/alarms.mdx b/src/content/docs/durable-objects/api/alarms.mdx index 807ae8edf12372..5e0c210c0eae03 100644 --- a/src/content/docs/durable-objects/api/alarms.mdx +++ b/src/content/docs/durable-objects/api/alarms.mdx @@ -45,6 +45,12 @@ Alarms can be used to build distributed primitives, like queues or batching of w - Set the time for the alarm to run. Specify the time as the number of milliseconds elapsed since the UNIX epoch. - If you call `setAlarm` when there is already one scheduled, it will override the existing alarm. +:::caution[Calling `setAlarm` inside the constructor] +If you wish to call `setAlarm` inside the constructor of a Durable Object, ensure that you are first checking whether an alarm has already been set. + +This is due to the fact that, if the Durable Object is inactive, the constructor is invoked before the [`alarm` handler](/durable-objects/api/alarms/#alarm). Therefore, if the constructor calls `setAlarm`, it could interfere with the next alarm which has already been set. +::: + ### `deleteAlarm` - `deleteAlarm()`: From 655025ad66fc1aefb0bd3168d67202b0ee0ffc6d Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Thu, 29 May 2025 15:22:47 +0100 Subject: [PATCH 2/2] Clarification of wording --- src/content/docs/durable-objects/api/alarms.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/durable-objects/api/alarms.mdx b/src/content/docs/durable-objects/api/alarms.mdx index 5e0c210c0eae03..6346589585f827 100644 --- a/src/content/docs/durable-objects/api/alarms.mdx +++ b/src/content/docs/durable-objects/api/alarms.mdx @@ -48,7 +48,7 @@ Alarms can be used to build distributed primitives, like queues or batching of w :::caution[Calling `setAlarm` inside the constructor] If you wish to call `setAlarm` inside the constructor of a Durable Object, ensure that you are first checking whether an alarm has already been set. -This is due to the fact that, if the Durable Object is inactive, the constructor is invoked before the [`alarm` handler](/durable-objects/api/alarms/#alarm). Therefore, if the constructor calls `setAlarm`, it could interfere with the next alarm which has already been set. +This is due to the fact that, if the Durable Object wakes up after being inactive, the constructor is invoked before the [`alarm` handler](/durable-objects/api/alarms/#alarm). Therefore, if the constructor calls `setAlarm`, it could interfere with the next alarm which has already been set. ::: ### `deleteAlarm`