Skip to content

Commit

Permalink
fix(be): 🐛 bull board and serverless compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
lehuygiang28 committed Jul 2, 2024
1 parent 357b40e commit d706af5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
20 changes: 12 additions & 8 deletions apps/be/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ export class AppModule {
private readonly redisService: RedisService,
) {}
configure(consumer: MiddlewareConsumer) {
consumer
.apply(
jobQueueUIMiddleware(
this.redisService.getClient,
this.configService.getOrThrow('app.globalPrefix', { infer: true }),
),
)
.forRoutes('admin/queues');
const bullBoardPath = this.configService.getOrThrow('app.bullBoardPath', { infer: true });

if (bullBoardPath) {
const prefix = this.configService.getOrThrow('app.globalPrefix', { infer: true })
? '/' + this.configService.getOrThrow('app.globalPrefix', { infer: true })
: '';
consumer
.apply(
jobQueueUIMiddleware(this.redisService.getClient, `${prefix}/${bullBoardPath}`),
)
.forRoutes(bullBoardPath);
}
}
}
11 changes: 10 additions & 1 deletion apps/be/src/app/config/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class EnvironmentVariablesValidator {
@ValidateIf((o: EnvironmentVariablesValidator) => !!o?.API_STATS_PATH)
@IsString()
API_STATS_PASSWORD: string;

@IsOptional()
@IsString()
BULL_BOARD_PATH: string;
}

export default registerAs<AppConfig>('app', () => {
Expand All @@ -66,8 +70,13 @@ export default registerAs<AppConfig>('app', () => {
: 100,
port: process.env?.PORT ? parseInt(process.env?.PORT, 10) : 8000,
fallbackLanguage: process.env?.FALLBACK_LANGUAGE || 'en',
apiStatsPath: process.env?.API_STATS_PATH,
apiStatsPath: process.env?.API_STATS_PATH
? process.env.API_STATS_PATH.replace(/^\/|\\|\/$|\\$/g, '')
: '',
apiStatsUsername: process.env?.API_STATS_USERNAME,
apiStatsPassword: process.env?.API_STATS_PASSWORD,
bullBoardPath: process.env?.BULL_BOARD_PATH
? process.env.BULL_BOARD_PATH.replace(/^\/|\\|\/$|\\$/g, '')
: '',
};
});
1 change: 1 addition & 0 deletions apps/be/src/app/config/app-config.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type AppConfig = {
apiStatsPath: string;
apiStatsUsername: string;
apiStatsPassword: string;
bullBoardPath: string;
};
7 changes: 5 additions & 2 deletions apps/be/src/app/middlewares/job-queues.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {
BULLMQ_BG_JOB_QUEUE,
} from '~be/common/bullmq/bullmq.constant';

export function jobQueueUIMiddleware(connection: Redis, globalPrefix = '') {
export function jobQueueUIMiddleware(connection: Redis, basePath = '') {
const serverAdapter = new ExpressAdapter();
const basePath = `${globalPrefix ? '/' + globalPrefix : ''}/admin/queues`;
serverAdapter.setBasePath(basePath);

const queues = [
Expand All @@ -35,6 +34,10 @@ export function jobQueueUIMiddleware(connection: Redis, globalPrefix = '') {
createBullBoard({
queues,
serverAdapter,
options: {
uiBasePath: require.resolve(`@bull-board/ui/package.json`).replace('package.json', ''),
uiConfig: {},
},
});

return serverAdapter.getRouter();
Expand Down

0 comments on commit d706af5

Please sign in to comment.