Skip to content

Commit

Permalink
refactor: bull-board
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Jan 11, 2025
1 parent b343fd2 commit 793ab85
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 28 deletions.
6 changes: 3 additions & 3 deletions packages/bull-board/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
"@midwayjs/core": "^3.19.0",
"@midwayjs/express": "^3.19.2",
"@midwayjs/koa": "^3.19.2",
"@midwayjs/mock": "^3.19.2"
"@midwayjs/mock": "^3.19.2",
"@midwayjs/bull": "^3.19.3",
"@midwayjs/bullmq": "^0.0.1"
},
"dependencies": {
"@bull-board/api": "5.23.0",
"@bull-board/ui": "5.23.0",
"@midwayjs/bull": "^3.19.3",
"@midwayjs/bullmq": "^1.0.0",
"ejs": "3.1.10"
},
"engines": {
Expand Down
34 changes: 30 additions & 4 deletions packages/bull-board/src/board.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import {
Scope,
ScopeEnum,
MidwayFrameworkType,
ApplicationContext,
IMidwayContainer,
MidwayFrameworkService,
} from '@midwayjs/core';
import { extname } from 'path';
import * as bull from '@midwayjs/bull';
import { createBullBoard } from '@bull-board/api';
import { BullAdapter } from '@bull-board/api/bullAdapter';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
import { MidwayAdapter } from './adapter';
import { BullBoardOption } from './interface';
import { BullBoardManager } from './board.manager';
import type { Framework as BullFramework } from '@midwayjs/bull';
import type { Framework as BullMQFramework } from '@midwayjs/bullmq';

const MIME_MAP = {
'.html': 'text/html',
Expand All @@ -44,21 +49,42 @@ export class BoardMiddleware
implements IMiddleware<IMidwayContext, NextFunction, unknown>
{
@Inject()
protected framework: bull.Framework;
protected frameworkService: MidwayFrameworkService;

@Config('bullBoard')
protected bullBoardConfig: BullBoardOption;

@Inject()
protected bullBoardManager: BullBoardManager;

@ApplicationContext()
protected applicationContext: IMidwayContainer;

private basePath: string;
private serverAdapter: MidwayAdapter;

@Init()
protected async init() {
const queueList = this.framework.getQueueList();
const wrapQueues = queueList.map(queue => new BullAdapter(queue));
let framework: BullFramework | BullMQFramework =
this.frameworkService.getFramework('bull') as BullFramework;
if (!framework) {
framework = this.frameworkService.getFramework(
'bullmq'
) as BullMQFramework;
}

if (!framework) {
return;
}

const queueList = framework.getQueueList();
const wrapQueues = queueList.map(queue => {
if (this.applicationContext.hasNamespace('bull')) {
return new BullAdapter(queue);
} else if (this.applicationContext.hasNamespace('bullmq')) {
return new BullMQAdapter(queue);
}
});
this.basePath = this.bullBoardConfig.basePath;

this.serverAdapter = new MidwayAdapter();
Expand Down
17 changes: 6 additions & 11 deletions packages/bull-board/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import * as bull from '@midwayjs/bull';
import * as bullmq from '@midwayjs/bullmq';
import {
Configuration,
IMidwayContainer,
Inject,
MidwayApplicationManager,
MidwayConfigService,
} from '@midwayjs/core';
import { BoardMiddleware } from './board.middleware';
import { BullMQBoardMiddleware } from './bullmq.board.middleware';

@Configuration({
namespace: 'bull-board',
imports: [bull, bullmq],
importConfigs: [
{
default: {
bullBoard: {
package: 'bull',
basePath: '/ui',
uiConfig: {},
adapterOptions: {
Expand All @@ -34,20 +30,19 @@ export class BullBoardConfiguration {
@Inject()
configService: MidwayConfigService;

async onReady() {
const queuePackage =
this.configService.getConfiguration('bullBoard.package');
async onReady(container: IMidwayContainer) {
const apps = this.applicationManager.getApplications([
'express',
'egg',
'koa',
]);
if (apps.length) {
apps.forEach(app => {
if (queuePackage === 'bull') {
if (
container.hasNamespace('bull') ||
container.hasNamespace('bullmq')
) {
app.useMiddleware(BoardMiddleware);
} else if (queuePackage === 'bullmq') {
app.useMiddleware(BullMQBoardMiddleware);
}
});
}
Expand Down
3 changes: 1 addition & 2 deletions packages/bull-board/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { QueueAdapterOptions, UIConfig } from "@bull-board/api/dist/typings/app";

export interface BullBoardOption {
package?: 'bull' | 'bullmq';
basePath?: string;
uiConfig?: UIConfig;
adapterOptions?: QueueAdapterOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ import * as koa from '@midwayjs/koa';
default: {
keys: 123,
bullmq: {
connection: {
defaultConnection: {
host: '127.0.0.1',
port: 6379,
}
},
bullBoard: {
package: 'bullmq'
},
},
},
],
Expand Down
6 changes: 5 additions & 1 deletion packages/bullmq/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
} from '@midwayjs/core';
import * as DefaultConfig from './config/config.default';
import { BullMQFramework } from './framework';
import { BULLMQ_FLOW_PRODUCER_KEY, BULLMQ_QUEUE_KEY, BULLMQ_WORKER_KEY } from './constants';
import {
BULLMQ_FLOW_PRODUCER_KEY,
BULLMQ_QUEUE_KEY,
BULLMQ_WORKER_KEY,
} from './constants';

@Configuration({
namespace: 'bullmq',
Expand Down
2 changes: 1 addition & 1 deletion packages/bullmq/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
export const BULLMQ_QUEUE_KEY = 'bullmq:queue';
export const BULLMQ_PROCESSOR_KEY = 'bullmq:processor';
export const BULLMQ_WORKER_KEY = 'bullmq:worker';
export const BULLMQ_FLOW_PRODUCER_KEY = 'bullmq:flow-producer';
export const BULLMQ_FLOW_PRODUCER_KEY = 'bullmq:flow-producer';
9 changes: 7 additions & 2 deletions packages/bullmq/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ export class BullMQFramework extends BaseFramework<Application, Context, any> {
defaultJobOptions: otherOptions,
});
if (!currentQueue) {
throw new MidwayCommonError(`[midway:bullmq] Queue ${options.queueName} not found`);
throw new MidwayCommonError(
`[midway:bullmq] Queue ${options.queueName} not found`
);
}
// clear old repeat job when start
if (this.clearRepeatJobWhenStart) {
Expand Down Expand Up @@ -218,7 +220,10 @@ export class BullMQFramework extends BaseFramework<Application, Context, any> {
/**
* Ensure a queue by name and queueOptions
*/
protected ensureQueue(name: string, queueOptions: Partial<QueueOptions> = {}) {
protected ensureQueue(
name: string,
queueOptions: Partial<QueueOptions> = {}
) {
if (!this.queueMap.has(name)) {
this.createQueue(name, queueOptions);
}
Expand Down

0 comments on commit 793ab85

Please sign in to comment.