Add protection against stale sync records

This commit is contained in:
Dominic Ferrando
2026-07-03 14:39:31 -04:00
parent 437300831f
commit 4d23052644
+27 -11
View File
@@ -94,19 +94,35 @@ export class SyncService {
});
}
catch (err) {
const isActiveSyncConflict = err instanceof DatabaseError && err.code === "23505" && (await this.getActiveSync());
if (isActiveSyncConflict) {
log.info("sync already active, enqueuing next sync");
await this.queue.enqueue(opt);
return;
const activeSync = await this.getActiveSync();
const activeSyncConflict = activeSync && err instanceof DatabaseError && err.code === "23505";
if (activeSyncConflict) {
// If active sync has been syncing for too long, clear it and retry this sync
const TEN_MIN = 600000;
if (Date.now() - (activeSync.started_at?.getTime() ?? 0) > TEN_MIN) {
log.info("already active sync exceeded timeout");
await db.updateTable("product_syncs")
.set({ syncing_printful_product_ids: [], ended_at: new Date(), has_failed: true })
.where("id", "=", activeSync.id)
.execute();
await this.sync(opt);
}
else {
log.info("sync already active, enqueuing next sync");
await this.queue.enqueue(opt);
}
}
else if (id) {
await db.updateTable("product_syncs")
.set({ syncing_printful_product_ids: [], ended_at: new Date(), has_failed: true })
.where("id", "=", id)
.execute();
else {
if (id) {
await db.updateTable("product_syncs")
.set({ syncing_printful_product_ids: [], ended_at: new Date(), has_failed: true })
.where("id", "=", id)
.execute();
}
throw err;
}
throw err;
}
}