Skip to content

Commit 0a07a52

Browse files
authored
fix(tasks): properly deal with concurrent (different stores) executions [#33]
1 parent ed6dec0 commit 0a07a52

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

functions/lib/tasks.js

+25-13
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,32 @@ const run = async (snap, data = null) => {
188188
const handleWorker = async () => {
189189
const queueControllerRef = admin.firestore().collection('queue_controller')
190190

191-
const queueDoc = await queueControllerRef.get()
192-
if (queueDoc.empty) {
191+
let queueControllerSnap = await queueControllerRef.get()
192+
if (queueControllerSnap.empty) {
193193
await queueControllerRef.add({
194194
running: false
195195
})
196+
queueControllerSnap = await queueControllerRef.get()
196197
}
197-
198-
const queueControllerSnap = await queueControllerRef.get()
199-
const queueController = queueControllerSnap.docs[0]
198+
let queueController = queueControllerSnap.docs[0]
200199

201200
try {
202201
// console.log('queueController', queueController.data())
203202
const notificationRef = admin.firestore().collection('ecom_notifications')
204203
const query = notificationRef.where('ready_at', '<=', admin.firestore.Timestamp.now().toMillis())
205204
const queueState = queueController.data()
206205
if (queueState.running) {
207-
if (!queueState.storeIds || !queueState.storeIds.length) {
206+
if (!queueState.store_ids || !queueState.store_ids.length) {
208207
return
209208
}
210-
query.where('store_id', 'not-in', queueState.storeIds)
209+
query.where('store_id', 'not-in', queueState.store_ids)
211210
}
212211
query.orderBy('ready_at').limit(20)
213212

214213
const notificationDocs = await query.get()
215214
// console.log('notification docs', notificationDocs.empty)
215+
const storeIds = []
216216
if (!notificationDocs.empty) {
217-
const storeIds = []
218217
const docsToRun = []
219218
notificationDocs.forEach(doc => {
220219
const data = doc.data()
@@ -233,12 +232,25 @@ const handleWorker = async () => {
233232
}, { merge: true })
234233
await Promise.allSettled(docsToRun)
235234
}
236-
queueControllerRef.doc(queueController.id)
237-
.set({ running: false, last_excution: admin.firestore.Timestamp.now() })
235+
236+
queueControllerSnap = await queueControllerRef.get()
237+
queueController = queueControllerSnap.docs[0]
238+
const newQueueState = queueController.data()
239+
if (newQueueState.store_ids) {
240+
storeIds.forEach(storeId => {
241+
const index = newQueueState.store_ids.indexOf(storeId)
242+
if (index > -1) {
243+
newQueueState.store_ids.splice(index, 1)
244+
}
245+
})
246+
newQueueState.running = Boolean(newQueueState.store_ids.length)
247+
} else {
248+
newQueueState.running = false
249+
}
250+
newQueueState.last_excution = admin.firestore.Timestamp.now()
251+
queueControllerRef.doc(queueController.id).set(newQueueState)
238252
} catch (error) {
239-
console.error(error)
240-
queueControllerRef.doc(queueController.id)
241-
.set({ running: false, last_excution: admin.firestore.Timestamp.now() })
253+
logger.error(error)
242254
} finally {
243255
const lastExcution = queueController.data().last_excution
244256
if (differenceInMinutes(admin.firestore.Timestamp.now().toDate(), lastExcution.toDate()) > 9) {

0 commit comments

Comments
 (0)