@@ -188,33 +188,32 @@ const run = async (snap, data = null) => {
188
188
const handleWorker = async ( ) => {
189
189
const queueControllerRef = admin . firestore ( ) . collection ( 'queue_controller' )
190
190
191
- const queueDoc = await queueControllerRef . get ( )
192
- if ( queueDoc . empty ) {
191
+ let queueControllerSnap = await queueControllerRef . get ( )
192
+ if ( queueControllerSnap . empty ) {
193
193
await queueControllerRef . add ( {
194
194
running : false
195
195
} )
196
+ queueControllerSnap = await queueControllerRef . get ( )
196
197
}
197
-
198
- const queueControllerSnap = await queueControllerRef . get ( )
199
- const queueController = queueControllerSnap . docs [ 0 ]
198
+ let queueController = queueControllerSnap . docs [ 0 ]
200
199
201
200
try {
202
201
// console.log('queueController', queueController.data())
203
202
const notificationRef = admin . firestore ( ) . collection ( 'ecom_notifications' )
204
203
const query = notificationRef . where ( 'ready_at' , '<=' , admin . firestore . Timestamp . now ( ) . toMillis ( ) )
205
204
const queueState = queueController . data ( )
206
205
if ( queueState . running ) {
207
- if ( ! queueState . storeIds || ! queueState . storeIds . length ) {
206
+ if ( ! queueState . store_ids || ! queueState . store_ids . length ) {
208
207
return
209
208
}
210
- query . where ( 'store_id' , 'not-in' , queueState . storeIds )
209
+ query . where ( 'store_id' , 'not-in' , queueState . store_ids )
211
210
}
212
211
query . orderBy ( 'ready_at' ) . limit ( 20 )
213
212
214
213
const notificationDocs = await query . get ( )
215
214
// console.log('notification docs', notificationDocs.empty)
215
+ const storeIds = [ ]
216
216
if ( ! notificationDocs . empty ) {
217
- const storeIds = [ ]
218
217
const docsToRun = [ ]
219
218
notificationDocs . forEach ( doc => {
220
219
const data = doc . data ( )
@@ -233,12 +232,25 @@ const handleWorker = async () => {
233
232
} , { merge : true } )
234
233
await Promise . allSettled ( docsToRun )
235
234
}
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 )
238
252
} 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 )
242
254
} finally {
243
255
const lastExcution = queueController . data ( ) . last_excution
244
256
if ( differenceInMinutes ( admin . firestore . Timestamp . now ( ) . toDate ( ) , lastExcution . toDate ( ) ) > 9 ) {
0 commit comments