Skip to content

Commit 1e3e856

Browse files
committed
remove old unused data
1 parent b27a0ad commit 1e3e856

File tree

1 file changed

+19
-8
lines changed
  • packages/cloudflare/src/api/durable-objects

1 file changed

+19
-8
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
117117
throw new RecoverableError(`An unknown error occurred while revalidating ${host}${url}`);
118118
}
119119
// Everything went well, we can update the sync table
120-
// We use unixepoch here,it also works with Date.now()/1000, but not with Date.now() alone- we need to investigate this
120+
// We use unixepoch here,it also works with Date.now()/1000, but not with Date.now() alone.
121+
// TODO: This needs to be investigated
121122
this.sql.exec(
122-
"INSERT OR REPLACE INTO sync (id, lastSuccess) VALUES (?, unixepoch())",
123+
"INSERT OR REPLACE INTO sync (id, lastSuccess, buildId) VALUES (?, unixepoch(), ?)",
123124
// We cannot use the deduplication id because it's not unique per route - every time a route is revalidated, the deduplication id is different.
124-
`${host}${url}`
125+
`${host}${url}`,
126+
process.env.__NEXT_BUILD_ID
125127
);
126128
// If everything went well, we can remove the route from the failed state
127129
this.routeInFailedState.delete(msg.MessageDeduplicationId);
@@ -184,9 +186,10 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
184186
}
185187
this.routeInFailedState.set(msg.MessageDeduplicationId, updatedFailedState);
186188
this.sql.exec(
187-
"INSERT OR REPLACE INTO failed_state (id, data) VALUES (?, ?)",
189+
"INSERT OR REPLACE INTO failed_state (id, data, buildId) VALUES (?, ?, ?)",
188190
msg.MessageDeduplicationId,
189-
JSON.stringify(updatedFailedState)
191+
JSON.stringify(updatedFailedState),
192+
process.env.__NEXT_BUILD_ID
190193
);
191194
// We probably want to do something if routeInFailedState is becoming too big, at least log it
192195
await this.addAlarm();
@@ -213,10 +216,14 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
213216
// We only restore the failed state and the alarm
214217
async initState() {
215218
// We store the failed state as a blob, we don't want to do anything with it anyway besides restoring
216-
this.sql.exec("CREATE TABLE IF NOT EXISTS failed_state (id TEXT PRIMARY KEY, data TEXT)");
219+
this.sql.exec("CREATE TABLE IF NOT EXISTS failed_state (id TEXT PRIMARY KEY, data TEXT, buildId TEXT)");
217220

218221
// We create the sync table to handle eventually consistent incremental cache
219-
this.sql.exec("CREATE TABLE IF NOT EXISTS sync (id TEXT PRIMARY KEY, lastSuccess INTEGER)");
222+
this.sql.exec("CREATE TABLE IF NOT EXISTS sync (id TEXT PRIMARY KEY, lastSuccess INTEGER, buildId TEXT)");
223+
224+
// Before doing anything else, we clear the DB for any potential old data
225+
this.sql.exec("DELETE FROM failed_state WHERE buildId != ?", process.env.__NEXT_BUILD_ID);
226+
this.sql.exec("DELETE FROM sync WHERE buildId != ?", process.env.__NEXT_BUILD_ID);
220227

221228
const failedStateCursor = this.sql.exec<{ id: string; data: string }>("SELECT * FROM failed_state");
222229
for (const row of failedStateCursor) {
@@ -237,7 +244,11 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
237244
const isNewer = this.sql
238245
.exec<{
239246
isNewer: number;
240-
}>("SELECT COUNT(*) as isNewer FROM sync WHERE id = ? AND lastSuccess > ?", `${msg.MessageBody.host}${msg.MessageBody.url}`, Math.round(msg.MessageBody.lastModified / 1000))
247+
}>(
248+
"SELECT COUNT(*) as isNewer FROM sync WHERE id = ? AND lastSuccess > ?",
249+
`${msg.MessageBody.host}${msg.MessageBody.url}`,
250+
Math.round(msg.MessageBody.lastModified / 1000)
251+
)
241252
.one().isNewer;
242253

243254
return isNewer > 0;

0 commit comments

Comments
 (0)