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) {
|
||||
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
|
||||
</script>
|
||||
|
||||
<div class="root">
|
||||
|
@ -133,6 +141,16 @@
|
|||
>
|
||||
</div>
|
||||
</div>
|
||||
<Label small />
|
||||
|
||||
<div class="timeout-width">
|
||||
<Input
|
||||
label="Timeout in seconds (120 max)"
|
||||
type="number"
|
||||
{error}
|
||||
bind:value={parameters.timeout}
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
<Label small />
|
||||
<Checkbox
|
||||
|
@ -169,6 +187,9 @@
|
|||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.timeout-width {
|
||||
width: 30%;
|
||||
}
|
||||
|
||||
.params {
|
||||
display: grid;
|
||||
|
|
|
@ -122,24 +122,21 @@ const deleteRowHandler = async action => {
|
|||
}
|
||||
|
||||
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)
|
||||
return { result }
|
||||
} else {
|
||||
await API.triggerAutomation({
|
||||
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 }
|
||||
}
|
||||
} catch (error) {
|
||||
// Abort next actions
|
||||
|
|
|
@ -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 },
|
||||
})
|
||||
},
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ import {
|
|||
Automation,
|
||||
AutomationActionStepId,
|
||||
AutomationResults,
|
||||
AutomationStepType,
|
||||
BBContext,
|
||||
} from "@budibase/types"
|
||||
import { getActionDefinitions as actionDefs } from "../../automations/actions"
|
||||
|
@ -262,6 +263,25 @@ export async function getDefinitionList(ctx: BBContext) {
|
|||
export async function trigger(ctx: BBContext) {
|
||||
const db = context.getAppDB()
|
||||
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, {
|
||||
...ctx.request.body,
|
||||
appId: ctx.appId,
|
||||
|
@ -271,23 +291,6 @@ export async function trigger(ctx: BBContext) {
|
|||
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) {
|
||||
|
|
|
@ -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 }),
|
||||
|
|
|
@ -91,7 +91,7 @@ emitter.on("row:delete", async function (event) {
|
|||
|
||||
export async function externalTrigger(
|
||||
automation: Automation,
|
||||
params: { fields: Record<string, any> },
|
||||
params: { fields: Record<string, any>; 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)
|
||||
}
|
||||
|
|
|
@ -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"))
|
||||
|
|
Loading…
Reference in New Issue