Skip to content

Commit

Permalink
chore(check-horus-api): check if the Horus API is ok
Browse files Browse the repository at this point in the history
  • Loading branch information
wisley7l committed Jun 7, 2024
1 parent 7cb999e commit ee2a489
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
26 changes: 16 additions & 10 deletions functions/lib/cron-events-horus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
const { parseDate } = require('./parsers/parse-to-horus')
const Horus = require('./horus/client')
const requestHorus = require('./horus/request')
const checkHorusApi = require('./horus/check-horus-api')
const { sendMessageTopic } = require('./pub-sub/utils')
const { getAllItemsHorusToImport } = require('../lib/integration/imports/utils')
const ecomClient = require('@ecomplus/client')
Expand Down Expand Up @@ -239,26 +240,31 @@ module.exports = async (appSdk) => {
.then((auth) => {
return getAppData({ appSdk, storeId, auth }, true)
})
.then((appData) => {
.then(async (appData) => {
const {
username,
password,
baseURL
} = appData
const horus = new Horus(username, password, baseURL)
const opts = { appData }
const horusApiOk = await checkHorusApi(horus)
const promises = []
promises.push(productsStocksEvents(horus, storeId, opts))
const now = new Date()
if (horusApiOk) {
promises.push(productsStocksEvents(horus, storeId, opts))
const now = new Date()

if ((now.getHours() - 6) % 24 === 0 && now.getMinutes() === 3) {
// run at 3 am (UTC -3) everyday
promises.push(productsPriceEvents(horus, storeId, opts))
}
if ((now.getHours() - 6) % 24 === 0 && now.getMinutes() === 3) {
// run at 3 am (UTC -3) everyday
promises.push(productsPriceEvents(horus, storeId, opts))
}

if (now.getMinutes() % 30 === 0) {
// run at 30 in 30min
promises.push(checkProductsImports({ appSdk, storeId }, horus, opts))
if (now.getMinutes() % 30 === 0) {
// run at 30 in 30min
promises.push(checkProductsImports({ appSdk, storeId }, horus, opts))
}
} else {
console.log('> Horus API Offline')
}

return Promise.all(promises)
Expand Down
30 changes: 30 additions & 0 deletions functions/lib/horus/check-horus-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const requestCheckHorus = (horus, isRetry) => new Promise((resolve, _reject) => {
horus.get('/Teste1')
.then((resp) => {
const { data } = resp
if (data && data.length) {
const [response] = data
if (response.ATIVA !== 'S') {
console.log('Check API', JSON.stringify(response))
}
resolve(true)
} else {
resolve(false)
}
})
.catch((err) => {
if (!isRetry) {
setTimeout(() => requestCheckHorus(horus, true), 700)
}

if (err.response) {
console.log(err.response)
} else {
console.error(err)
}

resolve(false)
})
})

module.exports = requestCheckHorus
27 changes: 23 additions & 4 deletions functions/lib/pub-sub/exec-queue-pub-sub.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { firestore } = require('firebase-admin')
const { sendMessageTopic } = require('./utils')
const Horus = require('../horus/client')
const checkHorusApi = require('../horus/check-horus-api')

const collectionName = 'queuePubSub'

Expand All @@ -10,13 +12,20 @@ const replayPubSub = async (_appSdk) => {
.limit(20)
.get()
const promises = []
listPubSubs.forEach((doc) => {
let i = 0

while (i < listPubSubs.docs.length) {
const doc = listPubSubs.docs[i]
const docRef = doc.ref
const { eventName, json } = doc.data()
let isSendMessage = true
if (json?.opts?.appData) {
const {
exportation,
importation
importation,
username,
password,
baseURL
} = json.opts.appData

if (importation.products) {
Expand All @@ -25,13 +34,23 @@ const replayPubSub = async (_appSdk) => {
if (exportation.orders) {
delete exportation.orders
}

if (username && password && baseURL) {
console.log('Queue Pub/Sub => check Horus Api')
const horus = new Horus(username, password, baseURL)
isSendMessage = await checkHorusApi(horus)
}
}
const run = async () => {
await docRef.delete()
return sendMessageTopic(eventName, json)
}
promises.push(run())
})
if (isSendMessage) {
promises.push(run())
}

i += 1
}
return Promise.all(promises)
.then(() => {
console.log('>> End Queue Pub/Sub ', promises?.length)
Expand Down

0 comments on commit ee2a489

Please sign in to comment.