@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/dist/package.json +3 -3
- package/dist/src/context/mainContext.d.ts +6 -0
- package/dist/src/context/mainContext.js +17 -1
- package/dist/src/context/mainContext.js.map +1 -1
- package/dist/src/context/types.d.ts +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/logging/pino/logger.js +11 -0
- package/dist/src/logging/pino/logger.js.map +1 -1
- package/dist/src/queue/inMemoryQueue.d.ts +1 -1
- package/dist/src/queue/inMemoryQueue.js +1 -0
- package/dist/src/queue/inMemoryQueue.js.map +1 -1
- package/dist/src/queue/listeners.js +116 -48
- package/dist/src/queue/listeners.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/context/mainContext.ts +21 -0
- package/src/context/types.ts +1 -0
- package/src/logging/pino/logger.ts +12 -0
- package/src/queue/inMemoryQueue.ts +1 -0
- package/src/queue/listeners.ts +125 -48
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@budibase/backend-core",
|
|
3
|
-
"version": "2.6.
|
|
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.
|
|
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": "
|
|
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
|
package/src/context/types.ts
CHANGED
|
@@ -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 {
|
package/src/queue/listeners.ts
CHANGED
|
@@ -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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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(
|
|
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.
|
|
118
|
+
console.info(...getLogParams(eventType, BullEvent.WAITING, { jobId }))
|
|
59
119
|
})
|
|
60
|
-
.on(
|
|
120
|
+
.on(BullEvent.ACTIVE, async (job: Job, jobPromise: any) => {
|
|
61
121
|
// A job has started. You can use `jobPromise.cancel()`` to abort it.
|
|
62
|
-
|
|
122
|
+
await doInJobContext(job, () => {
|
|
123
|
+
console.info(...getLogParams(eventType, BullEvent.ACTIVE, { job }))
|
|
124
|
+
})
|
|
63
125
|
})
|
|
64
|
-
.on(
|
|
65
|
-
// A job
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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(
|
|
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
|
-
|
|
141
|
+
await doInJobContext(job, () => {
|
|
142
|
+
console.info(
|
|
143
|
+
...getLogParams(eventType, BullEvent.COMPLETED, { job }, { result })
|
|
144
|
+
)
|
|
145
|
+
})
|
|
80
146
|
})
|
|
81
|
-
.on(
|
|
147
|
+
.on(BullEvent.FAILED, async (job: Job, error: any) => {
|
|
82
148
|
// A job failed with reason `err`!
|
|
83
|
-
|
|
149
|
+
await doInJobContext(job, () => {
|
|
150
|
+
console.error(
|
|
151
|
+
...getLogParams(eventType, BullEvent.FAILED, { job, error })
|
|
152
|
+
)
|
|
153
|
+
})
|
|
84
154
|
})
|
|
85
|
-
.on(
|
|
155
|
+
.on(BullEvent.PAUSED, () => {
|
|
86
156
|
// The queue has been paused.
|
|
87
|
-
console.
|
|
157
|
+
console.info(...getLogParams(eventType, BullEvent.PAUSED))
|
|
88
158
|
})
|
|
89
|
-
.on(
|
|
159
|
+
.on(BullEvent.RESUMED, () => {
|
|
90
160
|
// The queue has been resumed.
|
|
91
|
-
console.
|
|
161
|
+
console.info(...getLogParams(eventType, BullEvent.RESUMED))
|
|
92
162
|
})
|
|
93
|
-
.on(
|
|
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.
|
|
166
|
+
console.info(
|
|
167
|
+
...getLogParams(
|
|
168
|
+
eventType,
|
|
169
|
+
BullEvent.CLEANED,
|
|
170
|
+
{},
|
|
171
|
+
{ length: jobs.length, type }
|
|
172
|
+
)
|
|
173
|
+
)
|
|
97
174
|
})
|
|
98
|
-
.on(
|
|
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.
|
|
177
|
+
console.info(...getLogParams(eventType, BullEvent.DRAINED))
|
|
101
178
|
})
|
|
102
|
-
.on(
|
|
179
|
+
.on(BullEvent.REMOVED, (job: Job) => {
|
|
103
180
|
// A job successfully removed.
|
|
104
|
-
console.
|
|
181
|
+
console.info(...getLogParams(eventType, BullEvent.REMOVED, { job }))
|
|
105
182
|
})
|
|
106
183
|
}
|
|
107
184
|
}
|