Logging updates for automations
This commit is contained in:
parent
42f472b038
commit
ad37186665
|
@ -104,6 +104,18 @@ async function newContext(updates: ContextMap, task: any) {
|
||||||
return Context.run(context, task)
|
return Context.run(context, task)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function doInAutomationContext(appId: string, automationId: string, task: any): Promise<any> {
|
||||||
|
const tenantId = getTenantIDFromAppID(appId)
|
||||||
|
return newContext(
|
||||||
|
{
|
||||||
|
tenantId,
|
||||||
|
appId,
|
||||||
|
automationId
|
||||||
|
},
|
||||||
|
task
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export async function doInContext(appId: string, task: any): Promise<any> {
|
export async function doInContext(appId: string, task: any): Promise<any> {
|
||||||
const tenantId = getTenantIDFromAppID(appId)
|
const tenantId = getTenantIDFromAppID(appId)
|
||||||
return newContext(
|
return newContext(
|
||||||
|
@ -187,6 +199,11 @@ export function getTenantId(): string {
|
||||||
return tenantId
|
return tenantId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getAutomationId(): string | undefined {
|
||||||
|
const context = Context.get()
|
||||||
|
return context?.automationId
|
||||||
|
}
|
||||||
|
|
||||||
export function getAppId(): string | undefined {
|
export function getAppId(): string | undefined {
|
||||||
const context = Context.get()
|
const context = Context.get()
|
||||||
const foundId = context?.appId
|
const foundId = context?.appId
|
||||||
|
|
|
@ -7,4 +7,5 @@ export type ContextMap = {
|
||||||
identity?: IdentityContext
|
identity?: IdentityContext
|
||||||
environmentVariables?: Record<string, string>
|
environmentVariables?: Record<string, string>
|
||||||
isScim?: boolean
|
isScim?: boolean
|
||||||
|
automationId?: string
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ if (!env.DISABLE_PINO_LOGGER) {
|
||||||
objects?: any[]
|
objects?: any[]
|
||||||
tenantId?: string
|
tenantId?: string
|
||||||
appId?: string
|
appId?: string
|
||||||
|
automationId?: string
|
||||||
identityId?: string
|
identityId?: string
|
||||||
identityType?: IdentityType
|
identityType?: IdentityType
|
||||||
correlationId?: string
|
correlationId?: string
|
||||||
|
@ -86,6 +87,7 @@ if (!env.DISABLE_PINO_LOGGER) {
|
||||||
contextObject = {
|
contextObject = {
|
||||||
tenantId: getTenantId(),
|
tenantId: getTenantId(),
|
||||||
appId: getAppId(),
|
appId: getAppId(),
|
||||||
|
automationId: getAutomationId(),
|
||||||
identityId: identity?._id,
|
identityId: identity?._id,
|
||||||
identityType: identity?.type,
|
identityType: identity?.type,
|
||||||
correlationId: correlation.getId(),
|
correlationId: correlation.getId(),
|
||||||
|
@ -159,6 +161,16 @@ if (!env.DISABLE_PINO_LOGGER) {
|
||||||
return appId
|
return appId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getAutomationId = () => {
|
||||||
|
let appId
|
||||||
|
try {
|
||||||
|
appId = context.getAutomationId()
|
||||||
|
} catch (e) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
return appId
|
||||||
|
}
|
||||||
|
|
||||||
const getIdentity = () => {
|
const getIdentity = () => {
|
||||||
let identity
|
let identity
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Job, JobId, Queue } from "bull"
|
import { Job, JobId, Queue } from "bull"
|
||||||
import { JobQueue } from "./constants"
|
import { JobQueue } from "./constants"
|
||||||
|
import * as context from "../context"
|
||||||
|
|
||||||
export type StalledFn = (job: Job) => Promise<void>
|
export type StalledFn = (job: Job) => Promise<void>
|
||||||
|
|
||||||
|
@ -31,77 +32,135 @@ function handleStalled(queue: Queue, removeStalledCb?: StalledFn) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function logging(queue: Queue, jobQueue: JobQueue) {
|
function getLogParams(
|
||||||
let eventType: string
|
eventType: QueueEventType,
|
||||||
switch (jobQueue) {
|
event: BullEvent,
|
||||||
case JobQueue.AUTOMATION:
|
opts: {
|
||||||
eventType = "automation-event"
|
job?: Job,
|
||||||
break
|
jobId?: JobId,
|
||||||
case JobQueue.APP_BACKUP:
|
error?: Error
|
||||||
eventType = "app-backup-event"
|
} = {},
|
||||||
break
|
extra: any = {}
|
||||||
case JobQueue.AUDIT_LOG:
|
) {
|
||||||
eventType = "audit-log-event"
|
const message = `[BULL] ${eventType}=${event}`
|
||||||
break
|
const err = opts.error
|
||||||
case JobQueue.SYSTEM_EVENT_QUEUE:
|
|
||||||
eventType = "system-event"
|
const data = {
|
||||||
break
|
eventType,
|
||||||
|
event,
|
||||||
|
job: opts.job,
|
||||||
|
jobId: opts.jobId || opts.job?.id,
|
||||||
|
...extra
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return [message, err, data]
|
||||||
|
}
|
||||||
|
|
||||||
|
enum BullEvent {
|
||||||
|
ERROR="error",
|
||||||
|
WAITING="waiting",
|
||||||
|
ACTIVE="active",
|
||||||
|
STALLED="stalled",
|
||||||
|
PROGRESS="progress",
|
||||||
|
COMPLETED="completed",
|
||||||
|
FAILED="failed",
|
||||||
|
PAUSED="paused",
|
||||||
|
RESUMED="resumed",
|
||||||
|
CLEANED="cleaned",
|
||||||
|
DRAINED="drained",
|
||||||
|
REMOVED="removed",
|
||||||
|
}
|
||||||
|
|
||||||
|
enum QueueEventType {
|
||||||
|
AUTOMATION_EVENT="automation-event",
|
||||||
|
APP_BACKUP_EVENT="app-backup-event",
|
||||||
|
AUDIT_LOG_EVENT="audit-log-event",
|
||||||
|
SYSTEM_EVENT="system-event"
|
||||||
|
}
|
||||||
|
|
||||||
|
const EventTypeMap: { [key in JobQueue]: QueueEventType } = {
|
||||||
|
[JobQueue.AUTOMATION]: QueueEventType.AUTOMATION_EVENT,
|
||||||
|
[JobQueue.APP_BACKUP]: QueueEventType.APP_BACKUP_EVENT,
|
||||||
|
[JobQueue.AUDIT_LOG]: QueueEventType.AUDIT_LOG_EVENT,
|
||||||
|
[JobQueue.SYSTEM_EVENT_QUEUE]: QueueEventType.SYSTEM_EVENT,
|
||||||
|
}
|
||||||
|
|
||||||
|
function logging(queue: Queue, jobQueue: JobQueue) {
|
||||||
|
const eventType = EventTypeMap[jobQueue]
|
||||||
|
|
||||||
|
function doInJobContext(job: Job, task: any) {
|
||||||
|
// if this is an automation job try to get the app id
|
||||||
|
const appId = job.data.event?.appId
|
||||||
|
if (appId) {
|
||||||
|
return context.doInContext(appId, task)
|
||||||
|
} else {
|
||||||
|
task()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
queue
|
||||||
|
.on(BullEvent.STALLED, async (job: Job) => {
|
||||||
|
// A job has been marked as stalled. This is useful for debugging job
|
||||||
|
// workers that crash or pause the event loop.
|
||||||
|
await doInJobContext(job, () => {
|
||||||
|
console.error(...getLogParams(eventType, BullEvent.STALLED, { job }))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.on(BullEvent.ERROR, (error: any) => {
|
||||||
|
// An error occurred.
|
||||||
|
console.error(...getLogParams(eventType, BullEvent.ERROR, { error }))
|
||||||
|
})
|
||||||
|
|
||||||
if (process.env.NODE_DEBUG?.includes("bull")) {
|
if (process.env.NODE_DEBUG?.includes("bull")) {
|
||||||
queue
|
queue
|
||||||
.on("error", (error: any) => {
|
.on(BullEvent.WAITING, (jobId: JobId) => {
|
||||||
// An error occurred.
|
|
||||||
console.error(`${eventType}=error error=${JSON.stringify(error)}`)
|
|
||||||
})
|
|
||||||
.on("waiting", (jobId: JobId) => {
|
|
||||||
// A Job is waiting to be processed as soon as a worker is idling.
|
// A Job is waiting to be processed as soon as a worker is idling.
|
||||||
console.log(`${eventType}=waiting jobId=${jobId}`)
|
console.info(...getLogParams(eventType, BullEvent.WAITING, { jobId }))
|
||||||
})
|
})
|
||||||
.on("active", (job: Job, jobPromise: any) => {
|
.on(BullEvent.ACTIVE, async (job: Job, jobPromise: any) => {
|
||||||
// A job has started. You can use `jobPromise.cancel()`` to abort it.
|
// A job has started. You can use `jobPromise.cancel()`` to abort it.
|
||||||
console.log(`${eventType}=active jobId=${job.id}`)
|
await doInJobContext(job, () => {
|
||||||
|
console.info(...getLogParams(eventType, BullEvent.ACTIVE, { job }))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.on("stalled", (job: Job) => {
|
.on(BullEvent.PROGRESS, async (job: Job, progress: any) => {
|
||||||
// A job has been marked as stalled. This is useful for debugging job
|
// A job's progress was updated
|
||||||
// workers that crash or pause the event loop.
|
await doInJobContext(job, () => {
|
||||||
console.error(
|
console.info(...getLogParams(eventType, BullEvent.PROGRESS, { job }, { progress }))
|
||||||
`${eventType}=stalled jobId=${job.id} job=${JSON.stringify(job)}`
|
})
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.on("progress", (job: Job, progress: any) => {
|
.on(BullEvent.COMPLETED, async (job: Job, result) => {
|
||||||
// A job's progress was updated!
|
|
||||||
console.log(
|
|
||||||
`${eventType}=progress jobId=${job.id} progress=${progress}`
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.on("completed", (job: Job, result) => {
|
|
||||||
// A job successfully completed with a `result`.
|
// A job successfully completed with a `result`.
|
||||||
console.log(`${eventType}=completed jobId=${job.id} result=${result}`)
|
await doInJobContext(job, () => {
|
||||||
|
console.info(...getLogParams(eventType, BullEvent.COMPLETED, { job }, { result }))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.on("failed", (job, err: any) => {
|
.on(BullEvent.FAILED, async (job: Job, error: any) => {
|
||||||
// A job failed with reason `err`!
|
// A job failed with reason `err`!
|
||||||
console.log(`${eventType}=failed jobId=${job.id} error=${err}`)
|
await doInJobContext(job, () => {
|
||||||
|
console.error(...getLogParams(eventType, BullEvent.FAILED, { job, error }))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.on("paused", () => {
|
.on(BullEvent.PAUSED, () => {
|
||||||
// The queue has been paused.
|
// The queue has been paused.
|
||||||
console.log(`${eventType}=paused`)
|
console.info(...getLogParams(eventType, BullEvent.PAUSED))
|
||||||
})
|
})
|
||||||
.on("resumed", (job: Job) => {
|
.on(BullEvent.RESUMED, () => {
|
||||||
// The queue has been resumed.
|
// The queue has been resumed.
|
||||||
console.log(`${eventType}=paused jobId=${job.id}`)
|
console.info(...getLogParams(eventType, BullEvent.RESUMED))
|
||||||
})
|
})
|
||||||
.on("cleaned", (jobs: Job[], type: string) => {
|
.on(BullEvent.CLEANED, (jobs: Job[], type: string) => {
|
||||||
// Old jobs have been cleaned from the queue. `jobs` is an array of cleaned
|
// Old jobs have been cleaned from the queue. `jobs` is an array of cleaned
|
||||||
// jobs, and `type` is the type of jobs cleaned.
|
// jobs, and `type` is the type of jobs cleaned.
|
||||||
console.log(`${eventType}=cleaned length=${jobs.length} type=${type}`)
|
console.info(...getLogParams(eventType, BullEvent.CLEANED, {}, { length: jobs.length, type } ))
|
||||||
})
|
})
|
||||||
.on("drained", () => {
|
.on(BullEvent.DRAINED, () => {
|
||||||
// Emitted every time the queue has processed all the waiting jobs (even if there can be some delayed jobs not yet processed)
|
// Emitted every time the queue has processed all the waiting jobs (even if there can be some delayed jobs not yet processed)
|
||||||
console.log(`${eventType}=drained`)
|
console.info(...getLogParams(eventType, BullEvent.DRAINED ))
|
||||||
})
|
})
|
||||||
.on("removed", (job: Job) => {
|
.on(BullEvent.REMOVED, (job: Job) => {
|
||||||
// A job successfully removed.
|
// A job successfully removed.
|
||||||
console.log(`${eventType}=removed jobId=${job.id}`)
|
console.info(...getLogParams(eventType, BullEvent.REMOVED, { job } ))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { db as dbCore, context } from "@budibase/backend-core"
|
||||||
import { getAutomationMetadataParams } from "../db/utils"
|
import { getAutomationMetadataParams } from "../db/utils"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
import { quotas } from "@budibase/pro"
|
import { quotas } from "@budibase/pro"
|
||||||
import { Automation, WebhookActionType } from "@budibase/types"
|
import { Automation, AutomationJob, WebhookActionType } from "@budibase/types"
|
||||||
import sdk from "../sdk"
|
import sdk from "../sdk"
|
||||||
|
|
||||||
const REBOOT_CRON = "@reboot"
|
const REBOOT_CRON = "@reboot"
|
||||||
|
@ -16,27 +16,32 @@ 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 Runner = new Thread(ThreadType.AUTOMATION)
|
||||||
|
|
||||||
const jobMessage = (job: any, message: string) => {
|
function loggingArgs(job: AutomationJob) {
|
||||||
return `app=${job.data.event.appId} automation=${job.data.automation._id} jobId=${job.id} trigger=${job.data.automation.definition.trigger.event} : ${message}`
|
return {
|
||||||
|
jobId: job.id,
|
||||||
|
trigger: job.data.automation.definition.trigger.event
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function processEvent(job: any) {
|
export async function processEvent(job: AutomationJob) {
|
||||||
try {
|
const appId = job.data.event.appId!
|
||||||
const automationId = job.data.automation._id
|
const automationId = job.data.automation._id!
|
||||||
console.log(jobMessage(job, "running"))
|
return await context.doInAutomationContext(appId, automationId, async () => {
|
||||||
// need to actually await these so that an error can be captured properly
|
try {
|
||||||
return await context.doInContext(job.data.event.appId, async () => {
|
// 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 runFn = () => Runner.run(job)
|
||||||
return quotas.addAutomation(runFn, {
|
const result = await quotas.addAutomation(runFn, {
|
||||||
automationId,
|
automationId,
|
||||||
})
|
})
|
||||||
})
|
console.log("automation completed", loggingArgs(job))
|
||||||
} catch (err) {
|
return result
|
||||||
const errJson = JSON.stringify(err)
|
} catch (err) {
|
||||||
console.error(jobMessage(job, `was unable to run - ${errJson}`))
|
console.error(`automation was unable to run`, err, loggingArgs(job))
|
||||||
console.trace(err)
|
return { err }
|
||||||
return { err }
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateTestHistory(
|
export async function updateTestHistory(
|
||||||
|
|
Loading…
Reference in New Issue