Add protection against stale sync records
This commit is contained in:
@@ -94,13 +94,27 @@ export class SyncService {
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
const isActiveSyncConflict = err instanceof DatabaseError && err.code === "23505" && (await this.getActiveSync());
|
||||
if (isActiveSyncConflict) {
|
||||
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);
|
||||
return;
|
||||
}
|
||||
else if (id) {
|
||||
}
|
||||
else {
|
||||
if (id) {
|
||||
await db.updateTable("product_syncs")
|
||||
.set({ syncing_printful_product_ids: [], ended_at: new Date(), has_failed: true })
|
||||
.where("id", "=", id)
|
||||
@@ -108,6 +122,8 @@ export class SyncService {
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async getActiveSync() {
|
||||
|
||||
Reference in New Issue
Block a user