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) { catch (err) {
const isActiveSyncConflict = err instanceof DatabaseError && err.code === "23505" && (await this.getActiveSync()); const activeSync = await this.getActiveSync();
if (isActiveSyncConflict) {
log.info("sync already active, enqueuing next sync"); const activeSyncConflict = activeSync && err instanceof DatabaseError && err.code === "23505";
await this.queue.enqueue(opt); if (activeSyncConflict) {
return; // 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) { else {
await db.updateTable("product_syncs") if (id) {
.set({ syncing_printful_product_ids: [], ended_at: new Date(), has_failed: true }) await db.updateTable("product_syncs")
.where("id", "=", id) .set({ syncing_printful_product_ids: [], ended_at: new Date(), has_failed: true })
.execute(); .where("id", "=", id)
.execute();
}
throw err;
} }
throw err;
} }
} }