sync / async automations go through one endpoint
This commit is contained in:
parent
1626571081
commit
dcfb65b92d
|
@ -23,6 +23,12 @@
|
||||||
if (automationStatus === AUTOMATION_STATUS.NEW) {
|
if (automationStatus === AUTOMATION_STATUS.NEW) {
|
||||||
synchronous = false
|
synchronous = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (automationStatus === AUTOMATION_STATUS.EXISTING) {
|
||||||
|
synchronous = automations.find(
|
||||||
|
automation => automation._id === parameters.automationId
|
||||||
|
).synchronous
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$: automations = $automationStore.automations
|
$: automations = $automationStore.automations
|
||||||
.filter(a => a.definition.trigger?.stepId === TriggerStepID.APP)
|
.filter(a => a.definition.trigger?.stepId === TriggerStepID.APP)
|
||||||
|
@ -79,6 +85,8 @@
|
||||||
parameters.automationId = automationId
|
parameters.automationId = automationId
|
||||||
parameters.synchronous = synchronous
|
parameters.synchronous = synchronous
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: error = parameters.timeout > 120 ? "Timeout must be less than 120s" : null
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="root">
|
<div class="root">
|
||||||
|
@ -133,6 +141,16 @@
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<Label small />
|
||||||
|
|
||||||
|
<div class="timeout-width">
|
||||||
|
<Input
|
||||||
|
label="Timeout in seconds (120 max)"
|
||||||
|
type="number"
|
||||||
|
{error}
|
||||||
|
bind:value={parameters.timeout}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<Label small />
|
<Label small />
|
||||||
<Checkbox
|
<Checkbox
|
||||||
|
@ -169,6 +187,9 @@
|
||||||
max-width: 800px;
|
max-width: 800px;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
.timeout-width {
|
||||||
|
width: 30%;
|
||||||
|
}
|
||||||
|
|
||||||
.params {
|
.params {
|
||||||
display: grid;
|
display: grid;
|
||||||
|
|
|
@ -122,24 +122,21 @@ const deleteRowHandler = async action => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const triggerAutomationHandler = async action => {
|
const triggerAutomationHandler = async action => {
|
||||||
const { fields, notificationOverride, synchronous } = action.parameters
|
const { fields, notificationOverride, timeout } = action.parameters
|
||||||
if (fields) {
|
if (fields) {
|
||||||
try {
|
try {
|
||||||
if (synchronous) {
|
const result = await API.triggerAutomation({
|
||||||
const result = await API.triggerSynchronousAutomation({
|
|
||||||
automationId: action.parameters.automationId,
|
|
||||||
fields,
|
|
||||||
})
|
|
||||||
console.log(typeof result)
|
|
||||||
return { result }
|
|
||||||
} else {
|
|
||||||
await API.triggerAutomation({
|
|
||||||
automationId: action.parameters.automationId,
|
automationId: action.parameters.automationId,
|
||||||
fields,
|
fields,
|
||||||
|
timeout,
|
||||||
})
|
})
|
||||||
if (!notificationOverride) {
|
if (!notificationOverride) {
|
||||||
notificationStore.actions.success("Automation triggered")
|
notificationStore.actions.success("Automation triggered")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Value will exist if automation is synchronous, so return it.
|
||||||
|
if (result.value) {
|
||||||
|
return { result }
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Abort next actions
|
// Abort next actions
|
||||||
|
|
|
@ -4,17 +4,10 @@ export const buildAutomationEndpoints = API => ({
|
||||||
* @param automationId the ID of the automation to trigger
|
* @param automationId the ID of the automation to trigger
|
||||||
* @param fields the fields to trigger the automation with
|
* @param fields the fields to trigger the automation with
|
||||||
*/
|
*/
|
||||||
triggerAutomation: async ({ automationId, fields }) => {
|
triggerAutomation: async ({ automationId, fields, timeout }) => {
|
||||||
return await API.post({
|
return await API.post({
|
||||||
url: `/api/automations/${automationId}/trigger`,
|
url: `/api/automations/${automationId}/trigger`,
|
||||||
body: { fields },
|
body: { fields, timeout },
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
triggerSynchronousAutomation: async ({ automationId, fields }) => {
|
|
||||||
return await API.post({
|
|
||||||
url: `/api/automations/${automationId}/triggerSynchronous`,
|
|
||||||
body: { fields },
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import {
|
||||||
Automation,
|
Automation,
|
||||||
AutomationActionStepId,
|
AutomationActionStepId,
|
||||||
AutomationResults,
|
AutomationResults,
|
||||||
|
AutomationStepType,
|
||||||
BBContext,
|
BBContext,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { getActionDefinitions as actionDefs } from "../../automations/actions"
|
import { getActionDefinitions as actionDefs } from "../../automations/actions"
|
||||||
|
@ -262,6 +263,25 @@ export async function getDefinitionList(ctx: BBContext) {
|
||||||
export async function trigger(ctx: BBContext) {
|
export async function trigger(ctx: BBContext) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
let automation = await db.get(ctx.params.id)
|
let automation = await db.get(ctx.params.id)
|
||||||
|
|
||||||
|
let hasCollectBlock = automation.definition.steps.some(
|
||||||
|
(step: any) => step.stepId === AutomationActionStepId.COLLECT
|
||||||
|
)
|
||||||
|
if (hasCollectBlock) {
|
||||||
|
const response: AutomationResults = await triggers.externalTrigger(
|
||||||
|
automation,
|
||||||
|
{
|
||||||
|
fields: ctx.request.body.fields,
|
||||||
|
timeout: ctx.request.body.timeout || 120000,
|
||||||
|
},
|
||||||
|
{ getResponses: true }
|
||||||
|
)
|
||||||
|
|
||||||
|
let collectedValue = response.steps.find(
|
||||||
|
step => step.stepId === AutomationActionStepId.COLLECT
|
||||||
|
)
|
||||||
|
ctx.body = collectedValue?.outputs
|
||||||
|
} else {
|
||||||
await triggers.externalTrigger(automation, {
|
await triggers.externalTrigger(automation, {
|
||||||
...ctx.request.body,
|
...ctx.request.body,
|
||||||
appId: ctx.appId,
|
appId: ctx.appId,
|
||||||
|
@ -271,23 +291,6 @@ export async function trigger(ctx: BBContext) {
|
||||||
automation,
|
automation,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function triggerSynchronous(ctx: BBContext) {
|
|
||||||
const db = context.getAppDB()
|
|
||||||
let automation = await db.get(ctx.params.id)
|
|
||||||
const response: AutomationResults = await triggers.externalTrigger(
|
|
||||||
automation,
|
|
||||||
{
|
|
||||||
...ctx.request.body,
|
|
||||||
appId: ctx.appId,
|
|
||||||
},
|
|
||||||
{ getResponses: true }
|
|
||||||
)
|
|
||||||
|
|
||||||
let collectedValue = response.steps.find(
|
|
||||||
step => step.stepId === AutomationActionStepId.COLLECT
|
|
||||||
)
|
|
||||||
ctx.body = collectedValue?.outputs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepareTestInput(input: any) {
|
function prepareTestInput(input: any) {
|
||||||
|
|
|
@ -73,17 +73,6 @@ router
|
||||||
),
|
),
|
||||||
controller.trigger
|
controller.trigger
|
||||||
)
|
)
|
||||||
.post(
|
|
||||||
"/api/automations/:id/triggerSynchronous",
|
|
||||||
appInfoMiddleware({ appType: AppType.PROD }),
|
|
||||||
paramResource("id"),
|
|
||||||
authorized(
|
|
||||||
permissions.PermissionType.AUTOMATION,
|
|
||||||
permissions.PermissionLevel.EXECUTE
|
|
||||||
),
|
|
||||||
controller.triggerSynchronous
|
|
||||||
)
|
|
||||||
|
|
||||||
.post(
|
.post(
|
||||||
"/api/automations/:id/test",
|
"/api/automations/:id/test",
|
||||||
appInfoMiddleware({ appType: AppType.DEV }),
|
appInfoMiddleware({ appType: AppType.DEV }),
|
||||||
|
|
|
@ -91,7 +91,7 @@ emitter.on("row:delete", async function (event) {
|
||||||
|
|
||||||
export async function externalTrigger(
|
export async function externalTrigger(
|
||||||
automation: Automation,
|
automation: Automation,
|
||||||
params: { fields: Record<string, any> },
|
params: { fields: Record<string, any>; timeout?: number },
|
||||||
{ getResponses }: { getResponses?: boolean } = {}
|
{ getResponses }: { getResponses?: boolean } = {}
|
||||||
) {
|
) {
|
||||||
if (
|
if (
|
||||||
|
@ -116,7 +116,7 @@ export async function externalTrigger(
|
||||||
appId: context.getAppId(),
|
appId: context.getAppId(),
|
||||||
automation,
|
automation,
|
||||||
}
|
}
|
||||||
return utils.processEvent({ data })
|
return utils.processEvent({ data }, { timeout: params.timeout })
|
||||||
} else {
|
} else {
|
||||||
return automationQueue.add(data, JOB_OPTS)
|
return automationQueue.add(data, JOB_OPTS)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,13 +14,16 @@ import sdk from "../sdk"
|
||||||
const REBOOT_CRON = "@reboot"
|
const REBOOT_CRON = "@reboot"
|
||||||
const WH_STEP_ID = definitions.WEBHOOK.stepId
|
const WH_STEP_ID = definitions.WEBHOOK.stepId
|
||||||
const CRON_STEP_ID = definitions.CRON.stepId
|
const CRON_STEP_ID = definitions.CRON.stepId
|
||||||
const Runner = new Thread(ThreadType.AUTOMATION)
|
|
||||||
|
|
||||||
const jobMessage = (job: any, message: string) => {
|
const jobMessage = (job: any, message: string) => {
|
||||||
return `app=${job.data.event.appId} automation=${job.data.automation._id} jobId=${job.id} trigger=${job.data.automation.definition.trigger.event} : ${message}`
|
return `app=${job.data.event.appId} automation=${job.data.automation._id} jobId=${job.id} trigger=${job.data.automation.definition.trigger.event} : ${message}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function processEvent(job: any) {
|
export async function processEvent(job: any, timeout?: { timeout?: number }) {
|
||||||
|
const Runner = new Thread(ThreadType.AUTOMATION, {
|
||||||
|
timeout: timeout || null,
|
||||||
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const automationId = job.data.automation._id
|
const automationId = job.data.automation._id
|
||||||
console.log(jobMessage(job, "running"))
|
console.log(jobMessage(job, "running"))
|
||||||
|
|
Loading…
Reference in New Issue