2020-09-11 19:47:22 +02:00
|
|
|
const mustache = require("mustache")
|
|
|
|
const actions = require("./actions")
|
|
|
|
const logic = require("./logic")
|
|
|
|
|
2020-09-17 17:16:05 +02:00
|
|
|
const FILTER_STEP_ID = logic.BUILTIN_DEFINITIONS.FILTER.stepId
|
|
|
|
|
2020-09-17 14:36:19 +02:00
|
|
|
function cleanMustache(string) {
|
|
|
|
let charToReplace = {
|
|
|
|
"[": ".",
|
|
|
|
"]": "",
|
|
|
|
}
|
|
|
|
let regex = new RegExp(/{{[^}}]*}}/g)
|
2020-09-22 09:58:16 +02:00
|
|
|
for (let match of string.matchAll(regex)) {
|
2020-09-17 14:36:19 +02:00
|
|
|
let baseIdx = string.indexOf(match)
|
|
|
|
for (let key of Object.keys(charToReplace)) {
|
|
|
|
let idxChar = match[0].indexOf(key)
|
|
|
|
if (idxChar !== -1) {
|
|
|
|
string =
|
|
|
|
string.slice(baseIdx, baseIdx + idxChar) +
|
|
|
|
charToReplace[key] +
|
|
|
|
string.slice(baseIdx + idxChar + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string
|
|
|
|
}
|
|
|
|
|
2020-09-16 15:00:04 +02:00
|
|
|
function recurseMustache(inputs, context) {
|
2020-09-17 14:36:19 +02:00
|
|
|
for (let key of Object.keys(inputs)) {
|
2020-09-16 15:00:04 +02:00
|
|
|
let val = inputs[key]
|
|
|
|
if (typeof val === "string") {
|
2020-09-17 14:36:19 +02:00
|
|
|
val = cleanMustache(inputs[key])
|
|
|
|
inputs[key] = mustache.render(val, context)
|
2020-09-16 15:00:04 +02:00
|
|
|
}
|
|
|
|
// this covers objects and arrays
|
|
|
|
else if (typeof val === "object") {
|
|
|
|
inputs[key] = recurseMustache(inputs[key], context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inputs
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:47:22 +02:00
|
|
|
/**
|
|
|
|
* The workflow orchestrator is a class responsible for executing workflows.
|
|
|
|
* It handles the context of the workflow and makes sure each step gets the correct
|
|
|
|
* inputs and handles any outputs.
|
|
|
|
*/
|
|
|
|
class Orchestrator {
|
2020-09-16 15:00:04 +02:00
|
|
|
constructor(workflow, triggerOutput) {
|
|
|
|
this._instanceId = triggerOutput.instanceId
|
2020-09-17 17:16:05 +02:00
|
|
|
// remove from context
|
|
|
|
delete triggerOutput.instanceId
|
2020-09-17 14:36:19 +02:00
|
|
|
// step zero is never used as the mustache is zero indexed for customer facing
|
|
|
|
this._context = { steps: [{}], trigger: triggerOutput }
|
2020-09-11 19:47:22 +02:00
|
|
|
this._workflow = workflow
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
throw `Cannot find workflow step by name ${stepId}`
|
|
|
|
}
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2020-09-16 15:00:04 +02:00
|
|
|
async execute() {
|
2020-09-11 19:47:22 +02:00
|
|
|
let workflow = this._workflow
|
2020-09-17 14:36:19 +02:00
|
|
|
for (let step of workflow.definition.steps) {
|
|
|
|
let stepFn = await this.getStepFunctionality(step.type, step.stepId)
|
|
|
|
step.inputs = recurseMustache(step.inputs, this._context)
|
2020-09-16 15:00:04 +02:00
|
|
|
// instanceId is always passed
|
|
|
|
const outputs = await stepFn({
|
2020-09-17 14:36:19 +02:00
|
|
|
inputs: step.inputs,
|
2020-09-16 15:00:04 +02:00
|
|
|
instanceId: this._instanceId,
|
2020-09-11 19:47:22 +02:00
|
|
|
})
|
2020-09-17 17:16:05 +02:00
|
|
|
if (step.stepId === FILTER_STEP_ID && !outputs.success) {
|
|
|
|
break
|
|
|
|
}
|
2020-09-17 14:36:19 +02:00
|
|
|
this._context.steps.push(outputs)
|
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
|
2020-09-11 19:47:22 +02:00
|
|
|
module.exports = async (job, cb = null) => {
|
|
|
|
try {
|
2020-09-16 15:00:04 +02:00
|
|
|
const workflowOrchestrator = new Orchestrator(
|
|
|
|
job.data.workflow,
|
|
|
|
job.data.event
|
|
|
|
)
|
|
|
|
await workflowOrchestrator.execute()
|
2020-09-11 19:47:22 +02:00
|
|
|
if (cb) {
|
|
|
|
cb()
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (cb) {
|
|
|
|
cb(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|