Browse Source

Merge pull request #8 from nameczz/nameczz

hide system view and fix cron job
ryjiang 3 năm trước cách đây
mục cha
commit
713b771b13

+ 1 - 8
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"
 }

+ 4 - 1
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();
   });

+ 13 - 7
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);

+ 23 - 13
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
 }