2020-09-10 16:00:21 +02:00
|
|
|
const triggers = require("./triggers")
|
2020-10-28 21:35:06 +01:00
|
|
|
const env = require("../environment")
|
2020-09-11 19:47:22 +02:00
|
|
|
const workerFarm = require("worker-farm")
|
|
|
|
const singleThread = require("./thread")
|
2020-10-07 18:56:47 +02:00
|
|
|
const { getAPIKey, update, Properties } = require("../utilities/usageQuota")
|
2020-09-10 16:00:21 +02:00
|
|
|
|
2020-09-11 19:47:22 +02:00
|
|
|
let workers = workerFarm(require.resolve("./thread"))
|
2020-09-10 16:00:21 +02:00
|
|
|
|
2020-09-11 19:47:22 +02:00
|
|
|
function runWorker(job) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-05-04 12:32:22 +02:00
|
|
|
workers(job, err => {
|
2020-09-11 19:47:22 +02:00
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve()
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
2020-09-11 19:47:22 +02:00
|
|
|
})
|
|
|
|
})
|
2020-09-10 16:00:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-07 18:56:47 +02:00
|
|
|
async function updateQuota(automation) {
|
|
|
|
const appId = automation.appId
|
2020-10-08 18:34:41 +02:00
|
|
|
const apiObj = await getAPIKey(appId)
|
2020-10-07 18:56:47 +02:00
|
|
|
// this will fail, causing automation to escape if limits reached
|
2020-10-08 18:34:41 +02:00
|
|
|
await update(apiObj.apiKey, Properties.AUTOMATION, 1)
|
|
|
|
return apiObj.apiKey
|
2020-10-07 18:56:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:47:22 +02:00
|
|
|
/**
|
|
|
|
* This module is built purely to kick off the worker farm and manage the inputs/outputs
|
|
|
|
*/
|
2021-05-03 09:31:09 +02:00
|
|
|
module.exports.init = async function () {
|
2021-05-04 12:32:22 +02:00
|
|
|
triggers.automationQueue.process(async job => {
|
2021-03-15 15:11:13 +01:00
|
|
|
try {
|
2021-03-24 19:21:23 +01:00
|
|
|
if (env.USE_QUOTAS) {
|
2021-03-15 15:11:13 +01:00
|
|
|
job.data.automation.apiKey = await updateQuota(job.data.automation)
|
2020-09-18 17:50:52 +02:00
|
|
|
}
|
2021-03-24 19:21:23 +01:00
|
|
|
if (env.isProd()) {
|
2021-03-15 15:11:13 +01:00
|
|
|
await runWorker(job)
|
|
|
|
} else {
|
|
|
|
await singleThread(job)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(
|
|
|
|
`${job.data.automation.appId} automation ${job.data.automation._id} was unable to run - ${err}`
|
|
|
|
)
|
|
|
|
}
|
2020-09-10 16:00:21 +02:00
|
|
|
})
|
|
|
|
}
|