Converting main automation thread to typescript.
This commit is contained in:
parent
5d8efe40eb
commit
1848f85475
|
@ -208,10 +208,5 @@ exports.AutomationErrors = {
|
||||||
FAILURE_CONDITION: "FAILURE_CONDITION_MET",
|
FAILURE_CONDITION: "FAILURE_CONDITION_MET",
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.LoopStepTypes = {
|
|
||||||
ARRAY: "Array",
|
|
||||||
STRING: "String",
|
|
||||||
}
|
|
||||||
|
|
||||||
// pass through the list from the auth/core lib
|
// pass through the list from the auth/core lib
|
||||||
exports.ObjectStoreBuckets = ObjectStoreBuckets
|
exports.ObjectStoreBuckets = ObjectStoreBuckets
|
||||||
|
|
|
@ -41,6 +41,7 @@ const DocumentTypes = {
|
||||||
METADATA: "metadata",
|
METADATA: "metadata",
|
||||||
MEM_VIEW: "view",
|
MEM_VIEW: "view",
|
||||||
USER_FLAG: "flag",
|
USER_FLAG: "flag",
|
||||||
|
AUTOMATION_METADATA: "meta_au",
|
||||||
}
|
}
|
||||||
|
|
||||||
const InternalTables = {
|
const InternalTables = {
|
||||||
|
@ -311,6 +312,14 @@ exports.generateQueryID = datasourceId => {
|
||||||
}${SEPARATOR}${datasourceId}${SEPARATOR}${newid()}`
|
}${SEPARATOR}${datasourceId}${SEPARATOR}${newid()}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a metadata ID for automations, used to track errors in recurring
|
||||||
|
* automations etc.
|
||||||
|
*/
|
||||||
|
exports.generateAutomationMetadataID = automationId => {
|
||||||
|
return `${DocumentTypes.AUTOMATION_METADATA}${SEPARATOR}${automationId}`
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets parameters for retrieving a query, this is a utility function for the getDocParams function.
|
* Gets parameters for retrieving a query, this is a utility function for the getDocParams function.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
import { Automation, AutomationResults, AutomationStep } from "@budibase/types"
|
||||||
|
|
||||||
|
export enum LoopStepTypes {
|
||||||
|
ARRAY = "Array",
|
||||||
|
STRING = "String",
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LoopStep extends AutomationStep {
|
||||||
|
inputs: {
|
||||||
|
option: LoopStepTypes
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LoopInput {
|
||||||
|
binding: string[] | string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TriggerOutput {
|
||||||
|
metadata?: any
|
||||||
|
appId?: string
|
||||||
|
timestamp?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AutomationEvent {
|
||||||
|
data: {
|
||||||
|
automation: Automation
|
||||||
|
event: any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AutomationContext extends AutomationResults {
|
||||||
|
steps: any[]
|
||||||
|
trigger: any
|
||||||
|
}
|
|
@ -1,36 +1,43 @@
|
||||||
require("./utils").threadSetup()
|
import { threadSetup } from "./utils"
|
||||||
const actions = require("../automations/actions")
|
threadSetup()
|
||||||
const automationUtils = require("../automations/automationUtils")
|
import { default as actions } from "../automations/actions"
|
||||||
const AutomationEmitter = require("../events/AutomationEmitter")
|
import { default as automationUtils } from "../automations/automationUtils"
|
||||||
const { processObject } = require("@budibase/string-templates")
|
import { default as AutomationEmitter } from "../events/AutomationEmitter"
|
||||||
const { DocumentTypes } = require("../db/utils")
|
import { generateAutomationMetadataID } from "../db/utils"
|
||||||
const { definitions: triggerDefs } = require("../automations/triggerInfo")
|
import { definitions as triggerDefs } from "../automations/triggerInfo"
|
||||||
|
import { AutomationErrors } from "../constants"
|
||||||
|
import { storeLog } from "../automations/logging"
|
||||||
|
import { Automation, AutomationStep } from "@budibase/types"
|
||||||
|
import {
|
||||||
|
LoopStep,
|
||||||
|
LoopStepTypes,
|
||||||
|
LoopInput,
|
||||||
|
AutomationEvent,
|
||||||
|
TriggerOutput,
|
||||||
|
AutomationContext,
|
||||||
|
} from "../definitions/automations"
|
||||||
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
|
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
|
||||||
const { AutomationErrors, LoopStepTypes } = require("../constants")
|
const { processObject } = require("@budibase/string-templates")
|
||||||
const { storeLog } = require("../automations/logging")
|
|
||||||
const FILTER_STEP_ID = actions.ACTION_DEFINITIONS.FILTER.stepId
|
const FILTER_STEP_ID = actions.ACTION_DEFINITIONS.FILTER.stepId
|
||||||
const LOOP_STEP_ID = actions.ACTION_DEFINITIONS.LOOP.stepId
|
const LOOP_STEP_ID = actions.ACTION_DEFINITIONS.LOOP.stepId
|
||||||
|
|
||||||
const CRON_STEP_ID = triggerDefs.CRON.stepId
|
const CRON_STEP_ID = triggerDefs.CRON.stepId
|
||||||
const STOPPED_STATUS = { success: true, status: "STOPPED" }
|
const STOPPED_STATUS = { success: true, status: "STOPPED" }
|
||||||
const { cloneDeep } = require("lodash/fp")
|
const { cloneDeep } = require("lodash/fp")
|
||||||
const env = require("../environment")
|
const env = require("../environment")
|
||||||
|
|
||||||
function typecastForLooping(loopStep, input) {
|
function typecastForLooping(loopStep: LoopStep, input: LoopInput) {
|
||||||
if (!input || !input.binding) {
|
if (!input || !input.binding) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const isArray = Array.isArray(input.binding),
|
|
||||||
isString = typeof input.binding === "string"
|
|
||||||
try {
|
try {
|
||||||
switch (loopStep.inputs.option) {
|
switch (loopStep.inputs.option) {
|
||||||
case LoopStepTypes.ARRAY:
|
case LoopStepTypes.ARRAY:
|
||||||
if (isString) {
|
if (typeof input.binding === "string") {
|
||||||
return JSON.parse(input.binding)
|
return JSON.parse(input.binding)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case LoopStepTypes.STRING:
|
case LoopStepTypes.STRING:
|
||||||
if (isArray) {
|
if (Array.isArray(input.binding)) {
|
||||||
return input.binding.join(",")
|
return input.binding.join(",")
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
@ -41,7 +48,7 @@ function typecastForLooping(loopStep, input) {
|
||||||
return input.binding
|
return input.binding
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLoopIterations(loopStep, input) {
|
function getLoopIterations(loopStep: LoopStep, input: LoopInput) {
|
||||||
const binding = typecastForLooping(loopStep, input)
|
const binding = typecastForLooping(loopStep, input)
|
||||||
if (!loopStep || !binding) {
|
if (!loopStep || !binding) {
|
||||||
return 1
|
return 1
|
||||||
|
@ -57,11 +64,18 @@ function getLoopIterations(loopStep, input) {
|
||||||
* inputs and handles any outputs.
|
* inputs and handles any outputs.
|
||||||
*/
|
*/
|
||||||
class Orchestrator {
|
class Orchestrator {
|
||||||
constructor(automation, triggerOutput = {}) {
|
_metadata: any
|
||||||
|
_chainCount: number
|
||||||
|
_appId: string
|
||||||
|
_automation: Automation
|
||||||
|
_emitter: any
|
||||||
|
_context: AutomationContext
|
||||||
|
executionOutput: AutomationContext
|
||||||
|
|
||||||
|
constructor(automation: Automation, triggerOutput: TriggerOutput) {
|
||||||
this._metadata = triggerOutput.metadata
|
this._metadata = triggerOutput.metadata
|
||||||
this._chainCount = this._metadata ? this._metadata.automationChainCount : 0
|
this._chainCount = this._metadata ? this._metadata.automationChainCount : 0
|
||||||
this._appId = triggerOutput.appId
|
this._appId = triggerOutput.appId as string
|
||||||
this._app = null
|
|
||||||
const triggerStepId = automation.definition.trigger.stepId
|
const triggerStepId = automation.definition.trigger.stepId
|
||||||
triggerOutput = this.cleanupTriggerOutputs(triggerStepId, triggerOutput)
|
triggerOutput = this.cleanupTriggerOutputs(triggerStepId, triggerOutput)
|
||||||
// remove from context
|
// remove from context
|
||||||
|
@ -79,14 +93,14 @@ class Orchestrator {
|
||||||
this.updateExecutionOutput(triggerId, triggerStepId, null, triggerOutput)
|
this.updateExecutionOutput(triggerId, triggerStepId, null, triggerOutput)
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanupTriggerOutputs(stepId, triggerOutput) {
|
cleanupTriggerOutputs(stepId: string, triggerOutput: TriggerOutput) {
|
||||||
if (stepId === CRON_STEP_ID) {
|
if (stepId === CRON_STEP_ID) {
|
||||||
triggerOutput.timestamp = Date.now()
|
triggerOutput.timestamp = Date.now()
|
||||||
}
|
}
|
||||||
return triggerOutput
|
return triggerOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
async getStepFunctionality(stepId) {
|
async getStepFunctionality(stepId: string) {
|
||||||
let step = await actions.getAction(stepId)
|
let step = await actions.getAction(stepId)
|
||||||
if (step == null) {
|
if (step == null) {
|
||||||
throw `Cannot find automation step by name ${stepId}`
|
throw `Cannot find automation step by name ${stepId}`
|
||||||
|
@ -94,16 +108,18 @@ class Orchestrator {
|
||||||
return step
|
return step
|
||||||
}
|
}
|
||||||
|
|
||||||
async getApp() {
|
async getMetadata() {
|
||||||
if (this._app) {
|
const metadataId = generateAutomationMetadataID(this._automation._id)
|
||||||
return this._app
|
|
||||||
}
|
|
||||||
const db = getAppDB()
|
const db = getAppDB()
|
||||||
this._app = await db.get(DocumentTypes.APP_METADATA)
|
let metadata: any
|
||||||
return this._app
|
try {
|
||||||
|
metadata = await db.get(metadataId)
|
||||||
|
} catch (err) {
|
||||||
|
metadata = {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateExecutionOutput(id, stepId, inputs, outputs) {
|
updateExecutionOutput(id: string, stepId: string, inputs: any, outputs: any) {
|
||||||
const stepObj = { id, stepId, inputs, outputs }
|
const stepObj = { id, stepId, inputs, outputs }
|
||||||
// first entry is always the trigger (constructor)
|
// first entry is always the trigger (constructor)
|
||||||
if (this.executionOutput.steps.length === 0) {
|
if (this.executionOutput.steps.length === 0) {
|
||||||
|
@ -112,7 +128,15 @@ class Orchestrator {
|
||||||
this.executionOutput.steps.push(stepObj)
|
this.executionOutput.steps.push(stepObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateContextAndOutput(loopStepNumber, step, output, result) {
|
updateContextAndOutput(
|
||||||
|
loopStepNumber: number | undefined,
|
||||||
|
step: AutomationStep,
|
||||||
|
output: any,
|
||||||
|
result: { success: boolean; status: string }
|
||||||
|
) {
|
||||||
|
if (!loopStepNumber) {
|
||||||
|
throw new Error("No loop step number provided.")
|
||||||
|
}
|
||||||
this.executionOutput.steps.splice(loopStepNumber, 0, {
|
this.executionOutput.steps.splice(loopStepNumber, 0, {
|
||||||
id: step.id,
|
id: step.id,
|
||||||
stepId: step.stepId,
|
stepId: step.stepId,
|
||||||
|
@ -133,11 +157,11 @@ class Orchestrator {
|
||||||
async execute() {
|
async execute() {
|
||||||
let automation = this._automation
|
let automation = this._automation
|
||||||
let stopped = false
|
let stopped = false
|
||||||
let loopStep = null
|
let loopStep: AutomationStep | undefined = undefined
|
||||||
|
|
||||||
let stepCount = 0
|
let stepCount = 0
|
||||||
let loopStepNumber = null
|
let loopStepNumber: any = undefined
|
||||||
let loopSteps = []
|
let loopSteps: LoopStep[] | undefined = []
|
||||||
for (let step of automation.definition.steps) {
|
for (let step of automation.definition.steps) {
|
||||||
stepCount++
|
stepCount++
|
||||||
let input,
|
let input,
|
||||||
|
@ -151,7 +175,7 @@ class Orchestrator {
|
||||||
|
|
||||||
if (loopStep) {
|
if (loopStep) {
|
||||||
input = await processObject(loopStep.inputs, this._context)
|
input = await processObject(loopStep.inputs, this._context)
|
||||||
iterations = getLoopIterations(loopStep, input)
|
iterations = getLoopIterations(loopStep as LoopStep, input)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let index = 0; index < iterations; index++) {
|
for (let index = 0; index < iterations; index++) {
|
||||||
|
@ -166,14 +190,17 @@ class Orchestrator {
|
||||||
|
|
||||||
let tempOutput = { items: loopSteps, iterations: iterationCount }
|
let tempOutput = { items: loopSteps, iterations: iterationCount }
|
||||||
try {
|
try {
|
||||||
newInput.binding = typecastForLooping(loopStep, newInput)
|
newInput.binding = typecastForLooping(
|
||||||
|
loopStep as LoopStep,
|
||||||
|
newInput
|
||||||
|
)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.updateContextAndOutput(loopStepNumber, step, tempOutput, {
|
this.updateContextAndOutput(loopStepNumber, step, tempOutput, {
|
||||||
status: AutomationErrors.INCORRECT_TYPE,
|
status: AutomationErrors.INCORRECT_TYPE,
|
||||||
success: false,
|
success: false,
|
||||||
})
|
})
|
||||||
loopSteps = null
|
loopSteps = undefined
|
||||||
loopStep = null
|
loopStep = undefined
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,8 +250,8 @@ class Orchestrator {
|
||||||
status: AutomationErrors.MAX_ITERATIONS,
|
status: AutomationErrors.MAX_ITERATIONS,
|
||||||
success: true,
|
success: true,
|
||||||
})
|
})
|
||||||
loopSteps = null
|
loopSteps = undefined
|
||||||
loopStep = null
|
loopStep = undefined
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -232,7 +259,7 @@ class Orchestrator {
|
||||||
const currentItem = this._context.steps[loopStepNumber]?.currentItem
|
const currentItem = this._context.steps[loopStepNumber]?.currentItem
|
||||||
if (currentItem && typeof currentItem === "object") {
|
if (currentItem && typeof currentItem === "object") {
|
||||||
isFailure = Object.keys(currentItem).some(value => {
|
isFailure = Object.keys(currentItem).some(value => {
|
||||||
return currentItem[value] === loopStep.inputs.failure
|
return currentItem[value] === loopStep?.inputs.failure
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
isFailure = currentItem && currentItem === loopStep.inputs.failure
|
isFailure = currentItem && currentItem === loopStep.inputs.failure
|
||||||
|
@ -243,8 +270,8 @@ class Orchestrator {
|
||||||
status: AutomationErrors.FAILURE_CONDITION,
|
status: AutomationErrors.FAILURE_CONDITION,
|
||||||
success: false,
|
success: false,
|
||||||
})
|
})
|
||||||
loopSteps = null
|
loopSteps = undefined
|
||||||
loopStep = null
|
loopStep = undefined
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -295,7 +322,7 @@ class Orchestrator {
|
||||||
if (loopStep) {
|
if (loopStep) {
|
||||||
iterationCount++
|
iterationCount++
|
||||||
if (index === iterations - 1) {
|
if (index === iterations - 1) {
|
||||||
loopStep = null
|
loopStep = undefined
|
||||||
this._context.steps.splice(loopStepNumber, 1)
|
this._context.steps.splice(loopStepNumber, 1)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -316,7 +343,7 @@ class Orchestrator {
|
||||||
})
|
})
|
||||||
|
|
||||||
this._context.steps.splice(loopStepNumber, 0, tempOutput)
|
this._context.steps.splice(loopStepNumber, 0, tempOutput)
|
||||||
loopSteps = null
|
loopSteps = undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,7 +353,10 @@ class Orchestrator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = (input, callback) => {
|
module.exports = (
|
||||||
|
input: AutomationEvent,
|
||||||
|
callback: (error: any, response?: any) => void
|
||||||
|
) => {
|
||||||
const appId = input.data.event.appId
|
const appId = input.data.event.appId
|
||||||
doInAppContext(appId, async () => {
|
doInAppContext(appId, async () => {
|
||||||
const automationOrchestrator = new Orchestrator(
|
const automationOrchestrator = new Orchestrator(
|
|
@ -12,6 +12,14 @@ export interface Automation extends Document {
|
||||||
export interface AutomationStep {
|
export interface AutomationStep {
|
||||||
id: string
|
id: string
|
||||||
stepId: string
|
stepId: string
|
||||||
|
inputs: {
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
schema: {
|
||||||
|
inputs: {
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AutomationTrigger {
|
export interface AutomationTrigger {
|
||||||
|
@ -26,8 +34,8 @@ export enum AutomationStatus {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AutomationResults {
|
export interface AutomationResults {
|
||||||
automationId: string
|
automationId?: string
|
||||||
status: string
|
status?: string
|
||||||
trigger?: any
|
trigger?: any
|
||||||
steps: {
|
steps: {
|
||||||
stepId: string
|
stepId: string
|
||||||
|
|
Loading…
Reference in New Issue