Skip to content

Commit

Permalink
#3667 - Bull Scheduler Increments the Next Scheduled Job When Manuall…
Browse files Browse the repository at this point in the history
…y Promoted (#3957)

Updated Bull Board-related packages to take advantage of the feature
present in the newer version that allows a new job to be added (same as
duplicate), allowing the schedulers to be executed without affecting
their current time to be executed again.

_Notes:_
1. during the local tests, when a delayed job was deleted and the
`queue-consumers` restarted, the delayed job was restored.
2. this PR is intended to be part of the upcoming release. The research
for the ticket will continue.

## Using the add/duplicate option

While creating the new job the properties can be edited, for instance,
the corn expression can be edited to create a new time to execute the
scheduler.

_Please note that `timestamp` and `prevMillis` should be removed. These
properties are generated once the job is created.
As mentioned in the source code, `timestamp` is the "Timestamp when the
job was created." and `prevMillis` is a "Internal property used by
repeatable jobs."_


![image](https://github.com/user-attachments/assets/05245952-5bda-48db-a3b6-61e3a2317669)

```json
{
  "repeat": {
    "count": 1,
    "key": "__default__::::0 7 * * *",
    "cron": "0 7 * * *"
  },
  "jobId": "repeat:2c2720c5e8b4e9ce99993becec27a0ff:1731999600000",
  "delay": 42485905,
  "timestamp": 1731957114095,
  "prevMillis": 1731999600000,
  "attempts": 3,
  "backoff": {
    "type": "fixed",
    "delay": 180000
  }
}
```

## Refactor

- Refactored the code to move the Bull Board configuration to the Nestjs
modules instead of doing it on the main.ts.
The refactored code is equivalent to the one previously on the
`main.ts`.
- Updated icons and labels to make the dashboard look more like part of
the SIMS. The way it was done was by targeting less effort. In case it
causes some noise during the PR review the code will be removed.
  • Loading branch information
andrewsignori-aot authored Nov 19, 2024
1 parent 06eb979 commit 7b8df04
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Inject, LoggerService, Module, OnModuleInit } from "@nestjs/common";
import { ModuleRef } from "@nestjs/core";
import { BULL_BOARD_INSTANCE, BullBoardInstance } from "@bull-board/nestjs";
import { QueueService } from "@sims/services/queue/queue.service";
import { InjectLogger } from "@sims/utilities/logger";
import { getQueueToken } from "@nestjs/bull";
import { Queue } from "bull";
import { BullAdapter } from "@bull-board/api/bullAdapter";

@Module({})
export class BullBoardQueuesRegistrationModule implements OnModuleInit {
constructor(
private readonly moduleRef: ModuleRef,
private readonly queueService: QueueService,
@Inject(BULL_BOARD_INSTANCE)
private readonly board: BullBoardInstance,
) {}

/**
* Adds all queues to the bull board during application initialization
* checking if the queue is active and if it is a scheduler.
*/
async onModuleInit(): Promise<void> {
const queues = await this.queueService.queueConfigurationModel();
queues.forEach((queue) => {
if (!queue.isActive && queue.isScheduler) {
this.logger.log(`Queue service '${queue.name}' is inactive.`);
return;
}
const queueProvider = this.moduleRef.get<Queue>(
getQueueToken(queue.name),
{
strict: false,
},
);
const queueAdapter = new BullAdapter(queueProvider, {
readOnlyMode: queue.dashboardReadonly,
});
this.board.addQueue(queueAdapter);
});
}

@InjectLogger()
logger: LoggerService;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Module } from "@nestjs/common";
import { BullBoardModule, BullBoardModuleOptions } from "@bull-board/nestjs";
import { ConfigModule, ConfigService } from "@sims/utilities/config";
import * as basicAuth from "express-basic-auth";
import { ExpressAdapter } from "@bull-board/express";
import { BULL_BOARD_ROUTE } from "../constants";
import { BullBoardQueuesRegistrationModule } from "./bull-board-queues-registration.module";

/**
* Bull board related modules to allow the dashboard to be registered.
*/
@Module({
imports: [
BullBoardModule.forRootAsync({
imports: [ConfigModule],
useFactory: bullBoardModuleFactory,
inject: [ConfigService],
}),
BullBoardQueuesRegistrationModule,
],
exports: [BullBoardModule, BullBoardQueuesRegistrationModule],
})
export class BullBoardQueuesModule {}

/**
* Builds the Bull Board module options to register the dashboard in a dynamic way.
* @param configService service with the configuration of the application.
* @returns Bull Board module options with the dashboard route,
* authentication middleware and the board options.
*/
async function bullBoardModuleFactory(
configService: ConfigService,
): Promise<BullBoardModuleOptions> {
const queueDashboardUsers = {};
queueDashboardUsers[configService.queueDashboardCredential.userName] =
configService.queueDashboardCredential.password;
const authMiddleware = basicAuth({
users: queueDashboardUsers,
challenge: true,
});
return {
route: BULL_BOARD_ROUTE,
adapter: ExpressAdapter,
middleware: authMiddleware,
boardOptions: {
uiConfig: {
boardTitle: "SIMS-Queues",
boardLogo: {
path: "https://sims.studentaidbc.ca/favicon-32x32.png",
},
favIcon: {
default: "https://sims.studentaidbc.ca/favicon-16x16.png",
alternative: "https://sims.studentaidbc.ca/favicon-32x32.png",
},
},
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./error-code.constants";
export * from "./system-configurations.constants";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Bull Dashboard route.
*/
export const BULL_BOARD_ROUTE = "admin/queues";
41 changes: 0 additions & 41 deletions sources/packages/backend/apps/queue-consumers/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import "../../../env-setup";
import { QueueService } from "@sims/services/queue";
import { ConfigService } from "@sims/utilities/config";
import { createBullBoard } from "@bull-board/api";
import { BullAdapter } from "@bull-board/api/bullAdapter";
import { ExpressAdapter } from "@bull-board/express";
import { NestFactory } from "@nestjs/core";
import { Queue } from "bull";
import { QueueConsumersModule } from "./queue-consumers.module";
import * as basicAuth from "express-basic-auth";
import { LoggerService } from "@sims/utilities/logger";
import { SystemUsersService } from "@sims/services";

Expand All @@ -23,40 +17,5 @@ import { SystemUsersService } from "@sims/services";
const systemUsersService = app.get(SystemUsersService);
await systemUsersService.loadSystemUser();

// Queue service.
const queueService = app.get<QueueService>(QueueService);
const queues = await queueService.queueConfigurationModel();
// Create bull board UI dashboard for queue management.
const serverAdapter = new ExpressAdapter();
serverAdapter.setBasePath("/admin/queues");
const bullBoardQueues: BullAdapter[] = [];
queues.forEach((queue) => {
if (!queue.isActive && queue.isScheduler) {
logger.log(`Queue service "${queue.name}" is inactive.`);
} else {
bullBoardQueues.push(
new BullAdapter(app.get<Queue>(`BullQueue_${queue.name}`), {
readOnlyMode: queue.dashboardReadonly,
}),
);
}
});
createBullBoard({
queues: bullBoardQueues,
serverAdapter,
});
// Bull board user for basic authentication.
const queueDashboardUsers = {};
queueDashboardUsers[config.queueDashboardCredential.userName] =
config.queueDashboardCredential.password;
app.use(
"/admin/queues",
basicAuth({
users: queueDashboardUsers,
challenge: true,
}),
serverAdapter.getRouter(),
);

await app.listen(config.queueConsumersPort);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ import { CASSupplierIntegrationService } from "./services/cas-supplier/cas-suppl
import { VirusScanProcessor } from "./processors/virus-scan/virus-scan.processor";
import { CASService } from "@sims/integrations/cas/cas.service";
import { ObjectStorageService } from "@sims/integrations/object-storage";
import { BullBoardQueuesModule } from "./bull-board/bull-board-queues.module";

// TODO: Removed ATBCResponseIntegrationScheduler in providers, the queuename from enum and the decorators of the processor as part of #2539.
@Module({
imports: [
GlobalHttpModule,
DatabaseModule,
QueueModule,
BullBoardQueuesModule,
ZeebeModule.forRoot(),
IER12IntegrationModule,
ECEIntegrationModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { INestApplication } from "@nestjs/common";
import { Test, TestingModule } from "@nestjs/testing";
import { DataSource } from "typeorm";
import { QueueConsumersModule } from "../../../src/queue-consumers.module";
import { BullBoardQueuesModule } from "../../../src/bull-board/bull-board-queues.module";
import { SshService } from "@sims/integrations/services";
import { overrideImportsMetadata } from "@sims/test-utils";
import {
QueueModuleMock,
createObjectStorageServiceMock,
createSSHServiceMock,
createZeebeModuleMock,
BullBoardQueuesModuleMock,
} from "@sims/test-utils/mocks";
import * as Client from "ssh2-sftp-client";
import { DeepMocked, createMock } from "@golevelup/ts-jest";
Expand Down Expand Up @@ -45,6 +47,10 @@ export async function createTestingAppModule(): Promise<CreateTestingModuleResul
replace: QueueModule,
by: QueueModuleMock,
},
{
replace: BullBoardQueuesModule,
by: BullBoardQueuesModuleMock,
},
{
replace: ZeebeModule,
by: createZeebeModuleMock(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Global, Module } from "@nestjs/common";

/**
* Mock to entirely replace the Bull Board queues module
* that are not part of the E2E tests.
*/
@Global()
@Module({})
export class BullBoardQueuesModuleMock {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./ssh-service-mock";
export * from "./queue-module-mock";
export * from "./zeebe-client-mock";
export * from "./object-storage-service-mock";
export * from "./bull-board-queues-module-mock";
129 changes: 111 additions & 18 deletions sources/packages/backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions sources/packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@
},
"dependencies": {
"@aws-sdk/client-s3": "^3.663.0",
"@bull-board/api": "^5.15.1",
"@bull-board/express": "^5.15.1",
"@bull-board/ui": "^5.15.1",
"@bull-board/api": "^6.5.2",
"@bull-board/express": "^6.5.2",
"@bull-board/nestjs": "^6.5.2",
"@bull-board/ui": "^6.5.2",
"@camunda8/sdk": "^8.6.13",
"@golevelup/nestjs-discovery": "^4.0.0",
"@nestjs/axios": "^3.0.2",
Expand Down

0 comments on commit 7b8df04

Please sign in to comment.