2021-11-17 17:30:06 +01:00
|
|
|
const { createBullBoard } = require("@bull-board/api")
|
|
|
|
const { BullAdapter } = require("@bull-board/api/bullAdapter")
|
|
|
|
const { KoaAdapter } = require("@bull-board/koa")
|
2021-09-08 20:29:28 +02:00
|
|
|
const env = require("../environment")
|
|
|
|
const Queue = env.isTest()
|
|
|
|
? require("../utilities/queue/inMemoryQueue")
|
|
|
|
: require("bull")
|
|
|
|
const { JobQueues } = require("../constants")
|
2022-01-10 20:33:00 +01:00
|
|
|
const { utils } = require("@budibase/backend-core/redis")
|
2021-09-20 11:33:18 +02:00
|
|
|
const { opts, redisProtocolUrl } = utils.getRedisOptions()
|
2022-08-20 00:14:19 +02:00
|
|
|
const listeners = require("./listeners")
|
2021-09-08 20:29:28 +02:00
|
|
|
|
2021-11-16 19:58:24 +01:00
|
|
|
const CLEANUP_PERIOD_MS = 60 * 1000
|
|
|
|
const queueConfig = redisProtocolUrl || { redis: opts }
|
|
|
|
let cleanupInternal = null
|
|
|
|
|
|
|
|
let automationQueue = new Queue(JobQueues.AUTOMATIONS, queueConfig)
|
2022-08-20 00:14:19 +02:00
|
|
|
listeners.addListeners(automationQueue)
|
2021-11-16 19:58:24 +01:00
|
|
|
|
|
|
|
async function cleanup() {
|
|
|
|
await automationQueue.clean(CLEANUP_PERIOD_MS, "completed")
|
|
|
|
}
|
2021-05-07 13:24:51 +02:00
|
|
|
|
2021-11-17 17:30:06 +01:00
|
|
|
const PATH_PREFIX = "/bulladmin"
|
2021-05-07 13:24:51 +02:00
|
|
|
|
|
|
|
exports.init = () => {
|
2021-11-16 19:58:24 +01:00
|
|
|
// cleanup the events every 5 minutes
|
|
|
|
if (!cleanupInternal) {
|
|
|
|
cleanupInternal = setInterval(cleanup, CLEANUP_PERIOD_MS)
|
|
|
|
// fire off an initial cleanup
|
|
|
|
cleanup().catch(err => {
|
|
|
|
console.error(`Unable to cleanup automation queue initially - ${err}`)
|
|
|
|
})
|
|
|
|
}
|
2021-05-07 13:24:51 +02:00
|
|
|
// Set up queues for bull board admin
|
2021-09-08 20:29:28 +02:00
|
|
|
const queues = [automationQueue]
|
2021-05-07 13:24:51 +02:00
|
|
|
const adapters = []
|
2021-11-17 17:30:06 +01:00
|
|
|
const serverAdapter = new KoaAdapter()
|
2021-05-07 13:24:51 +02:00
|
|
|
for (let queue of queues) {
|
|
|
|
adapters.push(new BullAdapter(queue))
|
|
|
|
}
|
2021-11-17 17:30:06 +01:00
|
|
|
createBullBoard({
|
|
|
|
queues: adapters,
|
|
|
|
serverAdapter,
|
|
|
|
})
|
|
|
|
serverAdapter.setBasePath(PATH_PREFIX)
|
|
|
|
return serverAdapter.registerPlugin()
|
2021-05-07 14:55:57 +02:00
|
|
|
}
|
2021-09-08 20:29:28 +02:00
|
|
|
|
2022-05-30 22:22:06 +02:00
|
|
|
exports.shutdown = async () => {
|
|
|
|
if (automationQueue) {
|
|
|
|
clearInterval(cleanupInternal)
|
|
|
|
await automationQueue.close()
|
|
|
|
automationQueue = null
|
|
|
|
}
|
2022-08-31 11:47:41 +02:00
|
|
|
console.log("Bull shutdown")
|
2022-05-30 22:22:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 20:29:28 +02:00
|
|
|
exports.queue = automationQueue
|