Skip to content

Conversation

@Oxyjun
Copy link
Contributor

@Oxyjun Oxyjun commented Feb 14, 2025

Summary

Screenshots (optional)

Documentation checklist

  • The documentation style guide has been adhered to.
  • If a larger change - such as adding a new page- an issue has been opened in relation to any incorrect or out of date information that this PR fixes.
  • Files which have changed name or location have been allocated redirects.

@Oxyjun Oxyjun requested review from a team, elithrar and vy-ton as code owners February 14, 2025 16:54
@github-actions github-actions bot added the product:durable-objects Durable Objects: https://developers.cloudflare.com/workers/learning/using-durable-objects/ label Feb 14, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Feb 14, 2025


## Alarms in local Development

Currently, when developing locally (using `npx wrangler dev`), Durable Object [alarm methods](/durable-objects/api/alarms) may fail after a hot reload (if you edit the code while the code is running locally).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lambrospetrou I don't see this behavior where alarm methods work if no hot deploy. I'm not making code edits

https://paste.cfdata.org/JQ6bKUqtiYtrTKY3sbMC is the Worker code im running

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[wrangler:inf] Ready on http://localhost:8787
BOOM :: Promise { <pending> }
[wrangler:inf] GET / 200 OK (13ms)
[wrangler:inf] GET /favicon.ico 200 OK (2ms)
ALARM :: 1739555485366
ALARM :: 1739555490366
ALARM :: 1739555495366

I added some await as well since the alarm API is officially async:

[wrangler:inf] Ready on http://localhost:8787
BOOM :: 1739555410678
[wrangler:inf] GET / 200 OK (17ms)
[wrangler:inf] GET /favicon.ico 200 OK (2ms)
ALARM :: 1739555415679
ALARM :: 1739555420679
ALARM :: 1739555425679
ALARM :: 1739555430679

Works for both versions to me.

If you set an alarm very in the future, maybe the local miniflare implementation doesn't work correctly once the DO hibernates?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you set an alarm very in the future, maybe the local miniflare implementation doesn't work correctly once the DO hibernates?

No, I figured out my code was incorrect. I was calling setAlarm in my constructor, forgetting that when the alarm is invoked the DO constructor is called.

Which raises a different clarifying question: is the DO constructor always called when the alarm handler runs? I see this with wrangler dev. Is it local miniflare only behavior?

Sample output:

constructor
currentCount: 0
alarm: 1739570888980
constructor
currentCount: 1
alarm: 1739570898985
constructor
currentCount: 2
alarm: 1739570908991

Our docs say: "Durable Objects alarms allow you to schedule the Durable Object to be woken up at a time in the future."

I get that if your DO is inactive, then the alarm would start up the DO, ie run the constructor. But if your scheduled alarm time occurs when the DO is already running, then what happens (I assume the constructor is not invoked)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which raises a different clarifying question: is the DO constructor always called when the alarm handler runs? I see this with wrangler dev. Is it local miniflare only behavior?

Seems to be local-only behavior. With Workers Logs on my deployed Worker, I dont see the additional constructor calls.

@Oxyjun worth documenting this behavior as well, assuming its expected cc @lambrospetrou @joshthoward ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩷🩷🩷

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor is called only when the DO cold starts. If an alarm is very far apart from the previous run and the DO hibernates inbetween then yes the constructor would run each time the alarm would run.
But if the alarm is much more often, e.g. every second, then the constructor won't be running for every alarm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a problem to set an alarm within the constructor. I did that in my example case above too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor is called only when the DO cold starts. If an alarm is very far apart from the previous run and the DO hibernates inbetween then yes the constructor would run each time the alarm would run. But if the alarm is much more often, e.g. every second, then the constructor won't be running for every alarm.

So it's not determined by whether you're using local-only vs deploy remote?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Although the time until the DO hibernates locally vs remote could be different. I dont know the exact values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming. @vy-ton I think this PR is good to merge as far as "Known issues" are concerned, let me know if you're happy to merge :D

@Oxyjun Oxyjun merged commit a24f900 into production Feb 19, 2025
11 checks passed
@Oxyjun Oxyjun deleted the jun/do/known-limits-alarms-local-dev branch February 19, 2025 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product:durable-objects Durable Objects: https://developers.cloudflare.com/workers/learning/using-durable-objects/ size/xs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants