Fix performance issue with looping and context

This commit is contained in:
Peter Clement 2023-05-30 14:25:28 +01:00
parent 5d65de9ad4
commit 5a7f0ba586
1 changed files with 8 additions and 13 deletions

View File

@ -37,8 +37,8 @@ const LOOP_STEP_ID = actions.BUILTIN_ACTION_DEFINITIONS.LOOP.stepId
const CRON_STEP_ID = triggerDefs.CRON.stepId const CRON_STEP_ID = triggerDefs.CRON.stepId
const STOPPED_STATUS = { success: true, status: AutomationStatus.STOPPED } const STOPPED_STATUS = { success: true, status: AutomationStatus.STOPPED }
function getLoopIterations(loopStep: LoopStep, input: LoopInput) { function getLoopIterations(loopStep: LoopStep) {
const binding = automationUtils.typecastForLooping(loopStep, input) let binding = loopStep.inputs.binding
if (!binding) { if (!binding) {
return 0 return 0
} }
@ -68,7 +68,6 @@ class Orchestrator {
constructor(job: AutomationJob) { constructor(job: AutomationJob) {
let automation = job.data.automation let automation = job.data.automation
let triggerOutput = job.data.event let triggerOutput = job.data.event
let timeout = job.data.event.timeout
const metadata = triggerOutput.metadata const metadata = triggerOutput.metadata
this._chainCount = metadata ? metadata.automationChainCount! : 0 this._chainCount = metadata ? metadata.automationChainCount! : 0
this._appId = triggerOutput.appId as string this._appId = triggerOutput.appId as string
@ -277,22 +276,17 @@ class Orchestrator {
if (loopStep) { if (loopStep) {
input = await processObject(loopStep.inputs, this._context) input = await processObject(loopStep.inputs, this._context)
iterations = getLoopIterations(loopStep as LoopStep, input) iterations = getLoopIterations(loopStep as LoopStep)
} }
for (let index = 0; index < iterations; index++) { for (let index = 0; index < iterations; index++) {
let originalStepInput = cloneDeep(step.inputs) let originalStepInput = cloneDeep(step.inputs)
// Handle if the user has set a max iteration count or if it reaches the max limit set by us // Handle if the user has set a max iteration count or if it reaches the max limit set by us
if (loopStep && input.binding) { if (loopStep && input.binding) {
let newInput: any = await processObject(
loopStep.inputs,
cloneDeep(this._context)
)
let tempOutput = { items: loopSteps, iterations: iterationCount } let tempOutput = { items: loopSteps, iterations: iterationCount }
try { try {
newInput.binding = automationUtils.typecastForLooping( loopStep.inputs.binding = automationUtils.typecastForLooping(
loopStep as LoopStep, loopStep as LoopStep,
newInput loopStep.inputs as LoopInput
) )
} catch (err) { } catch (err) {
this.updateContextAndOutput(loopStepNumber, step, tempOutput, { this.updateContextAndOutput(loopStepNumber, step, tempOutput, {
@ -303,13 +297,12 @@ class Orchestrator {
loopStep = undefined loopStep = undefined
break break
} }
let item = [] let item = []
if ( if (
typeof loopStep.inputs.binding === "string" && typeof loopStep.inputs.binding === "string" &&
loopStep.inputs.option === "String" loopStep.inputs.option === "String"
) { ) {
item = automationUtils.stringSplit(newInput.binding) item = automationUtils.stringSplit(loopStep.inputs.binding)
} else if (Array.isArray(loopStep.inputs.binding)) { } else if (Array.isArray(loopStep.inputs.binding)) {
item = loopStep.inputs.binding item = loopStep.inputs.binding
} }
@ -351,6 +344,7 @@ class Orchestrator {
} }
} }
} }
if ( if (
index === env.AUTOMATION_MAX_ITERATIONS || index === env.AUTOMATION_MAX_ITERATIONS ||
index === parseInt(loopStep.inputs.iterations) index === parseInt(loopStep.inputs.iterations)
@ -439,6 +433,7 @@ class Orchestrator {
break break
} }
} }
console.log("end of loop!")
} }
if (loopStep && iterations === 0) { if (loopStep && iterations === 0) {