@budibase/backend-core 2.6.16 → 2.6.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@budibase/backend-core",
3
- "version": "2.6.16",
3
+ "version": "2.6.18",
4
4
  "description": "Budibase backend core libraries used in server and worker",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  "dependencies": {
25
25
  "@budibase/nano": "10.1.2",
26
26
  "@budibase/pouchdb-replication-stream": "1.2.10",
27
- "@budibase/types": "^2.6.16",
27
+ "@budibase/types": "^2.6.18",
28
28
  "@shopify/jest-koa-mocks": "5.0.1",
29
29
  "@techpass/passport-openidconnect": "0.3.2",
30
30
  "aws-cloudfront-sign": "2.2.0",
@@ -90,5 +90,5 @@
90
90
  "tsconfig-paths": "4.0.0",
91
91
  "typescript": "4.7.3"
92
92
  },
93
- "gitHead": "d43a6a44bd074e077ea131303c0f7a2b0e0ddb75"
93
+ "gitHead": "6e97c358e00e4a7be0c620858c564960c393c1b8"
94
94
  }
@@ -104,6 +104,22 @@ async function newContext(updates: ContextMap, task: any) {
104
104
  return Context.run(context, task)
105
105
  }
106
106
 
107
+ export async function doInAutomationContext(params: {
108
+ appId: string
109
+ automationId: string
110
+ task: any
111
+ }): Promise<any> {
112
+ const tenantId = getTenantIDFromAppID(params.appId)
113
+ return newContext(
114
+ {
115
+ tenantId,
116
+ appId: params.appId,
117
+ automationId: params.automationId,
118
+ },
119
+ params.task
120
+ )
121
+ }
122
+
107
123
  export async function doInContext(appId: string, task: any): Promise<any> {
108
124
  const tenantId = getTenantIDFromAppID(appId)
109
125
  return newContext(
@@ -187,6 +203,11 @@ export function getTenantId(): string {
187
203
  return tenantId
188
204
  }
189
205
 
206
+ export function getAutomationId(): string | undefined {
207
+ const context = Context.get()
208
+ return context?.automationId
209
+ }
210
+
190
211
  export function getAppId(): string | undefined {
191
212
  const context = Context.get()
192
213
  const foundId = context?.appId
@@ -7,4 +7,5 @@ export type ContextMap = {
7
7
  identity?: IdentityContext
8
8
  environmentVariables?: Record<string, string>
9
9
  isScim?: boolean
10
+ automationId?: string
10
11
  }
@@ -39,6 +39,7 @@ if (!env.DISABLE_PINO_LOGGER) {
39
39
  objects?: any[]
40
40
  tenantId?: string
41
41
  appId?: string
42
+ automationId?: string
42
43
  identityId?: string
43
44
  identityType?: IdentityType
44
45
  correlationId?: string
@@ -86,6 +87,7 @@ if (!env.DISABLE_PINO_LOGGER) {
86
87
  contextObject = {
87
88
  tenantId: getTenantId(),
88
89
  appId: getAppId(),
90
+ automationId: getAutomationId(),
89
91
  identityId: identity?._id,
90
92
  identityType: identity?.type,
91
93
  correlationId: correlation.getId(),
@@ -159,6 +161,16 @@ if (!env.DISABLE_PINO_LOGGER) {
159
161
  return appId
160
162
  }
161
163
 
164
+ const getAutomationId = () => {
165
+ let appId
166
+ try {
167
+ appId = context.getAutomationId()
168
+ } catch (e) {
169
+ // do nothing
170
+ }
171
+ return appId
172
+ }
173
+
162
174
  const getIdentity = () => {
163
175
  let identity
164
176
  try {
@@ -128,6 +128,7 @@ class InMemoryQueue {
128
128
 
129
129
  on() {
130
130
  // do nothing
131
+ return this
131
132
  }
132
133
 
133
134
  async waitForCompletion() {
@@ -1,5 +1,6 @@
1
1
  import { Job, JobId, Queue } from "bull"
2
2
  import { JobQueue } from "./constants"
3
+ import * as context from "../context"
3
4
 
4
5
  export type StalledFn = (job: Job) => Promise<void>
5
6
 
@@ -31,77 +32,153 @@ function handleStalled(queue: Queue, removeStalledCb?: StalledFn) {
31
32
  })
32
33
  }
33
34
 
35
+ function getLogParams(
36
+ eventType: QueueEventType,
37
+ event: BullEvent,
38
+ opts: {
39
+ job?: Job
40
+ jobId?: JobId
41
+ error?: Error
42
+ } = {},
43
+ extra: any = {}
44
+ ) {
45
+ const message = `[BULL] ${eventType}=${event}`
46
+ const err = opts.error
47
+
48
+ const data = {
49
+ eventType,
50
+ event,
51
+ job: opts.job,
52
+ jobId: opts.jobId || opts.job?.id,
53
+ ...extra,
54
+ }
55
+
56
+ return [message, err, data]
57
+ }
58
+
59
+ enum BullEvent {
60
+ ERROR = "error",
61
+ WAITING = "waiting",
62
+ ACTIVE = "active",
63
+ STALLED = "stalled",
64
+ PROGRESS = "progress",
65
+ COMPLETED = "completed",
66
+ FAILED = "failed",
67
+ PAUSED = "paused",
68
+ RESUMED = "resumed",
69
+ CLEANED = "cleaned",
70
+ DRAINED = "drained",
71
+ REMOVED = "removed",
72
+ }
73
+
74
+ enum QueueEventType {
75
+ AUTOMATION_EVENT = "automation-event",
76
+ APP_BACKUP_EVENT = "app-backup-event",
77
+ AUDIT_LOG_EVENT = "audit-log-event",
78
+ SYSTEM_EVENT = "system-event",
79
+ }
80
+
81
+ const EventTypeMap: { [key in JobQueue]: QueueEventType } = {
82
+ [JobQueue.AUTOMATION]: QueueEventType.AUTOMATION_EVENT,
83
+ [JobQueue.APP_BACKUP]: QueueEventType.APP_BACKUP_EVENT,
84
+ [JobQueue.AUDIT_LOG]: QueueEventType.AUDIT_LOG_EVENT,
85
+ [JobQueue.SYSTEM_EVENT_QUEUE]: QueueEventType.SYSTEM_EVENT,
86
+ }
87
+
34
88
  function logging(queue: Queue, jobQueue: JobQueue) {
35
- let eventType: string
36
- switch (jobQueue) {
37
- case JobQueue.AUTOMATION:
38
- eventType = "automation-event"
39
- break
40
- case JobQueue.APP_BACKUP:
41
- eventType = "app-backup-event"
42
- break
43
- case JobQueue.AUDIT_LOG:
44
- eventType = "audit-log-event"
45
- break
46
- case JobQueue.SYSTEM_EVENT_QUEUE:
47
- eventType = "system-event"
48
- break
89
+ const eventType = EventTypeMap[jobQueue]
90
+
91
+ function doInJobContext(job: Job, task: any) {
92
+ // if this is an automation job try to get the app id
93
+ const appId = job.data.event?.appId
94
+ if (appId) {
95
+ return context.doInContext(appId, task)
96
+ } else {
97
+ task()
98
+ }
49
99
  }
100
+
101
+ queue
102
+ .on(BullEvent.STALLED, async (job: Job) => {
103
+ // A job has been marked as stalled. This is useful for debugging job
104
+ // workers that crash or pause the event loop.
105
+ await doInJobContext(job, () => {
106
+ console.error(...getLogParams(eventType, BullEvent.STALLED, { job }))
107
+ })
108
+ })
109
+ .on(BullEvent.ERROR, (error: any) => {
110
+ // An error occurred.
111
+ console.error(...getLogParams(eventType, BullEvent.ERROR, { error }))
112
+ })
113
+
50
114
  if (process.env.NODE_DEBUG?.includes("bull")) {
51
115
  queue
52
- .on("error", (error: any) => {
53
- // An error occurred.
54
- console.error(`${eventType}=error error=${JSON.stringify(error)}`)
55
- })
56
- .on("waiting", (jobId: JobId) => {
116
+ .on(BullEvent.WAITING, (jobId: JobId) => {
57
117
  // A Job is waiting to be processed as soon as a worker is idling.
58
- console.log(`${eventType}=waiting jobId=${jobId}`)
118
+ console.info(...getLogParams(eventType, BullEvent.WAITING, { jobId }))
59
119
  })
60
- .on("active", (job: Job, jobPromise: any) => {
120
+ .on(BullEvent.ACTIVE, async (job: Job, jobPromise: any) => {
61
121
  // A job has started. You can use `jobPromise.cancel()`` to abort it.
62
- console.log(`${eventType}=active jobId=${job.id}`)
122
+ await doInJobContext(job, () => {
123
+ console.info(...getLogParams(eventType, BullEvent.ACTIVE, { job }))
124
+ })
63
125
  })
64
- .on("stalled", (job: Job) => {
65
- // A job has been marked as stalled. This is useful for debugging job
66
- // workers that crash or pause the event loop.
67
- console.error(
68
- `${eventType}=stalled jobId=${job.id} job=${JSON.stringify(job)}`
69
- )
126
+ .on(BullEvent.PROGRESS, async (job: Job, progress: any) => {
127
+ // A job's progress was updated
128
+ await doInJobContext(job, () => {
129
+ console.info(
130
+ ...getLogParams(
131
+ eventType,
132
+ BullEvent.PROGRESS,
133
+ { job },
134
+ { progress }
135
+ )
136
+ )
137
+ })
70
138
  })
71
- .on("progress", (job: Job, progress: any) => {
72
- // A job's progress was updated!
73
- console.log(
74
- `${eventType}=progress jobId=${job.id} progress=${progress}`
75
- )
76
- })
77
- .on("completed", (job: Job, result) => {
139
+ .on(BullEvent.COMPLETED, async (job: Job, result) => {
78
140
  // A job successfully completed with a `result`.
79
- console.log(`${eventType}=completed jobId=${job.id} result=${result}`)
141
+ await doInJobContext(job, () => {
142
+ console.info(
143
+ ...getLogParams(eventType, BullEvent.COMPLETED, { job }, { result })
144
+ )
145
+ })
80
146
  })
81
- .on("failed", (job, err: any) => {
147
+ .on(BullEvent.FAILED, async (job: Job, error: any) => {
82
148
  // A job failed with reason `err`!
83
- console.log(`${eventType}=failed jobId=${job.id} error=${err}`)
149
+ await doInJobContext(job, () => {
150
+ console.error(
151
+ ...getLogParams(eventType, BullEvent.FAILED, { job, error })
152
+ )
153
+ })
84
154
  })
85
- .on("paused", () => {
155
+ .on(BullEvent.PAUSED, () => {
86
156
  // The queue has been paused.
87
- console.log(`${eventType}=paused`)
157
+ console.info(...getLogParams(eventType, BullEvent.PAUSED))
88
158
  })
89
- .on("resumed", (job: Job) => {
159
+ .on(BullEvent.RESUMED, () => {
90
160
  // The queue has been resumed.
91
- console.log(`${eventType}=paused jobId=${job.id}`)
161
+ console.info(...getLogParams(eventType, BullEvent.RESUMED))
92
162
  })
93
- .on("cleaned", (jobs: Job[], type: string) => {
163
+ .on(BullEvent.CLEANED, (jobs: Job[], type: string) => {
94
164
  // Old jobs have been cleaned from the queue. `jobs` is an array of cleaned
95
165
  // jobs, and `type` is the type of jobs cleaned.
96
- console.log(`${eventType}=cleaned length=${jobs.length} type=${type}`)
166
+ console.info(
167
+ ...getLogParams(
168
+ eventType,
169
+ BullEvent.CLEANED,
170
+ {},
171
+ { length: jobs.length, type }
172
+ )
173
+ )
97
174
  })
98
- .on("drained", () => {
175
+ .on(BullEvent.DRAINED, () => {
99
176
  // Emitted every time the queue has processed all the waiting jobs (even if there can be some delayed jobs not yet processed)
100
- console.log(`${eventType}=drained`)
177
+ console.info(...getLogParams(eventType, BullEvent.DRAINED))
101
178
  })
102
- .on("removed", (job: Job) => {
179
+ .on(BullEvent.REMOVED, (job: Job) => {
103
180
  // A job successfully removed.
104
- console.log(`${eventType}=removed jobId=${job.id}`)
181
+ console.info(...getLogParams(eventType, BullEvent.REMOVED, { job }))
105
182
  })
106
183
  }
107
184
  }