forked from tsai/budibase
7 changed files with 105 additions and 62 deletions
@ -1,49 +1,12 @@ |
|||||
const triggers = require("./triggers") |
const triggers = require("./triggers") |
||||
const env = require("../environment") |
const { processEvent } = require("./utils") |
||||
const workerFarm = require("worker-farm") |
|
||||
const singleThread = require("./thread") |
|
||||
const { getAPIKey, update, Properties } = require("../utilities/usageQuota") |
|
||||
|
|
||||
let workers = workerFarm(require.resolve("./thread")) |
|
||||
|
|
||||
function runWorker(job) { |
|
||||
return new Promise((resolve, reject) => { |
|
||||
workers(job, err => { |
|
||||
if (err) { |
|
||||
reject(err) |
|
||||
} else { |
|
||||
resolve() |
|
||||
} |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
async function updateQuota(automation) { |
|
||||
const appId = automation.appId |
|
||||
const apiObj = await getAPIKey(appId) |
|
||||
// this will fail, causing automation to escape if limits reached
|
|
||||
await update(apiObj.apiKey, Properties.AUTOMATION, 1) |
|
||||
return apiObj.apiKey |
|
||||
} |
|
||||
|
|
||||
/** |
/** |
||||
* This module is built purely to kick off the worker farm and manage the inputs/outputs |
* This module is built purely to kick off the worker farm and manage the inputs/outputs |
||||
*/ |
*/ |
||||
module.exports.init = async function () { |
exports.init = async function () { |
||||
|
// don't wait this promise, it'll never end
|
||||
triggers.automationQueue.process(async job => { |
triggers.automationQueue.process(async job => { |
||||
try { |
await processEvent(job) |
||||
if (env.USE_QUOTAS) { |
|
||||
job.data.automation.apiKey = await updateQuota(job.data.automation) |
|
||||
} |
|
||||
if (env.isProd()) { |
|
||||
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}` |
|
||||
) |
|
||||
} |
|
||||
}) |
}) |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,56 @@ |
|||||
|
const env = require("../environment") |
||||
|
const workerFarm = require("worker-farm") |
||||
|
const { getAPIKey, update, Properties } = require("../utilities/usageQuota") |
||||
|
const singleThread = require("./thread") |
||||
|
|
||||
|
let workers = workerFarm(require.resolve("./thread")) |
||||
|
|
||||
|
function runWorker(job) { |
||||
|
return new Promise((resolve, reject) => { |
||||
|
workers(job, (err, output) => { |
||||
|
if (err) { |
||||
|
reject(err) |
||||
|
} else { |
||||
|
resolve(output) |
||||
|
} |
||||
|
}) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
function runSingleThread(job) { |
||||
|
return new Promise((resolve, reject) => { |
||||
|
singleThread(job, (err, output) => { |
||||
|
if (err) { |
||||
|
reject(err) |
||||
|
} else { |
||||
|
resolve(output) |
||||
|
} |
||||
|
}) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
async function updateQuota(automation) { |
||||
|
const appId = automation.appId |
||||
|
const apiObj = await getAPIKey(appId) |
||||
|
// this will fail, causing automation to escape if limits reached
|
||||
|
await update(apiObj.apiKey, Properties.AUTOMATION, 1) |
||||
|
return apiObj.apiKey |
||||
|
} |
||||
|
|
||||
|
exports.processEvent = async job => { |
||||
|
try { |
||||
|
if (env.USE_QUOTAS) { |
||||
|
job.data.automation.apiKey = await updateQuota(job.data.automation) |
||||
|
} |
||||
|
if (!env.isProd()) { |
||||
|
return runSingleThread(job) |
||||
|
} else { |
||||
|
return runWorker(job) |
||||
|
} |
||||
|
} catch (err) { |
||||
|
console.error( |
||||
|
`${job.data.automation.appId} automation ${job.data.automation._id} was unable to run - ${err}` |
||||
|
) |
||||
|
return err |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue