feat: integrate BullMQ for query processing and add QueueModule
This commit is contained in:
@ -45,7 +45,7 @@ export class CommandController {
|
||||
@Headers() headers: Record<string, any>,
|
||||
@Res() res: Response
|
||||
) {
|
||||
const queryResult = await this.queryExecuterService.runQuery(
|
||||
const queryResult = await this.queryExecuterService.runQueryQueued(
|
||||
token,
|
||||
query,
|
||||
headers
|
||||
|
||||
@ -10,14 +10,22 @@ import {
|
||||
registeredPlugins,
|
||||
} from "src/vm/vm.constants";
|
||||
import { DatabaseManagerService } from "src/databaseManager/database/database.manager.service";
|
||||
import { InjectQueue } from "@nestjs/bullmq";
|
||||
import { QUEUE_NAMES } from "src/queue/constants";
|
||||
import { Queue, QueueEvents } from "bullmq";
|
||||
|
||||
@Injectable()
|
||||
export class QueryExecuterService {
|
||||
private queueEvents: QueueEvents;
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Query)
|
||||
readonly queryRepository: Repository<Query>,
|
||||
readonly databaseManagerService: DatabaseManagerService
|
||||
) {}
|
||||
readonly databaseManagerService: DatabaseManagerService,
|
||||
@InjectQueue(QUEUE_NAMES.QUERY) private queryQueue: Queue
|
||||
) {
|
||||
this.queueEvents = new QueueEvents(this.queryQueue.name);
|
||||
}
|
||||
|
||||
parseImports(source: string): string[] {
|
||||
const importRegex =
|
||||
@ -38,6 +46,21 @@ export class QueryExecuterService {
|
||||
.trim();
|
||||
}
|
||||
|
||||
async runQueryQueued(
|
||||
token: string,
|
||||
queryData: any,
|
||||
headers: Record<string, any> = {}
|
||||
): Promise<QueryResponse> {
|
||||
const job = await this.queryQueue.add(`${new Date().getTime()}_${token}`, {
|
||||
token,
|
||||
queryData,
|
||||
headers,
|
||||
});
|
||||
|
||||
const result = await job.waitUntilFinished(this.queueEvents);
|
||||
return result;
|
||||
}
|
||||
|
||||
async runQuery(
|
||||
token: string,
|
||||
queryData: any,
|
||||
@ -120,4 +143,8 @@ export class QueryExecuterService {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.queueEvents.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ export class QueryController {
|
||||
@Headers() headers: Record<string, any>,
|
||||
@Res() res: Response
|
||||
) {
|
||||
const queryResult = await this.queryExecuterService.runQuery(
|
||||
const queryResult = await this.queryExecuterService.runQueryQueued(
|
||||
token,
|
||||
query,
|
||||
headers
|
||||
|
||||
Reference in New Issue
Block a user