Skip to content

Commit 3b821c8

Browse files
committed
remove old unused data
1 parent 7f17067 commit 3b821c8

File tree

1 file changed

+23
-5
lines changed
  • packages/cloudflare/src/api/durable-objects

1 file changed

+23
-5
lines changed

packages/cloudflare/src/api/durable-objects/queue.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
111111

112112
throw new RecoverableError(`An unknown error occurred while revalidating ${host}${url}`);
113113
}
114+
// Everything went well, we can update the sync table
115+
// We use unixepoch here,it also works with Date.now()/1000, but not with Date.now() alone.
116+
// TODO: This needs to be investigated
117+
this.sql.exec(
118+
"INSERT OR REPLACE INTO sync (id, lastSuccess, buildId) VALUES (?, unixepoch(), ?)",
119+
// We cannot use the deduplication id because it's not unique per route - every time a route is revalidated, the deduplication id is different.
120+
`${host}${url}`,
121+
process.env.__NEXT_BUILD_ID
122+
);
114123
// If everything went well, we can remove the route from the failed state
115124
this.routeInFailedState.delete(msg.MessageDeduplicationId);
116125
} catch (e) {
@@ -170,9 +179,10 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
170179
}
171180
this.routeInFailedState.set(msg.MessageDeduplicationId, updatedFailedState);
172181
this.sql.exec(
173-
"INSERT OR REPLACE INTO failed_state (id, data) VALUES (?, ?)",
182+
"INSERT OR REPLACE INTO failed_state (id, data, buildId) VALUES (?, ?, ?)",
174183
msg.MessageDeduplicationId,
175-
JSON.stringify(updatedFailedState)
184+
JSON.stringify(updatedFailedState),
185+
process.env.__NEXT_BUILD_ID
176186
);
177187
// We probably want to do something if routeInFailedState is becoming too big, at least log it
178188
await this.addAlarm();
@@ -198,10 +208,14 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
198208
// We only restore the failed state and the alarm
199209
async initState() {
200210
// We store the failed state as a blob, we don't want to do anything with it anyway besides restoring
201-
this.sql.exec("CREATE TABLE IF NOT EXISTS failed_state (id TEXT PRIMARY KEY, data TEXT)");
211+
this.sql.exec("CREATE TABLE IF NOT EXISTS failed_state (id TEXT PRIMARY KEY, data TEXT, buildId TEXT)");
202212

203213
// We create the sync table to handle eventually consistent incremental cache
204-
this.sql.exec("CREATE TABLE IF NOT EXISTS sync (id TEXT PRIMARY KEY, lastSuccess INTEGER)");
214+
this.sql.exec("CREATE TABLE IF NOT EXISTS sync (id TEXT PRIMARY KEY, lastSuccess INTEGER, buildId TEXT)");
215+
216+
// Before doing anything else, we clear the DB for any potential old data
217+
this.sql.exec("DELETE FROM failed_state WHERE buildId != ?", process.env.__NEXT_BUILD_ID);
218+
this.sql.exec("DELETE FROM sync WHERE buildId != ?", process.env.__NEXT_BUILD_ID);
205219

206220
const failedStateCursor = this.sql.exec<{ id: string; data: string }>("SELECT * FROM failed_state");
207221
for (const row of failedStateCursor) {
@@ -222,7 +236,11 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
222236
const isNewer = this.sql
223237
.exec<{
224238
isNewer: number;
225-
}>("SELECT COUNT(*) as isNewer FROM sync WHERE id = ? AND lastSuccess > ?", `${msg.MessageBody.host}${msg.MessageBody.url}`, Math.round(msg.MessageBody.lastModified / 1000))
239+
}>(
240+
"SELECT COUNT(*) as isNewer FROM sync WHERE id = ? AND lastSuccess > ?",
241+
`${msg.MessageBody.host}${msg.MessageBody.url}`,
242+
Math.round(msg.MessageBody.lastModified / 1000)
243+
)
226244
.one().isNewer;
227245

228246
return isNewer > 0;

0 commit comments

Comments
 (0)