2021-12-15 12:25:52 +01:00
|
|
|
require("./utils").threadSetup()
|
2021-11-11 13:11:09 +01:00
|
|
|
const actions = require("../automations/actions")
|
|
|
|
const automationUtils = require("../automations/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")
|
2022-01-12 12:32:14 +01:00
|
|
|
const { DEFAULT_TENANT_ID } = require("@budibase/backend-core/constants")
|
2022-03-21 17:08:25 +01:00
|
|
|
const { DocumentTypes } = require("../db/utils")
|
2022-01-10 20:33:00 +01:00
|
|
|
const { doInTenant } = require("@budibase/backend-core/tenancy")
|
2021-11-16 19:58:24 +01:00
|
|
|
const { definitions: triggerDefs } = require("../automations/triggerInfo")
|
2022-01-28 01:05:39 +01:00
|
|
|
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
|
2022-04-18 10:22:23 +02:00
|
|
|
const { AutomationErrors } = require("../constants")
|
2021-09-14 12:28:39 +02:00
|
|
|
const FILTER_STEP_ID = actions.ACTION_DEFINITIONS.FILTER.stepId
|
2022-03-25 10:26:55 +01:00
|
|
|
const LOOP_STEP_ID = actions.ACTION_DEFINITIONS.LOOP.stepId
|
|
|
|
|
2021-11-16 19:58:24 +01:00
|
|
|
const CRON_STEP_ID = triggerDefs.CRON.stepId
|
2021-10-14 16:26:38 +02:00
|
|
|
const STOPPED_STATUS = { success: false, status: "STOPPED" }
|
2022-03-25 11:52:22 +01:00
|
|
|
const { cloneDeep } = require("lodash/fp")
|
2022-04-12 10:13:01 +02:00
|
|
|
const env = require("../environment")
|
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
|
2021-11-16 19:58:24 +01:00
|
|
|
const triggerStepId = automation.definition.trigger.stepId
|
|
|
|
triggerOutput = this.cleanupTriggerOutputs(triggerStepId, triggerOutput)
|
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 triggerId = automation.definition.trigger.id
|
|
|
|
this.updateExecutionOutput(triggerId, triggerStepId, null, triggerOutput)
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 19:58:24 +01:00
|
|
|
cleanupTriggerOutputs(stepId, triggerOutput) {
|
|
|
|
if (stepId === CRON_STEP_ID) {
|
|
|
|
triggerOutput.timestamp = Date.now()
|
|
|
|
}
|
|
|
|
return triggerOutput
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:28:39 +02:00
|
|
|
async getStepFunctionality(stepId) {
|
|
|
|
let step = await actions.getAction(stepId)
|
2020-09-11 19:47:22 +02:00
|
|
|
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() {
|
|
|
|
if (this._app) {
|
|
|
|
return this._app
|
|
|
|
}
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
2021-08-05 10:59:08 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-04-12 00:10:29 +02:00
|
|
|
updateContextAndOutput(loopStepNumber, step, output, result) {
|
|
|
|
this.executionOutput.steps.splice(loopStepNumber, 0, {
|
|
|
|
id: step.id,
|
|
|
|
stepId: step.stepId,
|
|
|
|
outputs: {
|
|
|
|
...output,
|
|
|
|
success: result.success,
|
|
|
|
status: result.status,
|
|
|
|
},
|
|
|
|
inputs: step.inputs,
|
|
|
|
})
|
|
|
|
this._context.steps.splice(loopStepNumber, 0, {
|
|
|
|
...output,
|
|
|
|
success: result.success,
|
|
|
|
status: result.status,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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()
|
2021-10-14 16:26:38 +02:00
|
|
|
let stopped = false
|
2022-05-03 17:04:59 +02:00
|
|
|
let loopStep = null
|
2022-03-25 10:26:55 +01:00
|
|
|
|
|
|
|
let stepCount = 0
|
2022-05-03 17:04:59 +02:00
|
|
|
let loopStepNumber = null
|
2022-03-28 11:01:56 +02:00
|
|
|
let loopSteps = []
|
2020-09-21 14:49:34 +02:00
|
|
|
for (let step of automation.definition.steps) {
|
2022-03-25 10:26:55 +01:00
|
|
|
stepCount++
|
|
|
|
if (step.stepId === LOOP_STEP_ID) {
|
|
|
|
loopStep = step
|
|
|
|
loopStepNumber = stepCount
|
2021-10-14 16:26:38 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-03-25 10:26:55 +01:00
|
|
|
|
2022-04-11 11:26:59 +02:00
|
|
|
if (loopStep) {
|
2022-05-04 11:24:28 +02:00
|
|
|
// lets first of all handle the input
|
|
|
|
if (
|
|
|
|
typeof loopStep.inputs.binding === "string" &&
|
|
|
|
loopStep.inputs.option === "String"
|
|
|
|
) {
|
|
|
|
loopStep.inputs.binding = loopStep.inputs.binding.split("\n")
|
|
|
|
}
|
2022-04-11 11:26:59 +02:00
|
|
|
}
|
2022-05-04 11:24:28 +02:00
|
|
|
let iterations = loopStep ? loopStep.inputs.binding.length : 1
|
2022-04-11 11:26:59 +02:00
|
|
|
let iterationCount = 0
|
2022-03-25 10:26:55 +01:00
|
|
|
for (let index = 0; index < iterations; index++) {
|
2022-03-25 11:52:22 +01:00
|
|
|
let originalStepInput = cloneDeep(step.inputs)
|
2022-03-28 11:01:56 +02:00
|
|
|
|
2022-04-11 11:26:59 +02:00
|
|
|
// Handle if the user has set a max iteration count or if it reaches the max limit set by us
|
|
|
|
if (loopStep) {
|
|
|
|
let newInput = await processObject(
|
|
|
|
loopStep.inputs,
|
|
|
|
cloneDeep(this._context)
|
|
|
|
)
|
|
|
|
newInput = automationUtils.cleanInputValues(
|
|
|
|
newInput,
|
|
|
|
loopStep.schema.inputs
|
|
|
|
)
|
|
|
|
|
2022-04-12 00:10:29 +02:00
|
|
|
let tempOutput = { items: loopSteps, iterations: iterationCount }
|
2022-04-11 11:26:59 +02:00
|
|
|
if (
|
2022-05-04 11:24:28 +02:00
|
|
|
(originalStepInput.option === "Array" &&
|
|
|
|
!Array.isArray(originalStepInput.binding)) ||
|
|
|
|
(originalStepInput.option === "String" &&
|
|
|
|
typeof originalStepInput.binding !== "string")
|
2022-04-11 11:26:59 +02:00
|
|
|
) {
|
2022-04-12 00:10:29 +02:00
|
|
|
this.updateContextAndOutput(loopStepNumber, step, tempOutput, {
|
2022-04-18 10:22:23 +02:00
|
|
|
status: AutomationErrors.INCORRECT_TYPE,
|
2022-04-12 00:10:29 +02:00
|
|
|
success: false,
|
2022-04-11 11:26:59 +02:00
|
|
|
})
|
|
|
|
loopSteps = null
|
|
|
|
loopStep = null
|
|
|
|
break
|
|
|
|
}
|
2022-05-04 11:24:28 +02:00
|
|
|
this._context.steps[loopStepNumber] = {
|
|
|
|
currentItem: newInput.binding[index],
|
|
|
|
}
|
2022-04-11 11:26:59 +02:00
|
|
|
|
|
|
|
// The "Loop" binding in the front end is "fake", so replace it here so the context can understand it
|
2022-04-12 00:10:29 +02:00
|
|
|
// Pretty hacky because we need to account for the row object
|
2022-04-18 10:22:23 +02:00
|
|
|
for (let [key, value] of Object.entries(originalStepInput)) {
|
|
|
|
if (typeof value === "object") {
|
|
|
|
for (let [innerKey, innerValue] of Object.entries(
|
|
|
|
originalStepInput[key]
|
|
|
|
)) {
|
|
|
|
if (typeof innerValue === "string") {
|
|
|
|
originalStepInput[key][innerKey] =
|
|
|
|
automationUtils.substituteLoopStep(
|
|
|
|
innerValue,
|
|
|
|
`steps.${loopStepNumber}`
|
|
|
|
)
|
|
|
|
}
|
2022-04-11 11:26:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-18 10:22:23 +02:00
|
|
|
if (typeof value === "string") {
|
|
|
|
originalStepInput[key] = automationUtils.substituteLoopStep(
|
|
|
|
value,
|
|
|
|
`steps.${loopStepNumber}`
|
|
|
|
)
|
|
|
|
}
|
2022-04-11 11:26:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:13:01 +02:00
|
|
|
if (
|
2022-04-13 11:23:40 +02:00
|
|
|
index === parseInt(env.AUTOMATION_MAX_ITERATIONS) ||
|
2022-04-12 10:13:01 +02:00
|
|
|
index === loopStep.inputs.iterations
|
|
|
|
) {
|
2022-04-12 00:10:29 +02:00
|
|
|
this.updateContextAndOutput(loopStepNumber, step, tempOutput, {
|
2022-04-18 10:22:23 +02:00
|
|
|
status: AutomationErrors.MAX_ITERATIONS,
|
2022-04-11 11:26:59 +02:00
|
|
|
success: true,
|
|
|
|
})
|
|
|
|
loopSteps = null
|
|
|
|
loopStep = null
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
this._context.steps[loopStepNumber]?.currentItem ===
|
|
|
|
loopStep.inputs.failure
|
|
|
|
) {
|
2022-04-12 00:10:29 +02:00
|
|
|
this.updateContextAndOutput(loopStepNumber, step, tempOutput, {
|
2022-04-18 10:22:23 +02:00
|
|
|
status: AutomationErrors.FAILURE_CONDITION,
|
2022-04-12 00:10:29 +02:00
|
|
|
success: false,
|
2022-04-11 11:26:59 +02:00
|
|
|
})
|
|
|
|
loopSteps = null
|
|
|
|
loopStep = null
|
|
|
|
break
|
|
|
|
}
|
2022-03-28 11:01:56 +02:00
|
|
|
}
|
2022-04-11 11:26:59 +02:00
|
|
|
|
2022-03-25 10:26:55 +01:00
|
|
|
// execution stopped, record state for that
|
|
|
|
if (stopped) {
|
|
|
|
this.updateExecutionOutput(step.id, step.stepId, {}, STOPPED_STATUS)
|
2021-10-14 16:26:38 +02:00
|
|
|
continue
|
2020-09-23 13:29:20 +02:00
|
|
|
}
|
2022-03-25 10:26:55 +01:00
|
|
|
|
|
|
|
// If it's a loop step, we need to manually add the bindings to the context
|
|
|
|
let stepFn = await this.getStepFunctionality(step.stepId)
|
2022-03-25 11:52:22 +01:00
|
|
|
let inputs = await processObject(originalStepInput, this._context)
|
|
|
|
inputs = automationUtils.cleanInputValues(inputs, step.schema.inputs)
|
2022-03-25 10:26:55 +01:00
|
|
|
try {
|
|
|
|
// appId is always passed
|
|
|
|
let tenantId = app.tenantId || DEFAULT_TENANT_ID
|
|
|
|
const outputs = await doInTenant(tenantId, () => {
|
|
|
|
return stepFn({
|
2022-03-25 11:52:22 +01:00
|
|
|
inputs: inputs,
|
2022-03-25 10:26:55 +01:00
|
|
|
appId: this._appId,
|
|
|
|
emitter: this._emitter,
|
|
|
|
context: this._context,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
this._context.steps[stepCount] = outputs
|
|
|
|
// if filter causes us to stop execution don't break the loop, set a var
|
|
|
|
// so that we can finish iterating through the steps and record that it stopped
|
|
|
|
if (step.stepId === FILTER_STEP_ID && !outputs.success) {
|
|
|
|
stopped = true
|
|
|
|
this.updateExecutionOutput(step.id, step.stepId, step.inputs, {
|
|
|
|
...outputs,
|
|
|
|
...STOPPED_STATUS,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2022-03-29 11:29:51 +02:00
|
|
|
if (loopStep && loopSteps) {
|
|
|
|
loopSteps.push(outputs)
|
2022-03-28 11:01:56 +02:00
|
|
|
} else {
|
|
|
|
this.updateExecutionOutput(
|
|
|
|
step.id,
|
|
|
|
step.stepId,
|
|
|
|
step.inputs,
|
|
|
|
outputs
|
|
|
|
)
|
|
|
|
}
|
2022-03-25 10:26:55 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Automation error - ${step.stepId} - ${err}`)
|
|
|
|
return err
|
|
|
|
}
|
2022-04-11 11:26:59 +02:00
|
|
|
if (loopStep) {
|
|
|
|
iterationCount++
|
|
|
|
if (index === iterations - 1) {
|
|
|
|
loopStep = null
|
|
|
|
this._context.steps.splice(loopStepNumber, 1)
|
|
|
|
break
|
|
|
|
}
|
2022-03-25 10:26:55 +01:00
|
|
|
}
|
2020-09-17 17:16:05 +02:00
|
|
|
}
|
2022-03-29 11:29:51 +02:00
|
|
|
|
|
|
|
if (loopSteps && loopSteps.length) {
|
2022-04-11 11:26:59 +02:00
|
|
|
let tempOutput = {
|
|
|
|
success: true,
|
|
|
|
items: loopSteps,
|
|
|
|
iterations: iterationCount,
|
|
|
|
}
|
|
|
|
this.executionOutput.steps.splice(loopStepNumber + 1, 0, {
|
|
|
|
id: step.id,
|
|
|
|
stepId: step.stepId,
|
2022-03-29 11:29:51 +02:00
|
|
|
outputs: tempOutput,
|
|
|
|
inputs: step.inputs,
|
2022-03-28 11:01:56 +02:00
|
|
|
})
|
2022-04-11 11:26:59 +02:00
|
|
|
|
|
|
|
this._context.steps.splice(loopStepNumber, 0, tempOutput)
|
2022-03-28 11:01:56 +02:00
|
|
|
loopSteps = null
|
|
|
|
}
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
2022-04-11 11:26:59 +02:00
|
|
|
|
2021-09-08 15:08:22 +02:00
|
|
|
return this.executionOutput
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:11:09 +01:00
|
|
|
module.exports = (input, callback) => {
|
2022-01-28 01:05:39 +01:00
|
|
|
const appId = input.data.event.appId
|
2022-05-03 16:54:03 +02:00
|
|
|
doInAppContext(appId, async () => {
|
2022-01-28 01:05:39 +01:00
|
|
|
const automationOrchestrator = new Orchestrator(
|
|
|
|
input.data.automation,
|
|
|
|
input.data.event
|
|
|
|
)
|
2022-05-03 16:54:03 +02:00
|
|
|
try {
|
|
|
|
const response = await automationOrchestrator.execute()
|
|
|
|
callback(null, response)
|
|
|
|
} catch (err) {
|
|
|
|
callback(err)
|
|
|
|
}
|
2022-01-28 01:05:39 +01:00
|
|
|
})
|
2020-12-08 12:23:06 +01:00
|
|
|
}
|