Skip to content

Commit adb6dd1

Browse files
committed
remove old unused data
1 parent 3354564 commit adb6dd1

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
@@ -112,6 +112,15 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
112112

113113
throw new RecoverableError(`An unknown error occurred while revalidating ${host}${url}`);
114114
}
115+
// Everything went well, we can update the sync table
116+
// We use unixepoch here,it also works with Date.now()/1000, but not with Date.now() alone.
117+
// TODO: This needs to be investigated
118+
this.sql.exec(
119+
"INSERT OR REPLACE INTO sync (id, lastSuccess, buildId) VALUES (?, unixepoch(), ?)",
120+
// We cannot use the deduplication id because it's not unique per route - every time a route is revalidated, the deduplication id is different.
121+
`${host}${url}`,
122+
process.env.__NEXT_BUILD_ID
123+
);
115124
// If everything went well, we can remove the route from the failed state
116125
this.routeInFailedState.delete(msg.MessageDeduplicationId);
117126
} catch (e) {
@@ -174,9 +183,10 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
174183
}
175184
this.routeInFailedState.set(msg.MessageDeduplicationId, updatedFailedState);
176185
this.sql.exec(
177-
"INSERT OR REPLACE INTO failed_state (id, data) VALUES (?, ?)",
186+
"INSERT OR REPLACE INTO failed_state (id, data, buildId) VALUES (?, ?, ?)",
178187
msg.MessageDeduplicationId,
179-
JSON.stringify(updatedFailedState)
188+
JSON.stringify(updatedFailedState),
189+
process.env.__NEXT_BUILD_ID
180190
);
181191
// We probably want to do something if routeInFailedState is becoming too big, at least log it
182192
await this.addAlarm();
@@ -203,10 +213,14 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
203213
// We only restore the failed state and the alarm
204214
async initState() {
205215
// We store the failed state as a blob, we don't want to do anything with it anyway besides restoring
206-
this.sql.exec("CREATE TABLE IF NOT EXISTS failed_state (id TEXT PRIMARY KEY, data TEXT)");
216+
this.sql.exec("CREATE TABLE IF NOT EXISTS failed_state (id TEXT PRIMARY KEY, data TEXT, buildId TEXT)");
207217

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

211225
const failedStateCursor = this.sql.exec<{ id: string; data: string }>("SELECT * FROM failed_state");
212226
for (const row of failedStateCursor) {
@@ -227,7 +241,11 @@ export class DurableObjectQueueHandler extends DurableObject<CloudflareEnv> {
227241
const isNewer = this.sql
228242
.exec<{
229243
isNewer: number;
230-
}>("SELECT COUNT(*) as isNewer FROM sync WHERE id = ? AND lastSuccess > ?", `${msg.MessageBody.host}${msg.MessageBody.url}`, Math.round(msg.MessageBody.lastModified / 1000))
244+
}>(
245+
"SELECT COUNT(*) as isNewer FROM sync WHERE id = ? AND lastSuccess > ?",
246+
`${msg.MessageBody.host}${msg.MessageBody.url}`,
247+
Math.round(msg.MessageBody.lastModified / 1000)
248+
)
231249
.one().isNewer;
232250

233251
return isNewer > 0;

0 commit comments

Comments
 (0)