From c04716006e82f3c61bf4fec08c48a48261f7ff01 Mon Sep 17 00:00:00 2001 From: Alzer Del Zeyger <75466035+Dx-Alz-xD@users.noreply.github.com> Date: Wed, 11 Nov 2026 19:32:00 +0530 Subject: [PATCH 1/2] fix(upcoming): let a single request run the daily sync two requests right after the cutoff could both see an old lastSyncedDate and both rebuild the list, leaving duplicate subjects or an empty list between the delete and the insert. claim the day with an atomic update first, and insert the new subjects before deleting the old ones --- src/lib/services/upcoming-sync.ts | 35 +++++++++++++++++++------------ 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/lib/services/upcoming-sync.ts b/src/lib/services/upcoming-sync.ts index 5f0f52b..5da25c2 100644 --- a/src/lib/services/upcoming-sync.ts +++ b/src/lib/services/upcoming-sync.ts @@ -86,19 +86,28 @@ export async function checkAndSyncUpcomingSlots(): Promise { .sort((a, b) => b.count - a.count) .slice(0, targetUniqueCount); - await UpcomingSlot.findOneAndUpdate( - {}, - { $set: { slot: scheduledSlot, lastSyncedDate: today } }, - { upsert: true, new: true, setDefaultsOnInsert: true }, + const todaysSlot = { $set: { slot: scheduledSlot, lastSyncedDate: today } }; + const wonTheSync = currentSlotDoc + ? await UpcomingSlot.findOneAndUpdate( + { _id: currentSlotDoc._id, lastSyncedDate: { $ne: today } }, + todaysSlot, + ) + : await UpcomingSlot.findOneAndUpdate({}, todaysSlot, { + upsert: true, + new: true, + setDefaultsOnInsert: true, + }); + + if (!wonTheSync) return; + + const freshSubjects = await UpcomingSubject.insertMany( + ranked.map((item) => ({ + subject: item.subject, + slots: item.slots, + })), ); - await UpcomingSubject.deleteMany({}); - if (ranked.length > 0) { - await UpcomingSubject.insertMany( - ranked.map((item) => ({ - subject: item.subject, - slots: item.slots, - })), - ); - } + await UpcomingSubject.deleteMany({ + _id: { $nin: freshSubjects.map((subject) => subject._id) }, + }); } From 53d15b0d198dcefcf34b32c6ad17b98de6ae5849 Mon Sep 17 00:00:00 2001 From: Alzer Del Zeyger <75466035+Dx-Alz-xD@users.noreply.github.com> Date: Tue, 1 Dec 2026 19:29:39 +0530 Subject: [PATCH 2/2] fix(upcoming): undo the day's claim when the subject swap fails if inserting the new subjects or deleting the old ones throws, the request that claimed the sync puts lastSyncedDate (and the slot) back to what they were, so the next request runs the sync again instead of the day being marked done with the old or a partial list --- src/lib/services/upcoming-sync.ts | 35 ++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/lib/services/upcoming-sync.ts b/src/lib/services/upcoming-sync.ts index 5da25c2..0a024e3 100644 --- a/src/lib/services/upcoming-sync.ts +++ b/src/lib/services/upcoming-sync.ts @@ -100,14 +100,29 @@ export async function checkAndSyncUpcomingSlots(): Promise { if (!wonTheSync) return; - const freshSubjects = await UpcomingSubject.insertMany( - ranked.map((item) => ({ - subject: item.subject, - slots: item.slots, - })), - ); - - await UpcomingSubject.deleteMany({ - _id: { $nin: freshSubjects.map((subject) => subject._id) }, - }); + try { + const freshSubjects = await UpcomingSubject.insertMany( + ranked.map((item) => ({ + subject: item.subject, + slots: item.slots, + })), + ); + + await UpcomingSubject.deleteMany({ + _id: { $nin: freshSubjects.map((subject) => subject._id) }, + }); + } catch (error) { + await UpcomingSlot.updateOne( + { _id: wonTheSync._id, lastSyncedDate: today }, + currentSlotDoc?.lastSyncedDate + ? { + $set: { + slot: currentSlotDoc.slot, + lastSyncedDate: currentSlotDoc.lastSyncedDate, + }, + } + : { $unset: { lastSyncedDate: 1 } }, + ); + throw error; + } }