From d023412e5620c72667830e9a4561a3881dd7aaad Mon Sep 17 00:00:00 2001 From: nameczz Date: Tue, 21 Dec 2021 16:25:34 +0800 Subject: [PATCH] hide system view and fix cron job Signed-off-by: nameczz --- client/src/plugins/system/config.json | 9 +---- .../src/__tests__/crons/crons.service.test.ts | 5 ++- express/src/crons/crons.controller.ts | 20 +++++++---- express/src/crons/crons.service.ts | 36 ++++++++++++------- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/client/src/plugins/system/config.json b/client/src/plugins/system/config.json index 48720780..fb48d2f8 100644 --- a/client/src/plugins/system/config.json +++ b/client/src/plugins/system/config.json @@ -1,11 +1,4 @@ { "name": "system-view", - "version": "0.1.0", - "client": { - "path": "system", - "auth": true, - "entry": "SystemView.tsx", - "label": "System View", - "iconName": "navSystem" - } + "version": "0.1.0" } \ No newline at end of file diff --git a/express/src/__tests__/crons/crons.service.test.ts b/express/src/__tests__/crons/crons.service.test.ts index b7fc7c24..39bccd44 100644 --- a/express/src/__tests__/crons/crons.service.test.ts +++ b/express/src/__tests__/crons/crons.service.test.ts @@ -158,7 +158,10 @@ describe('test crons service', () => { newSchedulerRegistry ); - await newCronsService.getCollections(WS_EVENTS.COLLECTION); + await newCronsService.getCollections( + WS_EVENTS.COLLECTION, + '127.0.0.1:19530' + ); expect(schedule).toBeCalledWith(mockCronEverySec, expect.any(Function)); expect(handleEndTask).toBeCalled(); }); diff --git a/express/src/crons/crons.controller.ts b/express/src/crons/crons.controller.ts index 75449d00..b1084049 100644 --- a/express/src/crons/crons.controller.ts +++ b/express/src/crons/crons.controller.ts @@ -1,8 +1,9 @@ -import { NextFunction, Request, Response, Router } from "express"; -import { dtoValidationMiddleware } from "../middlewares/validation"; -import { CronsService, SchedulerRegistry } from "./crons.service"; -import { collectionsService } from "../collections"; -import { ToggleCronJobByNameDto } from "./dto"; +import { NextFunction, Request, Response, Router } from 'express'; +import { dtoValidationMiddleware } from '../middlewares/validation'; +import { CronsService, SchedulerRegistry } from './crons.service'; +import { collectionsService } from '../collections'; +import { ToggleCronJobByNameDto } from './dto'; +import { MILVUS_ADDRESS } from '../utils/Const'; export class CronsController { private router: Router; @@ -20,7 +21,7 @@ export class CronsController { generateRoutes() { this.router.put( - "/", + '/', dtoValidationMiddleware(ToggleCronJobByNameDto), this.toggleCronJobByName.bind(this) ); @@ -30,8 +31,13 @@ export class CronsController { async toggleCronJobByName(req: Request, res: Response, next: NextFunction) { const cronData = req.body; + const milvusAddress = (req.headers[MILVUS_ADDRESS] as string) || ''; + console.log(cronData, milvusAddress); try { - const result = await this.cronsService.toggleCronJobByName(cronData); + const result = await this.cronsService.toggleCronJobByName({ + ...cronData, + address: milvusAddress, + }); res.send(result); } catch (error) { next(error); diff --git a/express/src/crons/crons.service.ts b/express/src/crons/crons.service.ts index 65235bdb..6f0f2682 100644 --- a/express/src/crons/crons.service.ts +++ b/express/src/crons/crons.service.ts @@ -9,14 +9,18 @@ export class CronsService { private schedulerRegistry: SchedulerRegistry ) {} - async toggleCronJobByName(data: { name: string; type: WS_EVENTS_TYPE }) { - const { name, type } = data; + async toggleCronJobByName(data: { + name: string; + type: WS_EVENTS_TYPE; + address: string; + }) { + const { name, type, address } = data; switch (name) { case WS_EVENTS.COLLECTION: - const cronJobEntity = this.schedulerRegistry.getCronJob(name); + const cronJobEntity = this.schedulerRegistry.getCronJob(name, address); if (!cronJobEntity && Number(type) === WS_EVENTS_TYPE.START) { - return this.getCollections(WS_EVENTS.COLLECTION); + return this.getCollections(WS_EVENTS.COLLECTION, address); } if (!cronJobEntity) { return; @@ -29,7 +33,7 @@ export class CronsService { } } - async getCollections(name: string) { + async getCollections(name: string, address: string) { const task = async () => { try { const res = await this.collectionService.getAllCollections(); @@ -42,7 +46,7 @@ export class CronsService { return res; } catch (error) { // When user not connect milvus, stop cron - const cronJobEntity = this.schedulerRegistry.getCronJob(name); + const cronJobEntity = this.schedulerRegistry.getCronJob(name, address); if (cronJobEntity) { cronJobEntity.stop(); } @@ -50,21 +54,23 @@ export class CronsService { throw new Error(error); } }; - this.schedulerRegistry.setCronJobEverySecond(name, task); + this.schedulerRegistry.setCronJobEverySecond(name, task, address); } } export class SchedulerRegistry { constructor(private cronJobList: CronJob[]) {} - getCronJob(name: string) { - const target = this.cronJobList.find((item) => item.name === name); + getCronJob(name: string, address: string) { + const target = this.cronJobList.find( + item => item.name === name && item.address === address + ); return target?.entity; } - setCronJobEverySecond(name: string, func: () => {}) { + setCronJobEverySecond(name: string, func: () => {}, address: string) { // The cron job will run every second - this.setCronJob(name, '* * * * * *', func); + this.setCronJob(name, '* * * * * *', func, address); } // ┌────────────── second (optional) @@ -77,8 +83,10 @@ export class SchedulerRegistry { // │ │ │ │ │ │ // * * * * * * // https://www.npmjs.com/package/node-cron - setCronJob(name: string, scheduler: string, func: () => {}) { - const target = this.cronJobList.find((item) => item.name === name); + setCronJob(name: string, scheduler: string, func: () => {}, address: string) { + const target = this.cronJobList.find( + item => item.name === name && item.address === address + ); if (target) { target?.entity?.stop(); } else { @@ -89,6 +97,7 @@ export class SchedulerRegistry { this.cronJobList.push({ name, entity: task, + address, }); } } @@ -97,4 +106,5 @@ export class SchedulerRegistry { interface CronJob { name: string; entity: ScheduledTask; + address: string; // milvus address }