Skip to content

Commit

Permalink
fix(handle-events): add debug
Browse files Browse the repository at this point in the history
  • Loading branch information
wisley7l committed Sep 19, 2024
1 parent 2edd99b commit e0ffb27
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ exports.updateTokens = functions.pubsub.schedule(cron).onRun(() => {
})
console.log(`-- Sheduled update E-Com Plus tokens '${cron}'`)

exports.eventsqueue = firestore.onDocumentWritten(
exports.eventsQueue = firestore.onDocumentWritten(
`queue/{storeId}/${nameCollectionEvents}/{docId}`,
createExecContext(addEventsQueue)
)
Expand All @@ -159,7 +159,7 @@ console.log('-- Starting the event queue')

const handleEvents = require('./lib/events/handle-events')

exports.onHandlequeue = firestore.onDocumentCreated(
exports.onHandleQueue = firestore.onDocumentCreated(
'running_events/{docId}',
createExecContext(handleEvents)
)
Expand Down
8 changes: 6 additions & 2 deletions functions/lib/events/handle-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const addEventsQueue = async (event) => {
const strStoreId = event.params.storeId
const collectionName = `queue/${strStoreId}/${nameCollectionEvents}`
const eventRef = admin.firestore().collection(collectionName)
console.log(`> ${collectionName}`)

const oldestEventSnapshot = await eventRef
.orderBy('createdAt', 'asc')
Expand All @@ -63,9 +62,11 @@ const addEventsQueue = async (event) => {
const processingTime = processingAt && (now.toMillis() - processingAt.toMillis())
const isProcessing = processingTime && processingTime < limitTimeProcessing
if (!storeId || attempts > 3) {
logger.info('0')
await deleteEvent(storeId, id) // event starts only on creation
await docOldestEvent.ref.delete()
} else if (!processingAt) {
logger.info('1')
await createEvent(storeId, id, documentId)
.then(async (resp) => {
if (resp) {
Expand All @@ -89,6 +90,7 @@ const addEventsQueue = async (event) => {
lastExecuted: documentId
}, { merge: true })
} else if (!isProcessing) {
logger.info('2')
await deleteEvent(storeId, id) // event starts only on creation

const attempts = (oldestEvent.attempts || 0) + 1
Expand All @@ -106,8 +108,10 @@ const addEventsQueue = async (event) => {
}
} else {
// now.toDate().toISOString()
console.log(`${documentId}, ${processingAt.toDate().toISOString()}, ${processingTime}`)
logger.info(`${documentId}, ${processingAt.toDate().toISOString()}, ${processingTime}`)
}
} else {
logger.info('is empty')
}

return null
Expand Down

0 comments on commit e0ffb27

Please sign in to comment.