diff --git a/packages/server/src/automations/triggers.ts b/packages/server/src/automations/triggers.ts index 78f8a87b0c..3436c1be7a 100644 --- a/packages/server/src/automations/triggers.ts +++ b/packages/server/src/automations/triggers.ts @@ -9,7 +9,7 @@ import { checkTestFlag } from "../utilities/redis" import * as utils from "./utils" import env from "../environment" import { context, db as dbCore } from "@budibase/backend-core" -import { Automation, Row } from "@budibase/types" +import { Automation, Row, AutomationData, AutomationJob } from "@budibase/types" export const TRIGGER_DEFINITIONS = definitions const JOB_OPTS = { @@ -109,14 +109,16 @@ export async function externalTrigger( } params.fields = coercedFields } - const data: Record = { automation, event: params } + + const data: AutomationData = { automation, event: params as any } if (getResponses) { data.event = { ...data.event, appId: context.getAppId(), automation, } - return utils.processEvent({ data }) + const job = { data } as AutomationJob + return utils.processEvent(job) } else { return automationQueue.add(data, JOB_OPTS) } diff --git a/packages/server/src/definitions/automations.ts b/packages/server/src/definitions/automations.ts index 79cbe6e4ac..d7a7b42095 100644 --- a/packages/server/src/definitions/automations.ts +++ b/packages/server/src/definitions/automations.ts @@ -1,4 +1,4 @@ -import { AutomationResults, AutomationStep, Document } from "@budibase/types" +import { AutomationResults, AutomationStep } from "@budibase/types" export enum LoopStepType { ARRAY = "Array", @@ -28,6 +28,3 @@ export interface AutomationContext extends AutomationResults { trigger: any } -export interface AutomationMetadata extends Document { - errorCount?: number -} diff --git a/packages/server/src/threads/automation.ts b/packages/server/src/threads/automation.ts index adc1a83af0..6bfb6afc25 100644 --- a/packages/server/src/threads/automation.ts +++ b/packages/server/src/threads/automation.ts @@ -13,13 +13,12 @@ import { generateAutomationMetadataID, isProdAppID } from "../db/utils" import { definitions as triggerDefs } from "../automations/triggerInfo" import { AutomationErrors, MAX_AUTOMATION_RECURRING_ERRORS } from "../constants" import { storeLog } from "../automations/logging" -import { Automation, AutomationStep, AutomationStatus } from "@budibase/types" +import { Automation, AutomationStep, AutomationStatus, AutomationMetadata, AutomationJob } from "@budibase/types" import { LoopStep, LoopInput, TriggerOutput, AutomationContext, - AutomationMetadata, } from "../definitions/automations" import { WorkerCallback } from "./definitions" import { context, logging } from "@budibase/backend-core" @@ -60,11 +59,11 @@ class Orchestrator { _job: Job executionOutput: AutomationContext - constructor(job: Job) { - let automation = job.data.automation, - triggerOutput = job.data.event + constructor(job: AutomationJob) { + let automation = job.data.automation + let triggerOutput = job.data.event const metadata = triggerOutput.metadata - this._chainCount = metadata ? metadata.automationChainCount : 0 + this._chainCount = metadata ? metadata.automationChainCount! : 0 this._appId = triggerOutput.appId as string this._job = job const triggerStepId = automation.definition.trigger.stepId diff --git a/packages/server/src/threads/definitions.ts b/packages/server/src/threads/definitions.ts index 2cf5d8066c..21e7ce0b69 100644 --- a/packages/server/src/threads/definitions.ts +++ b/packages/server/src/threads/definitions.ts @@ -1,5 +1,3 @@ -import { EnvironmentVariablesDecrypted } from "@budibase/types" - export type WorkerCallback = (error: any, response?: any) => void export interface QueryEvent { diff --git a/packages/server/src/threads/index.ts b/packages/server/src/threads/index.ts index 6876f1a07c..9b6bffa867 100644 --- a/packages/server/src/threads/index.ts +++ b/packages/server/src/threads/index.ts @@ -1,5 +1,7 @@ import workerFarm from "worker-farm" import env from "../environment" +import { AutomationJob } from "@budibase/types" +import { QueryEvent } from "./definitions" export const ThreadType = { QUERY: "query", @@ -64,11 +66,11 @@ export class Thread { ) } - run(data: any) { + run(job: AutomationJob | QueryEvent) { const timeout = this.timeoutMs return new Promise((resolve, reject) => { function fire(worker: any) { - worker.execute(data, (err: any, response: any) => { + worker.execute(job, (err: any, response: any) => { if (err && err.type === "TimeoutError") { reject( new Error(`Query response time exceeded ${timeout}ms timeout.`) diff --git a/packages/types/src/documents/app/automation.ts b/packages/types/src/documents/app/automation.ts index 110a2c0642..f726e70b21 100644 --- a/packages/types/src/documents/app/automation.ts +++ b/packages/types/src/documents/app/automation.ts @@ -177,3 +177,8 @@ export type AutomationStepInput = { appId: string apiKey?: string } + +export interface AutomationMetadata extends Document { + errorCount?: number + automationChainCount?: number +} \ No newline at end of file diff --git a/packages/types/src/sdk/automations/index.ts b/packages/types/src/sdk/automations/index.ts new file mode 100644 index 0000000000..1025220791 --- /dev/null +++ b/packages/types/src/sdk/automations/index.ts @@ -0,0 +1,15 @@ +import { Automation, AutomationMetadata } from "../../documents" +import { Job } from "bull" + +export interface AutomationDataEvent { + appId?: string + metadata?: AutomationMetadata + automation?: Automation +} + +export interface AutomationData { + event: AutomationDataEvent + automation: Automation +} + +export type AutomationJob = Job \ No newline at end of file diff --git a/packages/types/src/sdk/index.ts b/packages/types/src/sdk/index.ts index 8fc5fb287e..ed44c13667 100644 --- a/packages/types/src/sdk/index.ts +++ b/packages/types/src/sdk/index.ts @@ -1,3 +1,4 @@ +export * from "./automations" export * from "./hosting" export * from "./context" export * from "./events"