2022-07-25 21:45:29 +02:00
|
|
|
import { default as threadUtils } from "./utils"
|
2022-10-14 14:26:42 +02:00
|
|
|
import { Job } from "bull"
|
2022-07-25 21:45:29 +02:00
|
|
|
threadUtils.threadSetup()
|
2022-10-14 14:26:42 +02:00
|
|
|
import {
|
|
|
|
isRecurring,
|
|
|
|
disableCronById,
|
|
|
|
isErrorInOutput,
|
|
|
|
} from "../automations/utils"
|
2022-11-26 16:10:41 +01:00
|
|
|
import * as actions from "../automations/actions"
|
|
|
|
import * as automationUtils from "../automations/automationUtils"
|
2022-07-20 20:05:54 +02:00
|
|
|
import { default as AutomationEmitter } from "../events/AutomationEmitter"
|
2022-07-25 21:45:29 +02:00
|
|
|
import { generateAutomationMetadataID, isProdAppID } from "../db/utils"
|
2022-07-20 20:05:54 +02:00
|
|
|
import { definitions as triggerDefs } from "../automations/triggerInfo"
|
2022-07-20 23:38:06 +02:00
|
|
|
import { AutomationErrors, MAX_AUTOMATION_RECURRING_ERRORS } from "../constants"
|
2022-07-20 20:05:54 +02:00
|
|
|
import { storeLog } from "../automations/logging"
|
2023-05-17 15:10:22 +02:00
|
|
|
import {
|
|
|
|
Automation,
|
|
|
|
AutomationStep,
|
|
|
|
AutomationStatus,
|
|
|
|
AutomationMetadata,
|
|
|
|
AutomationJob,
|
2023-05-25 13:09:12 +02:00
|
|
|
AutomationData,
|
2023-05-17 15:10:22 +02:00
|
|
|
} from "@budibase/types"
|
2022-07-20 20:05:54 +02:00
|
|
|
import {
|
|
|
|
LoopStep,
|
|
|
|
LoopInput,
|
|
|
|
TriggerOutput,
|
|
|
|
AutomationContext,
|
|
|
|
} from "../definitions/automations"
|
2022-07-25 21:45:29 +02:00
|
|
|
import { WorkerCallback } from "./definitions"
|
2022-11-22 20:49:59 +01:00
|
|
|
import { context, logging } from "@budibase/backend-core"
|
|
|
|
import { processObject } from "@budibase/string-templates"
|
|
|
|
import { cloneDeep } from "lodash/fp"
|
2023-01-16 17:47:59 +01:00
|
|
|
import * as sdkUtils from "../sdk/utils"
|
2022-11-22 20:49:59 +01:00
|
|
|
import env from "../environment"
|
2023-04-11 00:48:54 +02:00
|
|
|
const FILTER_STEP_ID = actions.BUILTIN_ACTION_DEFINITIONS.FILTER.stepId
|
|
|
|
const LOOP_STEP_ID = actions.BUILTIN_ACTION_DEFINITIONS.LOOP.stepId
|
2021-11-16 19:58:24 +01:00
|
|
|
const CRON_STEP_ID = triggerDefs.CRON.stepId
|
2022-07-20 23:38:06 +02:00
|
|
|
const STOPPED_STATUS = { success: true, status: AutomationStatus.STOPPED }
|
2020-09-17 17:16:05 +02:00
|
|
|
|
2023-05-30 15:25:28 +02:00
|
|
|
function getLoopIterations(loopStep: LoopStep) {
|
2023-05-30 17:52:22 +02:00
|
|
|
let binding = loopStep.inputs.binding
|
2023-03-31 13:20:47 +02:00
|
|
|
if (!binding) {
|
|
|
|
return 0
|
2022-05-11 12:29:33 +02:00
|
|
|
}
|
2023-01-24 20:09:36 +01:00
|
|
|
if (Array.isArray(binding)) {
|
|
|
|
return binding.length
|
|
|
|
}
|
|
|
|
if (typeof binding === "string") {
|
|
|
|
return automationUtils.stringSplit(binding).length
|
|
|
|
}
|
2023-03-31 13:20:47 +02:00
|
|
|
return 0
|
2022-05-11 12:29:33 +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 {
|
2022-07-20 20:05:54 +02:00
|
|
|
_chainCount: number
|
|
|
|
_appId: string
|
|
|
|
_automation: Automation
|
|
|
|
_emitter: any
|
|
|
|
_context: AutomationContext
|
2022-10-14 14:26:42 +02:00
|
|
|
_job: Job
|
2022-07-20 20:05:54 +02:00
|
|
|
executionOutput: AutomationContext
|
|
|
|
|
2023-05-17 13:26:07 +02:00
|
|
|
constructor(job: AutomationJob) {
|
|
|
|
let automation = job.data.automation
|
|
|
|
let triggerOutput = job.data.event
|
2022-07-20 23:38:06 +02:00
|
|
|
const metadata = triggerOutput.metadata
|
2023-05-17 13:26:07 +02:00
|
|
|
this._chainCount = metadata ? metadata.automationChainCount! : 0
|
2022-07-20 20:05:54 +02:00
|
|
|
this._appId = triggerOutput.appId as string
|
2022-10-14 14:26:42 +02:00
|
|
|
this._job = job
|
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
|
|
|
}
|
|
|
|
|
2022-07-20 20:05:54 +02:00
|
|
|
cleanupTriggerOutputs(stepId: string, triggerOutput: TriggerOutput) {
|
2021-11-16 19:58:24 +01:00
|
|
|
if (stepId === CRON_STEP_ID) {
|
|
|
|
triggerOutput.timestamp = Date.now()
|
|
|
|
}
|
|
|
|
return triggerOutput
|
|
|
|
}
|
|
|
|
|
2022-07-20 20:05:54 +02:00
|
|
|
async getStepFunctionality(stepId: string) {
|
2021-09-14 12:28:39 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-20 23:38:06 +02:00
|
|
|
async getMetadata(): Promise<AutomationMetadata> {
|
2022-11-15 19:33:24 +01:00
|
|
|
const metadataId = generateAutomationMetadataID(this._automation._id!)
|
2022-11-22 20:49:59 +01:00
|
|
|
const db = context.getAppDB()
|
2022-07-20 23:38:06 +02:00
|
|
|
let metadata: AutomationMetadata
|
2022-07-20 20:05:54 +02:00
|
|
|
try {
|
|
|
|
metadata = await db.get(metadataId)
|
|
|
|
} catch (err) {
|
2022-07-20 23:38:06 +02:00
|
|
|
metadata = {
|
|
|
|
_id: metadataId,
|
|
|
|
errorCount: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return metadata
|
|
|
|
}
|
|
|
|
|
2022-08-20 00:14:19 +02:00
|
|
|
async stopCron(reason: string) {
|
2022-10-14 14:26:42 +02:00
|
|
|
if (!this._job.opts.repeat) {
|
2022-08-20 00:14:19 +02:00
|
|
|
return
|
|
|
|
}
|
2022-11-22 20:49:59 +01:00
|
|
|
logging.logWarn(
|
2022-08-20 00:14:19 +02:00
|
|
|
`CRON disabled reason=${reason} - ${this._appId}/${this._automation._id}`
|
|
|
|
)
|
|
|
|
const automation = this._automation
|
|
|
|
const trigger = automation.definition.trigger
|
2022-10-14 14:26:42 +02:00
|
|
|
await disableCronById(this._job.id)
|
2022-08-20 00:14:19 +02:00
|
|
|
this.updateExecutionOutput(
|
|
|
|
trigger.id,
|
|
|
|
trigger.stepId,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
status: AutomationStatus.STOPPED_ERROR,
|
|
|
|
success: false,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
await storeLog(automation, this.executionOutput)
|
|
|
|
}
|
|
|
|
|
2022-07-20 23:38:06 +02:00
|
|
|
async checkIfShouldStop(metadata: AutomationMetadata): Promise<boolean> {
|
2022-10-14 14:26:42 +02:00
|
|
|
if (!metadata.errorCount || !this._job.opts.repeat) {
|
2022-07-20 23:38:06 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (metadata.errorCount >= MAX_AUTOMATION_RECURRING_ERRORS) {
|
2022-08-20 00:14:19 +02:00
|
|
|
await this.stopCron("errors")
|
2022-07-20 23:38:06 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateMetadata(metadata: AutomationMetadata) {
|
|
|
|
const output = this.executionOutput,
|
|
|
|
automation = this._automation
|
|
|
|
if (!output || !isRecurring(automation)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const count = metadata.errorCount
|
2022-07-25 21:45:29 +02:00
|
|
|
const isError = isErrorInOutput(output)
|
2022-07-20 23:38:06 +02:00
|
|
|
// nothing to do in this scenario, escape
|
|
|
|
if (!count && !isError) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (isError) {
|
|
|
|
metadata.errorCount = count ? count + 1 : 1
|
|
|
|
} else {
|
|
|
|
metadata.errorCount = 0
|
|
|
|
}
|
2022-11-22 20:49:59 +01:00
|
|
|
const db = context.getAppDB()
|
2022-07-20 23:38:06 +02:00
|
|
|
try {
|
|
|
|
await db.put(metadata)
|
|
|
|
} catch (err) {
|
2022-11-22 20:49:59 +01:00
|
|
|
logging.logAlertWithInfo(
|
2022-07-20 23:38:06 +02:00
|
|
|
"Failed to write automation metadata",
|
|
|
|
db.name,
|
2022-11-22 20:49:59 +01:00
|
|
|
automation._id!,
|
2022-07-20 23:38:06 +02:00
|
|
|
err
|
|
|
|
)
|
2022-07-20 20:05:54 +02:00
|
|
|
}
|
2021-08-05 10:59:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-20 20:05:54 +02:00
|
|
|
updateExecutionOutput(id: string, stepId: string, inputs: any, outputs: any) {
|
2021-09-08 15:08:22 +02:00
|
|
|
const stepObj = { id, stepId, inputs, outputs }
|
2022-07-26 14:59:22 +02:00
|
|
|
// replacing trigger when disabling CRON
|
|
|
|
if (
|
|
|
|
stepId === CRON_STEP_ID &&
|
|
|
|
outputs.status === AutomationStatus.STOPPED_ERROR
|
|
|
|
) {
|
|
|
|
this.executionOutput.trigger = stepObj
|
|
|
|
this.executionOutput.steps = [stepObj]
|
|
|
|
return
|
|
|
|
}
|
2021-09-08 15:08:22 +02:00
|
|
|
// first entry is always the trigger (constructor)
|
2022-07-26 14:59:22 +02:00
|
|
|
if (
|
|
|
|
this.executionOutput.steps.length === 0 ||
|
|
|
|
this.executionOutput.trigger.id === id
|
|
|
|
) {
|
2021-09-08 15:08:22 +02:00
|
|
|
this.executionOutput.trigger = stepObj
|
|
|
|
}
|
|
|
|
this.executionOutput.steps.push(stepObj)
|
|
|
|
}
|
|
|
|
|
2022-07-20 20:05:54 +02:00
|
|
|
updateContextAndOutput(
|
|
|
|
loopStepNumber: number | undefined,
|
|
|
|
step: AutomationStep,
|
|
|
|
output: any,
|
|
|
|
result: { success: boolean; status: string }
|
|
|
|
) {
|
|
|
|
if (!loopStepNumber) {
|
|
|
|
throw new Error("No loop step number provided.")
|
|
|
|
}
|
2022-04-12 00:10:29 +02:00
|
|
|
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() {
|
2023-01-16 17:47:59 +01:00
|
|
|
// this will retrieve from context created at start of thread
|
|
|
|
this._context.env = await sdkUtils.getEnvironmentVariables()
|
2020-09-21 14:49:34 +02:00
|
|
|
let automation = this._automation
|
2021-10-14 16:26:38 +02:00
|
|
|
let stopped = false
|
2022-07-20 20:05:54 +02:00
|
|
|
let loopStep: AutomationStep | undefined = undefined
|
2022-03-25 10:26:55 +01:00
|
|
|
|
|
|
|
let stepCount = 0
|
2022-07-20 20:05:54 +02:00
|
|
|
let loopStepNumber: any = undefined
|
|
|
|
let loopSteps: LoopStep[] | undefined = []
|
2022-07-20 23:38:06 +02:00
|
|
|
let metadata
|
2023-05-24 15:41:58 +02:00
|
|
|
let timeoutFlag = false
|
2022-10-05 10:20:59 +02:00
|
|
|
let wasLoopStep = false
|
2023-05-24 15:41:58 +02:00
|
|
|
let timeout = this._job.data.event.timeout
|
2022-07-20 23:38:06 +02:00
|
|
|
// check if this is a recurring automation,
|
2022-07-25 21:45:29 +02:00
|
|
|
if (isProdAppID(this._appId) && isRecurring(automation)) {
|
2022-07-20 23:38:06 +02:00
|
|
|
metadata = await this.getMetadata()
|
|
|
|
const shouldStop = await this.checkIfShouldStop(metadata)
|
|
|
|
if (shouldStop) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 17:52:22 +02:00
|
|
|
const start = performance.now()
|
2020-09-21 14:49:34 +02:00
|
|
|
for (let step of automation.definition.steps) {
|
2023-05-24 15:41:58 +02:00
|
|
|
if (timeoutFlag) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout) {
|
|
|
|
setTimeout(() => {
|
|
|
|
timeoutFlag = true
|
|
|
|
}, timeout || 12000)
|
|
|
|
}
|
|
|
|
|
2022-03-25 10:26:55 +01:00
|
|
|
stepCount++
|
2022-11-22 20:49:59 +01:00
|
|
|
let input: any,
|
2022-05-11 12:29:33 +02:00
|
|
|
iterations = 1,
|
|
|
|
iterationCount = 0
|
2022-10-05 10:20:59 +02:00
|
|
|
|
2022-03-25 10:26:55 +01:00
|
|
|
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 12:55:26 +02:00
|
|
|
input = await processObject(loopStep.inputs, this._context)
|
2023-05-30 15:25:28 +02:00
|
|
|
iterations = getLoopIterations(loopStep as LoopStep)
|
2022-04-11 11:26:59 +02:00
|
|
|
}
|
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-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
|
2022-07-18 14:10:18 +02:00
|
|
|
if (loopStep && input.binding) {
|
2022-04-12 00:10:29 +02:00
|
|
|
let tempOutput = { items: loopSteps, iterations: iterationCount }
|
2022-05-11 12:29:33 +02:00
|
|
|
try {
|
2023-05-30 15:25:28 +02:00
|
|
|
loopStep.inputs.binding = automationUtils.typecastForLooping(
|
2022-07-20 20:05:54 +02:00
|
|
|
loopStep as LoopStep,
|
2023-05-30 15:25:28 +02:00
|
|
|
loopStep.inputs as LoopInput
|
2022-07-20 20:05:54 +02:00
|
|
|
)
|
2022-05-11 12:29:33 +02:00
|
|
|
} catch (err) {
|
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
|
|
|
})
|
2022-07-20 20:05:54 +02:00
|
|
|
loopSteps = undefined
|
|
|
|
loopStep = undefined
|
2022-04-11 11:26:59 +02:00
|
|
|
break
|
|
|
|
}
|
2023-01-24 20:09:36 +01:00
|
|
|
let item = []
|
2022-05-04 12:55:26 +02:00
|
|
|
if (
|
|
|
|
typeof loopStep.inputs.binding === "string" &&
|
|
|
|
loopStep.inputs.option === "String"
|
|
|
|
) {
|
2023-05-30 15:25:28 +02:00
|
|
|
item = automationUtils.stringSplit(loopStep.inputs.binding)
|
2023-01-24 20:09:36 +01:00
|
|
|
} else if (Array.isArray(loopStep.inputs.binding)) {
|
2022-05-04 12:55:26 +02:00
|
|
|
item = loopStep.inputs.binding
|
|
|
|
}
|
2022-05-04 11:24:28 +02:00
|
|
|
this._context.steps[loopStepNumber] = {
|
2022-05-04 12:55:26 +02:00
|
|
|
currentItem: item[index],
|
2022-05-04 11:24:28 +02:00
|
|
|
}
|
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-10-05 10:53:47 +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-10-05 10:20:59 +02:00
|
|
|
} else if (typeof value === "object") {
|
|
|
|
for (let [innerObject, innerValue] of Object.entries(
|
|
|
|
originalStepInput[key][innerKey]
|
|
|
|
)) {
|
|
|
|
originalStepInput[key][innerKey][innerObject] =
|
|
|
|
automationUtils.substituteLoopStep(
|
2022-11-26 16:10:41 +01:00
|
|
|
innerValue as string,
|
2022-10-05 10:20:59 +02:00
|
|
|
`steps.${loopStepNumber}`
|
|
|
|
)
|
|
|
|
}
|
2022-04-18 10:22:23 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2023-05-30 17:52:22 +02:00
|
|
|
|
2022-04-12 10:13:01 +02:00
|
|
|
if (
|
2022-11-22 20:49:59 +01:00
|
|
|
index === env.AUTOMATION_MAX_ITERATIONS ||
|
2022-05-19 17:38:16 +02:00
|
|
|
index === parseInt(loopStep.inputs.iterations)
|
2022-04-12 10:13:01 +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.MAX_ITERATIONS,
|
2022-04-11 11:26:59 +02:00
|
|
|
success: true,
|
|
|
|
})
|
2022-07-20 20:05:54 +02:00
|
|
|
loopSteps = undefined
|
|
|
|
loopStep = undefined
|
2022-04-11 11:26:59 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-05-04 14:25:46 +02:00
|
|
|
let isFailure = false
|
2022-05-11 12:29:33 +02:00
|
|
|
const currentItem = this._context.steps[loopStepNumber]?.currentItem
|
|
|
|
if (currentItem && typeof currentItem === "object") {
|
|
|
|
isFailure = Object.keys(currentItem).some(value => {
|
2022-07-20 20:05:54 +02:00
|
|
|
return currentItem[value] === loopStep?.inputs.failure
|
2022-05-04 14:25:46 +02:00
|
|
|
})
|
|
|
|
} else {
|
2022-05-11 12:29:33 +02:00
|
|
|
isFailure = currentItem && currentItem === loopStep.inputs.failure
|
2022-05-04 14:25:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isFailure) {
|
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
|
|
|
})
|
2022-07-20 20:05:54 +02:00
|
|
|
loopSteps = undefined
|
|
|
|
loopStep = undefined
|
2022-04-11 11:26:59 +02:00
|
|
|
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-10-05 10:20:59 +02:00
|
|
|
|
2022-03-25 10:26:55 +01:00
|
|
|
try {
|
|
|
|
// appId is always passed
|
2022-07-18 18:38:58 +02:00
|
|
|
const outputs = await stepFn({
|
|
|
|
inputs: inputs,
|
|
|
|
appId: this._appId,
|
|
|
|
emitter: this._emitter,
|
|
|
|
context: this._context,
|
2022-03-25 10:26:55 +01:00
|
|
|
})
|
2023-05-25 10:14:09 +02:00
|
|
|
|
2022-03-25 10:26:55 +01:00
|
|
|
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
|
2022-06-28 18:02:24 +02:00
|
|
|
if (step.stepId === FILTER_STEP_ID && !outputs.result) {
|
2022-03-25 10:26:55 +01:00
|
|
|
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-10-05 10:20:59 +02:00
|
|
|
|
2022-04-11 11:26:59 +02:00
|
|
|
if (loopStep) {
|
|
|
|
iterationCount++
|
|
|
|
if (index === iterations - 1) {
|
2022-07-20 20:05:54 +02:00
|
|
|
loopStep = undefined
|
2022-04-11 11:26:59 +02:00
|
|
|
this._context.steps.splice(loopStepNumber, 1)
|
|
|
|
break
|
|
|
|
}
|
2022-03-25 10:26:55 +01:00
|
|
|
}
|
2023-05-30 17:52:22 +02:00
|
|
|
console.log("end of loop!")
|
2020-09-17 17:16:05 +02:00
|
|
|
}
|
2022-03-29 11:29:51 +02:00
|
|
|
|
2023-03-31 13:20:47 +02:00
|
|
|
if (loopStep && iterations === 0) {
|
|
|
|
loopStep = undefined
|
|
|
|
this.executionOutput.steps.splice(loopStepNumber + 1, 0, {
|
|
|
|
id: step.id,
|
|
|
|
stepId: step.stepId,
|
|
|
|
outputs: { status: AutomationStatus.NO_ITERATIONS, success: true },
|
|
|
|
inputs: {},
|
|
|
|
})
|
|
|
|
|
|
|
|
this._context.steps.splice(loopStepNumber, 1)
|
|
|
|
iterations = 1
|
|
|
|
}
|
|
|
|
|
2022-10-05 10:20:59 +02:00
|
|
|
// Delete the step after the loop step as it's irrelevant, since information is included
|
|
|
|
// in the loop step
|
2023-01-05 11:52:50 +01:00
|
|
|
if (wasLoopStep && !loopStep) {
|
2022-10-05 10:20:59 +02:00
|
|
|
this._context.steps.splice(loopStepNumber + 1, 1)
|
|
|
|
wasLoopStep = false
|
|
|
|
}
|
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-10-05 10:20:59 +02:00
|
|
|
this._context.steps[loopStepNumber] = tempOutput
|
2022-04-11 11:26:59 +02:00
|
|
|
|
2023-01-05 13:16:45 +01:00
|
|
|
wasLoopStep = true
|
2023-01-05 11:52:50 +01:00
|
|
|
loopSteps = []
|
2022-03-28 11:01:56 +02:00
|
|
|
}
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
2022-04-11 11:26:59 +02:00
|
|
|
|
2023-05-30 17:52:22 +02:00
|
|
|
const end = performance.now()
|
|
|
|
const executionTime = end - start
|
|
|
|
|
|
|
|
console.log(`Execution time: ${executionTime} milliseconds`)
|
|
|
|
|
2022-06-01 23:39:51 +02:00
|
|
|
// store the logs for the automation run
|
2023-05-25 13:09:12 +02:00
|
|
|
try {
|
|
|
|
await storeLog(this._automation, this.executionOutput)
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e.status === 413 && e.request?.data) {
|
|
|
|
// if content is too large we shouldn't log it
|
|
|
|
delete e.request.data
|
|
|
|
e.request.data = { message: "removed due to large size" }
|
|
|
|
}
|
|
|
|
logging.logAlert("Error writing automation log", e)
|
|
|
|
}
|
2022-07-25 21:45:29 +02:00
|
|
|
if (isProdAppID(this._appId) && isRecurring(automation) && metadata) {
|
2022-07-20 23:38:06 +02:00
|
|
|
await this.updateMetadata(metadata)
|
|
|
|
}
|
2021-09-08 15:08:22 +02:00
|
|
|
return this.executionOutput
|
2020-09-11 19:47:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 13:09:12 +02:00
|
|
|
export function execute(job: Job<AutomationData>, callback: WorkerCallback) {
|
2022-10-14 14:26:42 +02:00
|
|
|
const appId = job.data.event.appId
|
2023-05-25 13:09:12 +02:00
|
|
|
const automationId = job.data.automation._id
|
2022-09-07 18:05:17 +02:00
|
|
|
if (!appId) {
|
|
|
|
throw new Error("Unable to execute, event doesn't contain app ID.")
|
|
|
|
}
|
2023-05-25 13:09:12 +02:00
|
|
|
if (!automationId) {
|
|
|
|
throw new Error("Unable to execute, event doesn't contain automation ID.")
|
|
|
|
}
|
|
|
|
return context.doInAutomationContext({ appId, automationId, task: async () => {
|
|
|
|
const envVars = await sdkUtils.getEnvironmentVariables()
|
|
|
|
// put into automation thread for whole context
|
|
|
|
await context.doInEnvironmentContext(envVars, async () => {
|
|
|
|
const automationOrchestrator = new Orchestrator(job)
|
|
|
|
try {
|
|
|
|
const response = await automationOrchestrator.execute()
|
|
|
|
callback(null, response)
|
|
|
|
} catch (err) {
|
|
|
|
callback(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}})
|
2020-12-08 12:23:06 +01:00
|
|
|
}
|
2022-08-20 00:14:19 +02:00
|
|
|
|
2023-05-18 18:01:53 +02:00
|
|
|
export function executeSynchronously(job: Job) {
|
|
|
|
const appId = job.data.event.appId
|
|
|
|
if (!appId) {
|
|
|
|
throw new Error("Unable to execute, event doesn't contain app ID.")
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeoutPromise = new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
reject(new Error("Timeout exceeded"))
|
|
|
|
}, job.data.event.timeout || 12000)
|
|
|
|
})
|
|
|
|
|
|
|
|
return context.doInAppContext(appId, async () => {
|
|
|
|
const envVars = await sdkUtils.getEnvironmentVariables()
|
|
|
|
// put into automation thread for whole context
|
|
|
|
return context.doInEnvironmentContext(envVars, async () => {
|
|
|
|
const automationOrchestrator = new Orchestrator(job)
|
2023-05-19 11:07:02 +02:00
|
|
|
const response = await Promise.race([
|
|
|
|
automationOrchestrator.execute(),
|
|
|
|
timeoutPromise,
|
|
|
|
])
|
|
|
|
return response
|
2023-05-18 18:01:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:26:42 +02:00
|
|
|
export const removeStalled = async (job: Job) => {
|
|
|
|
const appId = job.data.event.appId
|
2022-09-07 18:05:17 +02:00
|
|
|
if (!appId) {
|
|
|
|
throw new Error("Unable to execute, event doesn't contain app ID.")
|
|
|
|
}
|
2022-11-22 20:49:59 +01:00
|
|
|
await context.doInAppContext(appId, async () => {
|
2023-02-10 15:14:43 +01:00
|
|
|
const automationOrchestrator = new Orchestrator(job)
|
|
|
|
await automationOrchestrator.stopCron("stalled")
|
2022-08-20 00:14:19 +02:00
|
|
|
})
|
|
|
|
}
|