diff --git a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/TriggerAutomation.svelte b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/TriggerAutomation.svelte
index aa617ab9a3..ecbb5a9a31 100644
--- a/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/TriggerAutomation.svelte
+++ b/packages/builder/src/components/design/settings/controls/ButtonActionEditor/actions/TriggerAutomation.svelte
@@ -23,6 +23,12 @@
if (automationStatus === AUTOMATION_STATUS.NEW) {
synchronous = false
}
+
+ if (automationStatus === AUTOMATION_STATUS.EXISTING) {
+ synchronous = automations.find(
+ automation => automation._id === parameters.automationId
+ ).synchronous
+ }
}
$: automations = $automationStore.automations
.filter(a => a.definition.trigger?.stepId === TriggerStepID.APP)
@@ -79,6 +85,8 @@
parameters.automationId = automationId
parameters.synchronous = synchronous
}
+
+ $: error = parameters.timeout > 120 ? "Timeout must be less than 120s" : null
@@ -133,6 +141,16 @@
>
+
+
+
+
+
{/if}
{
}
const triggerAutomationHandler = async action => {
- const { fields, notificationOverride, synchronous } = action.parameters
+ const { fields, notificationOverride, timeout } = action.parameters
if (fields) {
try {
- if (synchronous) {
- const result = await API.triggerSynchronousAutomation({
- automationId: action.parameters.automationId,
- fields,
- })
- console.log(typeof result)
+ const result = await API.triggerAutomation({
+ automationId: action.parameters.automationId,
+ fields,
+ timeout,
+ })
+ if (!notificationOverride) {
+ notificationStore.actions.success("Automation triggered")
+ }
+
+ // Value will exist if automation is synchronous, so return it.
+ if (result.value) {
return { result }
- } else {
- await API.triggerAutomation({
- automationId: action.parameters.automationId,
- fields,
- })
- if (!notificationOverride) {
- notificationStore.actions.success("Automation triggered")
- }
}
} catch (error) {
// Abort next actions
diff --git a/packages/frontend-core/src/api/automations.js b/packages/frontend-core/src/api/automations.js
index 12fdf6c926..37a834cf04 100644
--- a/packages/frontend-core/src/api/automations.js
+++ b/packages/frontend-core/src/api/automations.js
@@ -4,17 +4,10 @@ export const buildAutomationEndpoints = API => ({
* @param automationId the ID of the automation to trigger
* @param fields the fields to trigger the automation with
*/
- triggerAutomation: async ({ automationId, fields }) => {
+ triggerAutomation: async ({ automationId, fields, timeout }) => {
return await API.post({
url: `/api/automations/${automationId}/trigger`,
- body: { fields },
- })
- },
-
- triggerSynchronousAutomation: async ({ automationId, fields }) => {
- return await API.post({
- url: `/api/automations/${automationId}/triggerSynchronous`,
- body: { fields },
+ body: { fields, timeout },
})
},
diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts
index 2f56179ac0..dccab86156 100644
--- a/packages/server/src/api/controllers/automation.ts
+++ b/packages/server/src/api/controllers/automation.ts
@@ -19,6 +19,7 @@ import {
Automation,
AutomationActionStepId,
AutomationResults,
+ AutomationStepType,
BBContext,
} from "@budibase/types"
import { getActionDefinitions as actionDefs } from "../../automations/actions"
@@ -262,32 +263,34 @@ export async function getDefinitionList(ctx: BBContext) {
export async function trigger(ctx: BBContext) {
const db = context.getAppDB()
let automation = await db.get(ctx.params.id)
- await triggers.externalTrigger(automation, {
- ...ctx.request.body,
- appId: ctx.appId,
- })
- ctx.body = {
- message: `Automation ${automation._id} has been triggered.`,
- 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,
- {
+ 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, {
...ctx.request.body,
appId: ctx.appId,
- },
- { getResponses: true }
- )
-
- let collectedValue = response.steps.find(
- step => step.stepId === AutomationActionStepId.COLLECT
- )
- ctx.body = collectedValue?.outputs
+ })
+ ctx.body = {
+ message: `Automation ${automation._id} has been triggered.`,
+ automation,
+ }
+ }
}
function prepareTestInput(input: any) {
diff --git a/packages/server/src/api/routes/automation.ts b/packages/server/src/api/routes/automation.ts
index 9c0ce13c48..cb8b6042ae 100644
--- a/packages/server/src/api/routes/automation.ts
+++ b/packages/server/src/api/routes/automation.ts
@@ -73,17 +73,6 @@ router
),
controller.trigger
)
- .post(
- "/api/automations/:id/triggerSynchronous",
- appInfoMiddleware({ appType: AppType.PROD }),
- paramResource("id"),
- authorized(
- permissions.PermissionType.AUTOMATION,
- permissions.PermissionLevel.EXECUTE
- ),
- controller.triggerSynchronous
- )
-
.post(
"/api/automations/:id/test",
appInfoMiddleware({ appType: AppType.DEV }),
diff --git a/packages/server/src/automations/triggers.ts b/packages/server/src/automations/triggers.ts
index 78f8a87b0c..772ffa4fd2 100644
--- a/packages/server/src/automations/triggers.ts
+++ b/packages/server/src/automations/triggers.ts
@@ -91,7 +91,7 @@ emitter.on("row:delete", async function (event) {
export async function externalTrigger(
automation: Automation,
- params: { fields: Record },
+ params: { fields: Record; timeout?: number },
{ getResponses }: { getResponses?: boolean } = {}
) {
if (
@@ -116,7 +116,7 @@ export async function externalTrigger(
appId: context.getAppId(),
automation,
}
- return utils.processEvent({ data })
+ return utils.processEvent({ data }, { timeout: params.timeout })
} else {
return automationQueue.add(data, JOB_OPTS)
}
diff --git a/packages/server/src/automations/utils.ts b/packages/server/src/automations/utils.ts
index 5296a0fa50..82e3c941e8 100644
--- a/packages/server/src/automations/utils.ts
+++ b/packages/server/src/automations/utils.ts
@@ -14,13 +14,16 @@ import sdk from "../sdk"
const REBOOT_CRON = "@reboot"
const WH_STEP_ID = definitions.WEBHOOK.stepId
const CRON_STEP_ID = definitions.CRON.stepId
-const Runner = new Thread(ThreadType.AUTOMATION)
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}`
}
-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 {
const automationId = job.data.automation._id
console.log(jobMessage(job, "running"))