Skip to content

Commit 7844b89

Browse files
feat(publisher): change cloudflare durable object cleanup (#1266)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Renamed the inactive cleanup option to a clearer name and increased the default cleanup scheduling interval from 6 hours to 12 hours. * **Tests** * Updated tests to reflect the renamed option and the revised scheduling expectations. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent f724c58 commit 7844b89

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

apps/content/docs/helpers/publisher.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export class PublisherDO extends PublisherDurableObject {
235235
super(ctx, env, {
236236
resume: {
237237
retentionSeconds: 60 * 2, // Retain events for 2 minutes to support resume
238+
cleanupIntervalSeconds: 12 * 60 * 60, // Interval for inactivity checks; if inactive, the DO is cleaned up (default: 12 hours)
238239
},
239240
})
240241
}

packages/publisher-durable-object/src/resume-storage.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,22 +293,22 @@ describe('resumeStorage', () => {
293293
expect(ctx.storage.setAlarm).toHaveBeenCalledOnce()
294294
})
295295

296-
it('uses custom inactiveDataRetentionTime for alarm scheduling', async () => {
296+
it('uses custom cleanupIntervalSeconds for alarm scheduling', async () => {
297297
const ctx = createDurableObjectState()
298298
const storage = new ResumeStorage(ctx, {
299299
retentionSeconds: 60,
300-
inactiveDataRetentionTime: 120,
300+
cleanupIntervalSeconds: 120,
301301
})
302302

303303
await storage.store(JSON.stringify({ data: 'test' }))
304304

305-
// Alarm should be scheduled at retentionSeconds + inactiveDataRetentionTime
305+
// Alarm should be scheduled at cleanupIntervalSeconds
306306
expect(ctx.storage.setAlarm).toHaveBeenCalledWith(
307307
expect.any(Number),
308308
)
309309

310310
const alarmTime = ctx.storage.setAlarm.mock.calls[0]![0]
311-
const expectedDelay = (60 + 120) * 1000
311+
const expectedDelay = 120 * 1000
312312
expect(alarmTime).toBeGreaterThanOrEqual(Date.now() + expectedDelay - 1000)
313313
expect(alarmTime).toBeLessThanOrEqual(Date.now() + expectedDelay + 1000)
314314
})

packages/publisher-durable-object/src/resume-storage.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ export interface ResumeStorageOptions {
1818
retentionSeconds?: number
1919

2020
/**
21-
* How long (in seconds) of inactivity before auto-deleting the durable object's data.
22-
* Inactivity means no active WebSocket connections and no events within the retention period.
21+
* Interval (in seconds) between cleanup checks for the Durable Object.
2322
*
24-
* The alarm is scheduled at `retentionSeconds + inactiveDataRetentionTime`.
23+
* At each interval, verify whether the Durable Object is inactive
24+
* (no active WebSocket connections and no stored events). If inactive, all
25+
* data is deleted to free resources; otherwise, another check is scheduled.
2526
*
26-
* @default 6 * 60 * 60 (6 hours)
27+
* @default 12 * 60 * 60 (12 hours)
2728
*/
28-
inactiveDataRetentionTime?: number
29+
cleanupIntervalSeconds?: number
2930

3031
/**
3132
* Prefix for the resume storage table schema.
@@ -38,7 +39,7 @@ export interface ResumeStorageOptions {
3839

3940
export class ResumeStorage {
4041
private readonly retentionSeconds: number
41-
private readonly inactiveDataRetentionTime: number
42+
private readonly cleanupIntervalSeconds: number
4243
private readonly schemaPrefix: string
4344

4445
private isInitedSchema = false
@@ -54,7 +55,7 @@ export class ResumeStorage {
5455
options: ResumeStorageOptions = {},
5556
) {
5657
this.retentionSeconds = options.retentionSeconds ?? 0
57-
this.inactiveDataRetentionTime = options.inactiveDataRetentionTime ?? 6 * 60 * 60
58+
this.cleanupIntervalSeconds = options.cleanupIntervalSeconds ?? 12 * 60 * 60
5859
this.schemaPrefix = options.schemaPrefix ?? 'orpc:publisher:resume:'
5960
}
6061

@@ -230,6 +231,6 @@ export class ResumeStorage {
230231
}
231232

232233
private scheduleAlarm(): Promise<void> {
233-
return this.ctx.storage.setAlarm(Date.now() + (this.retentionSeconds + this.inactiveDataRetentionTime) * 1000)
234+
return this.ctx.storage.setAlarm(Date.now() + this.cleanupIntervalSeconds * 1000)
234235
}
235236
}

0 commit comments

Comments
 (0)