2020-09-11 19:47:22 +02:00
|
|
|
const actions = require("./actions")
|
|
|
|
const logic = require("./logic")
|
2020-09-23 14:34:11 +02:00
|
|
|
const automationUtils = require("./automationUtils")
|
2020-12-07 18:23:53 +01:00
|
|
|
const AutomationEmitter = require("../events/AutomationEmitter")
|
2021-01-20 14:32:15 +01:00
|
|
|
const { processObject } = require("@budibase/string-templates")
|
2021-08-05 10:59:08 +02:00
|
|
|
const { DEFAULT_TENANT_ID } = require("@budibase/auth").constants
|
|
|
|
const CouchDB = require("../db")
|
|
|
|
const { DocumentTypes } = require("../db/utils")
|
|
|
|
const { doInTenant } = require("@budibase/auth/tenancy")
|
2020-10-27 14:19:38 +01:00
|
|
|
|
2021-09-06 18:53:02 +02:00
|
|
|
const FILTER_STEP_ID = logic.LOGIC_DEFINITIONS.FILTER.stepId
|
2020-09-17 17:16:05 +02:00
|
|
|
|
2020-09-11 19:47:22 +02:00
|
|
|
/**
|
2020-09-21 14:49:34 +02:00
|
|
|
* The automation orchestrator is a class responsible for executing automations.
|
|
|
|
* It handles the context of the automation and makes sure each step gets the correct
|
2020-09-11 19:47:22 +02:00
|
|
|
* inputs and handles any outputs.
|
|
|
|
*/
|
|
|
|
class Orchestrator {
|
2021-05-18 22:03:26 +02:00
|
|
|
constructor(automation, triggerOutput = {}) {
|
2020-12-07 18:23:53 +01:00
|
|
|
this._metadata = triggerOutput.metadata
|
|
|
|
this._chainCount = this._metadata ? this._metadata.automationChainCount : 0
|
2020-10-29 11:28:27 +01:00
|
|
|
this._appId = triggerOutput.appId
|
2021-08-05 10:59:08 +02:00
|
|
|
this._app = null
|
2020-09-17 17:16:05 +02:00
|
|
|
// remove from context
|
2020-10-29 11:28:27 +01:00
|
|
|
delete triggerOutput.appId
|
2020-12-07 18:23:53 +01:00
|
|
|
delete triggerOutput.metadata
|
2021-01-19 18:29:38 +01:00
|
|
|
// step zero is never used as the template string is zero indexed for customer facing
|
2020-09-17 14:36:19 +02:00
|
|
|
this._context = { steps: [{}], trigger: triggerOutput }
|
2020-09-21 14:49:34 +02:00
|
|
|
this._automation = automation
|
2020-12-07 18:23:53 +01:00
|
|
|
// create an emitter which has the chain count for this automation run in it, so it can block
|
|
|
|
// excessive chaining if required
|
|
|
|
this._emitter = new AutomationEmitter(this._chainCount + 1)
|
2021-09-08 15:08:22 +02:00
|
|
|
this.executionOutput = { trigger: {}, steps: [] }
|
|
|
|
// setup the execution output
|
|
|
|
const triggerStepId = automation.definition.trigger.stepId
|
|
|
|
const triggerId = automation.definition.trigger.id
|
|
|
|
this.updateExecutionOutput(triggerId, triggerStepId, null, triggerOutput)
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
|
2020-09-16 15:00:04 +02:00
|
|
|
async getStepFunctionality(type, stepId) {
|
2020-09-11 19:47:22 +02:00
|
|
|
let step = null
|
|
|
|
if (type === "ACTION") {
|
|
|
|
step = await actions.getAction(stepId)
|
|
|
|
} else if (type === "LOGIC") {
|
|
|
|
step = logic.getLogic(stepId)
|
|
|
|
}
|
|
|
|
if (step == null) {
|
2020-09-21 14:49:34 +02:00
|
|
|
throw `Cannot find automation step by name ${stepId}`
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
async getApp() {
|
|
|
|
const appId = this._appId
|
|
|
|
if (this._app) {
|
|
|
|
return this._app
|
|
|
|
}
|
|
|
|
const db = new CouchDB(appId)
|
|
|
|
this._app = await db.get(DocumentTypes.APP_METADATA)
|
|
|
|
return this._app
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:08:22 +02:00
|
|
|
updateExecutionOutput(id, stepId, inputs, outputs) {
|
|
|
|
const stepObj = { id, stepId, inputs, outputs }
|
|
|
|
// first entry is always the trigger (constructor)
|
|
|
|
if (this.executionOutput.steps.length === 0) {
|
|
|
|
this.executionOutput.trigger = stepObj
|
|
|
|
}
|
|
|
|
this.executionOutput.steps.push(stepObj)
|
|
|
|
}
|
|
|
|
|
2020-09-16 15:00:04 +02:00
|
|
|
async execute() {
|
2020-09-21 14:49:34 +02:00
|
|
|
let automation = this._automation
|
2021-08-05 10:59:08 +02:00
|
|
|
const app = await this.getApp()
|
2020-09-21 14:49:34 +02:00
|
|
|
for (let step of automation.definition.steps) {
|
2020-09-17 14:36:19 +02:00
|
|
|
let stepFn = await this.getStepFunctionality(step.type, step.stepId)
|
2021-01-20 14:32:15 +01:00
|
|
|
step.inputs = await processObject(step.inputs, this._context)
|
2020-09-23 14:34:11 +02:00
|
|
|
step.inputs = automationUtils.cleanInputValues(
|
|
|
|
step.inputs,
|
|
|
|
step.schema.inputs
|
|
|
|
)
|
2020-10-29 11:28:27 +01:00
|
|
|
// appId is always passed
|
2020-09-23 13:29:20 +02:00
|
|
|
try {
|
2021-08-05 10:59:08 +02:00
|
|
|
let tenantId = app.tenantId || DEFAULT_TENANT_ID
|
|
|
|
const outputs = await doInTenant(tenantId, () => {
|
|
|
|
return stepFn({
|
|
|
|
inputs: step.inputs,
|
|
|
|
appId: this._appId,
|
|
|
|
apiKey: automation.apiKey,
|
|
|
|
emitter: this._emitter,
|
|
|
|
context: this._context,
|
|
|
|
})
|
2020-09-23 13:29:20 +02:00
|
|
|
})
|
|
|
|
if (step.stepId === FILTER_STEP_ID && !outputs.success) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
this._context.steps.push(outputs)
|
2021-09-08 15:08:22 +02:00
|
|
|
this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs)
|
2020-09-23 13:29:20 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Automation error - ${step.stepId} - ${err}`)
|
2021-09-07 20:06:20 +02:00
|
|
|
return err
|
2020-09-17 17:16:05 +02:00
|
|
|
}
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
2021-09-08 15:08:22 +02:00
|
|
|
return this.executionOutput
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 11:30:35 +02:00
|
|
|
// callback is required for worker-farm to state that the worker thread has completed
|
2021-09-07 20:06:20 +02:00
|
|
|
module.exports = (job, cb) => {
|
|
|
|
if (!cb) {
|
|
|
|
throw "Callback must be defined."
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
2021-09-07 20:06:20 +02:00
|
|
|
const automationOrchestrator = new Orchestrator(
|
|
|
|
job.data.automation,
|
|
|
|
job.data.event
|
|
|
|
)
|
|
|
|
automationOrchestrator
|
|
|
|
.execute()
|
|
|
|
.then(output => {
|
|
|
|
cb(null, output)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
cb(err)
|
|
|
|
})
|
2020-12-08 12:23:06 +01:00
|
|
|
}
|