Skip to content

Commit b6e9709

Browse files
authored
fix(task): manually handle running store ids [#33]
1 parent 03d4527 commit b6e9709

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

functions/lib/tasks.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -200,31 +200,29 @@ const handleWorker = async () => {
200200
try {
201201
// console.log('queueController', queueController.data())
202202
const notificationRef = admin.firestore().collection('ecom_notifications')
203-
const query = notificationRef.where('ready_at', '<=', admin.firestore.Timestamp.now().toMillis())
204203
const queueState = queueController.data()
205-
if (queueState.running) {
206-
if (!queueState.store_ids || !queueState.store_ids.length) {
207-
return
208-
}
209-
query.where('store_id', 'not-in', queueState.store_ids)
204+
if (queueState.running && (!queueState.store_ids || !queueState.store_ids.length)) {
205+
return
210206
}
211-
query.orderBy('ready_at').limit(20)
207+
const query = notificationRef
208+
.where('ready_at', '<=', admin.firestore.Timestamp.now().toMillis())
209+
.orderBy('ready_at').limit(600)
212210

213211
const notificationDocs = await query.get()
214212
// console.log('notification docs', notificationDocs.empty)
215213
const storeIds = []
216214
if (!notificationDocs.empty) {
217215
const docsToRun = []
218216
notificationDocs.forEach(doc => {
219-
const data = doc.data()
220-
if (!storeIds.includes(data.store_id)) {
221-
if (storeIds.length >= 10) {
222-
// https://firebase.google.com/docs/firestore/query-data/queries#limitations_2
223-
return
217+
if (docsToRun.length < 20) {
218+
const data = doc.data()
219+
if (!queueState.store_ids.includes(data.store_id)) {
220+
if (!storeIds.includes(data.store_id)) {
221+
storeIds.push(data.store_id)
222+
}
223+
docsToRun.push(run(doc, data))
224224
}
225-
storeIds.push(data.store_id)
226225
}
227-
docsToRun.push(run(doc, data))
228226
})
229227
queueControllerRef.doc(queueController.id).set({
230228
running: true,

0 commit comments

Comments
 (0)