2022-03-20 02:13:54 +01:00
|
|
|
import { Thread, ThreadType } from "../threads"
|
|
|
|
import { definitions } from "./triggerInfo"
|
2022-10-13 18:39:26 +02:00
|
|
|
import { automationQueue } from "./bullboard"
|
2022-03-20 02:13:54 +01:00
|
|
|
import { updateEntityMetadata } from "../utilities"
|
2022-10-25 19:19:18 +02:00
|
|
|
import { MetadataTypes } from "../constants"
|
2024-07-11 15:27:48 +02:00
|
|
|
import { db as dbCore, context, utils } from "@budibase/backend-core"
|
2022-09-28 09:56:45 +02:00
|
|
|
import { getAutomationMetadataParams } from "../db/utils"
|
2022-03-20 02:13:54 +01:00
|
|
|
import { cloneDeep } from "lodash/fp"
|
|
|
|
import { quotas } from "@budibase/pro"
|
2024-07-17 17:46:48 +02:00
|
|
|
import { Automation, AutomationJob } from "@budibase/types"
|
2023-09-05 13:28:56 +02:00
|
|
|
import { automationsEnabled } from "../features"
|
2024-02-13 16:14:03 +01:00
|
|
|
import { helpers, REBOOT_CRON } from "@budibase/shared-core"
|
2024-01-02 12:36:32 +01:00
|
|
|
import tracer from "dd-trace"
|
2021-09-08 20:29:28 +02:00
|
|
|
|
2023-09-01 16:12:23 +02:00
|
|
|
const CRON_STEP_ID = definitions.CRON.stepId
|
2023-09-05 13:28:56 +02:00
|
|
|
let Runner: Thread
|
|
|
|
if (automationsEnabled()) {
|
|
|
|
Runner = new Thread(ThreadType.AUTOMATION)
|
|
|
|
}
|
2021-09-07 20:06:20 +02:00
|
|
|
|
2023-09-01 16:12:23 +02:00
|
|
|
function loggingArgs(job: AutomationJob) {
|
|
|
|
return [
|
2023-05-17 23:18:50 +02:00
|
|
|
{
|
|
|
|
_logKey: "automation",
|
|
|
|
trigger: job.data.automation.definition.trigger.event,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
_logKey: "bull",
|
|
|
|
jobId: job.id,
|
|
|
|
},
|
|
|
|
]
|
2022-09-28 09:56:45 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 14:54:20 +02:00
|
|
|
export async function processEvent(job: AutomationJob) {
|
2024-01-02 12:36:32 +01:00
|
|
|
return tracer.trace(
|
|
|
|
"processEvent",
|
|
|
|
{ resource: "automation" },
|
|
|
|
async span => {
|
|
|
|
const appId = job.data.event.appId!
|
|
|
|
const automationId = job.data.automation._id!
|
2023-05-17 14:54:20 +02:00
|
|
|
|
2024-01-02 12:36:32 +01:00
|
|
|
span?.addTags({
|
|
|
|
appId,
|
2022-05-26 17:01:10 +02:00
|
|
|
automationId,
|
2024-01-02 12:36:32 +01:00
|
|
|
job: {
|
|
|
|
id: job.id,
|
|
|
|
name: job.name,
|
|
|
|
attemptsMade: job.attemptsMade,
|
|
|
|
opts: {
|
|
|
|
attempts: job.opts.attempts,
|
|
|
|
priority: job.opts.priority,
|
|
|
|
delay: job.opts.delay,
|
|
|
|
repeat: job.opts.repeat,
|
|
|
|
backoff: job.opts.backoff,
|
|
|
|
lifo: job.opts.lifo,
|
|
|
|
timeout: job.opts.timeout,
|
|
|
|
jobId: job.opts.jobId,
|
|
|
|
removeOnComplete: job.opts.removeOnComplete,
|
|
|
|
removeOnFail: job.opts.removeOnFail,
|
|
|
|
stackTraceLimit: job.opts.stackTraceLimit,
|
|
|
|
preventParsingData: job.opts.preventParsingData,
|
|
|
|
},
|
|
|
|
},
|
2022-05-26 17:01:10 +02:00
|
|
|
})
|
2023-12-21 12:06:05 +01:00
|
|
|
|
2024-01-02 12:36:32 +01:00
|
|
|
const task = async () => {
|
|
|
|
try {
|
|
|
|
// need to actually await these so that an error can be captured properly
|
|
|
|
console.log("automation running", ...loggingArgs(job))
|
|
|
|
|
|
|
|
const runFn = () => Runner.run(job)
|
|
|
|
const result = await quotas.addAutomation(runFn, {
|
|
|
|
automationId,
|
|
|
|
})
|
|
|
|
console.log("automation completed", ...loggingArgs(job))
|
|
|
|
return result
|
|
|
|
} catch (err) {
|
|
|
|
span?.addTags({ error: true })
|
|
|
|
console.error(
|
|
|
|
`automation was unable to run`,
|
|
|
|
err,
|
|
|
|
...loggingArgs(job)
|
|
|
|
)
|
|
|
|
return { err }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return await context.doInAutomationContext({ appId, automationId, task })
|
|
|
|
}
|
|
|
|
)
|
2021-09-07 20:06:20 +02:00
|
|
|
}
|
2021-09-08 20:29:28 +02:00
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export async function updateTestHistory(
|
|
|
|
appId: any,
|
|
|
|
automation: any,
|
|
|
|
history: any
|
|
|
|
) {
|
2021-09-10 14:52:41 +02:00
|
|
|
return updateEntityMetadata(
|
|
|
|
MetadataTypes.AUTOMATION_TEST_HISTORY,
|
|
|
|
automation._id,
|
2022-03-20 02:13:54 +01:00
|
|
|
(metadata: any) => {
|
2021-09-10 14:52:41 +02:00
|
|
|
if (metadata && Array.isArray(metadata.history)) {
|
|
|
|
metadata.history.push(history)
|
|
|
|
} else {
|
|
|
|
metadata = {
|
|
|
|
history: [history],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return metadata
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export function removeDeprecated(definitions: any) {
|
2021-12-14 18:59:02 +01:00
|
|
|
const base = cloneDeep(definitions)
|
|
|
|
for (let key of Object.keys(base)) {
|
|
|
|
if (base[key].deprecated) {
|
|
|
|
delete base[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base
|
|
|
|
}
|
|
|
|
|
2021-09-08 20:29:28 +02:00
|
|
|
// end the repetition and the job itself
|
2022-03-20 02:13:54 +01:00
|
|
|
export async function disableAllCrons(appId: any) {
|
2021-09-08 20:29:28 +02:00
|
|
|
const promises = []
|
2022-10-13 18:39:26 +02:00
|
|
|
const jobs = await automationQueue.getRepeatableJobs()
|
2021-09-08 20:29:28 +02:00
|
|
|
for (let job of jobs) {
|
|
|
|
if (job.key.includes(`${appId}_cron`)) {
|
2022-10-13 18:39:26 +02:00
|
|
|
promises.push(automationQueue.removeRepeatableByKey(job.key))
|
2022-03-20 02:13:54 +01:00
|
|
|
if (job.id) {
|
2022-10-13 18:39:26 +02:00
|
|
|
promises.push(automationQueue.removeJobs(job.id))
|
2022-03-20 02:13:54 +01:00
|
|
|
}
|
2021-09-08 20:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-05 13:28:56 +02:00
|
|
|
const results = await Promise.all(promises)
|
|
|
|
return { count: results.length / 2 }
|
2021-09-08 20:29:28 +02:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:26:42 +02:00
|
|
|
export async function disableCronById(jobId: number | string) {
|
|
|
|
const repeatJobs = await automationQueue.getRepeatableJobs()
|
|
|
|
for (let repeatJob of repeatJobs) {
|
|
|
|
if (repeatJob.id === jobId) {
|
|
|
|
await automationQueue.removeRepeatableByKey(repeatJob.key)
|
|
|
|
}
|
|
|
|
}
|
2022-09-28 09:56:45 +02:00
|
|
|
console.log(`jobId=${jobId} disabled`)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function clearMetadata() {
|
2022-11-11 12:57:50 +01:00
|
|
|
const db = context.getProdAppDB()
|
2022-09-28 09:56:45 +02:00
|
|
|
const automationMetadata = (
|
|
|
|
await db.allDocs(
|
|
|
|
getAutomationMetadataParams({
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map((row: any) => row.doc)
|
|
|
|
for (let metadata of automationMetadata) {
|
|
|
|
metadata._deleted = true
|
|
|
|
}
|
|
|
|
await db.bulkDocs(automationMetadata)
|
|
|
|
}
|
|
|
|
|
2023-09-01 16:12:23 +02:00
|
|
|
export function isCronTrigger(auto: Automation) {
|
|
|
|
return (
|
|
|
|
auto &&
|
|
|
|
auto.definition.trigger &&
|
|
|
|
auto.definition.trigger.stepId === CRON_STEP_ID
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isRebootTrigger(auto: Automation) {
|
|
|
|
const trigger = auto ? auto.definition.trigger : null
|
|
|
|
return isCronTrigger(auto) && trigger?.inputs.cron === REBOOT_CRON
|
|
|
|
}
|
|
|
|
|
2021-09-08 20:29:28 +02:00
|
|
|
/**
|
|
|
|
* This function handles checking of any cron jobs that need to be enabled/updated.
|
2023-10-17 17:46:32 +02:00
|
|
|
* @param appId The ID of the app in which we are checking for webhooks
|
|
|
|
* @param automation The automation object to be updated.
|
2021-09-08 20:29:28 +02:00
|
|
|
*/
|
2022-09-28 09:56:45 +02:00
|
|
|
export async function enableCronTrigger(appId: any, automation: Automation) {
|
2021-09-08 20:29:28 +02:00
|
|
|
const trigger = automation ? automation.definition.trigger : null
|
2023-09-05 13:28:56 +02:00
|
|
|
let enabled = false
|
2022-09-28 09:56:45 +02:00
|
|
|
|
2021-09-08 20:29:28 +02:00
|
|
|
// need to create cron job
|
2023-09-01 16:12:23 +02:00
|
|
|
if (
|
|
|
|
isCronTrigger(automation) &&
|
|
|
|
!isRebootTrigger(automation) &&
|
2024-05-20 16:13:08 +02:00
|
|
|
!automation.disabled &&
|
2023-09-01 16:12:23 +02:00
|
|
|
trigger?.inputs.cron
|
|
|
|
) {
|
2024-02-09 19:04:40 +01:00
|
|
|
const cronExp = trigger.inputs.cron
|
|
|
|
const validation = helpers.cron.validate(cronExp)
|
|
|
|
if (!validation.valid) {
|
|
|
|
throw new Error(
|
|
|
|
`Invalid automation CRON "${cronExp}" - ${validation.err.join(", ")}`
|
|
|
|
)
|
|
|
|
}
|
2021-09-08 20:29:28 +02:00
|
|
|
// make a job id rather than letting Bull decide, makes it easier to handle on way out
|
2024-07-11 15:27:48 +02:00
|
|
|
const jobId = `${appId}_cron_${utils.newid()}`
|
2022-10-13 18:39:26 +02:00
|
|
|
const job: any = await automationQueue.add(
|
2021-09-08 20:29:28 +02:00
|
|
|
{
|
|
|
|
automation,
|
|
|
|
event: { appId, timestamp: Date.now() },
|
|
|
|
},
|
2024-02-09 19:04:40 +01:00
|
|
|
{ repeat: { cron: cronExp }, jobId }
|
2021-09-08 20:29:28 +02:00
|
|
|
)
|
|
|
|
// Assign cron job ID from bull so we can remove it later if the cron trigger is removed
|
|
|
|
trigger.cronJobId = job.id
|
2022-01-31 18:27:47 +01:00
|
|
|
// can't use getAppDB here as this is likely to be called from dev app,
|
|
|
|
// but this call could be for dev app or prod app, need to just use what
|
|
|
|
// was passed in
|
2022-11-11 12:57:50 +01:00
|
|
|
await dbCore.doWithDB(appId, async (db: any) => {
|
2022-04-19 20:42:52 +02:00
|
|
|
const response = await db.put(automation)
|
|
|
|
automation._id = response.id
|
|
|
|
automation._rev = response.rev
|
|
|
|
})
|
2023-09-05 13:28:56 +02:00
|
|
|
enabled = true
|
2021-09-08 20:29:28 +02:00
|
|
|
}
|
2023-09-05 13:28:56 +02:00
|
|
|
return { enabled, automation }
|
2021-09-08 20:29:28 +02:00
|
|
|
}
|
|
|
|
|
2021-11-17 17:28:52 +01:00
|
|
|
/**
|
|
|
|
* When removing an app/unpublishing it need to make sure automations are cleaned up (cron).
|
2023-10-17 17:46:32 +02:00
|
|
|
* @param appId the app that is being removed.
|
|
|
|
* @return clean is complete if this succeeds.
|
2021-11-17 17:28:52 +01:00
|
|
|
*/
|
2022-03-20 02:13:54 +01:00
|
|
|
export async function cleanupAutomations(appId: any) {
|
|
|
|
await disableAllCrons(appId)
|
2021-11-17 17:28:52 +01:00
|
|
|
}
|
2022-09-28 09:56:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the supplied automation is of a recurring type.
|
|
|
|
* @param automation The automation to check.
|
2023-10-17 17:46:32 +02:00
|
|
|
* @return if it is recurring (cron).
|
2022-09-28 09:56:45 +02:00
|
|
|
*/
|
|
|
|
export function isRecurring(automation: Automation) {
|
|
|
|
return automation.definition.trigger.stepId === definitions.CRON.stepId
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isErrorInOutput(output: {
|
|
|
|
steps: { outputs?: { success: boolean } }[]
|
|
|
|
}) {
|
|
|
|
let first = true,
|
|
|
|
error = false
|
|
|
|
for (let step of output.steps) {
|
|
|
|
// skip the trigger, its always successful if automation ran
|
|
|
|
if (first) {
|
|
|
|
first = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (!step.outputs?.success) {
|
|
|
|
error = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error
|
|
|
|
}
|