Skip to content

Commit 713b771

Browse files
authored
Merge pull request #8 from nameczz/nameczz
hide system view and fix cron job
2 parents d7232a3 + 480cca5 commit 713b771

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

client/src/plugins/system/config.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
{
22
"name": "system-view",
3-
"version": "0.1.0",
4-
"client": {
5-
"path": "system",
6-
"auth": true,
7-
"entry": "SystemView.tsx",
8-
"label": "System View",
9-
"iconName": "navSystem"
10-
}
3+
"version": "0.1.0"
114
}

express/src/__tests__/crons/crons.service.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ describe('test crons service', () => {
158158
newSchedulerRegistry
159159
);
160160

161-
await newCronsService.getCollections(WS_EVENTS.COLLECTION);
161+
await newCronsService.getCollections(
162+
WS_EVENTS.COLLECTION,
163+
'127.0.0.1:19530'
164+
);
162165
expect(schedule).toBeCalledWith(mockCronEverySec, expect.any(Function));
163166
expect(handleEndTask).toBeCalled();
164167
});

express/src/crons/crons.controller.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { NextFunction, Request, Response, Router } from "express";
2-
import { dtoValidationMiddleware } from "../middlewares/validation";
3-
import { CronsService, SchedulerRegistry } from "./crons.service";
4-
import { collectionsService } from "../collections";
5-
import { ToggleCronJobByNameDto } from "./dto";
1+
import { NextFunction, Request, Response, Router } from 'express';
2+
import { dtoValidationMiddleware } from '../middlewares/validation';
3+
import { CronsService, SchedulerRegistry } from './crons.service';
4+
import { collectionsService } from '../collections';
5+
import { ToggleCronJobByNameDto } from './dto';
6+
import { MILVUS_ADDRESS } from '../utils/Const';
67

78
export class CronsController {
89
private router: Router;
@@ -20,7 +21,7 @@ export class CronsController {
2021

2122
generateRoutes() {
2223
this.router.put(
23-
"/",
24+
'/',
2425
dtoValidationMiddleware(ToggleCronJobByNameDto),
2526
this.toggleCronJobByName.bind(this)
2627
);
@@ -30,8 +31,13 @@ export class CronsController {
3031

3132
async toggleCronJobByName(req: Request, res: Response, next: NextFunction) {
3233
const cronData = req.body;
34+
const milvusAddress = (req.headers[MILVUS_ADDRESS] as string) || '';
35+
console.log(cronData, milvusAddress);
3336
try {
34-
const result = await this.cronsService.toggleCronJobByName(cronData);
37+
const result = await this.cronsService.toggleCronJobByName({
38+
...cronData,
39+
address: milvusAddress,
40+
});
3541
res.send(result);
3642
} catch (error) {
3743
next(error);

express/src/crons/crons.service.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ export class CronsService {
99
private schedulerRegistry: SchedulerRegistry
1010
) {}
1111

12-
async toggleCronJobByName(data: { name: string; type: WS_EVENTS_TYPE }) {
13-
const { name, type } = data;
12+
async toggleCronJobByName(data: {
13+
name: string;
14+
type: WS_EVENTS_TYPE;
15+
address: string;
16+
}) {
17+
const { name, type, address } = data;
1418

1519
switch (name) {
1620
case WS_EVENTS.COLLECTION:
17-
const cronJobEntity = this.schedulerRegistry.getCronJob(name);
21+
const cronJobEntity = this.schedulerRegistry.getCronJob(name, address);
1822
if (!cronJobEntity && Number(type) === WS_EVENTS_TYPE.START) {
19-
return this.getCollections(WS_EVENTS.COLLECTION);
23+
return this.getCollections(WS_EVENTS.COLLECTION, address);
2024
}
2125
if (!cronJobEntity) {
2226
return;
@@ -29,7 +33,7 @@ export class CronsService {
2933
}
3034
}
3135

32-
async getCollections(name: string) {
36+
async getCollections(name: string, address: string) {
3337
const task = async () => {
3438
try {
3539
const res = await this.collectionService.getAllCollections();
@@ -42,29 +46,31 @@ export class CronsService {
4246
return res;
4347
} catch (error) {
4448
// When user not connect milvus, stop cron
45-
const cronJobEntity = this.schedulerRegistry.getCronJob(name);
49+
const cronJobEntity = this.schedulerRegistry.getCronJob(name, address);
4650
if (cronJobEntity) {
4751
cronJobEntity.stop();
4852
}
4953

5054
throw new Error(error);
5155
}
5256
};
53-
this.schedulerRegistry.setCronJobEverySecond(name, task);
57+
this.schedulerRegistry.setCronJobEverySecond(name, task, address);
5458
}
5559
}
5660

5761
export class SchedulerRegistry {
5862
constructor(private cronJobList: CronJob[]) {}
5963

60-
getCronJob(name: string) {
61-
const target = this.cronJobList.find((item) => item.name === name);
64+
getCronJob(name: string, address: string) {
65+
const target = this.cronJobList.find(
66+
item => item.name === name && item.address === address
67+
);
6268
return target?.entity;
6369
}
6470

65-
setCronJobEverySecond(name: string, func: () => {}) {
71+
setCronJobEverySecond(name: string, func: () => {}, address: string) {
6672
// The cron job will run every second
67-
this.setCronJob(name, '* * * * * *', func);
73+
this.setCronJob(name, '* * * * * *', func, address);
6874
}
6975

7076
// ┌────────────── second (optional)
@@ -77,8 +83,10 @@ export class SchedulerRegistry {
7783
// │ │ │ │ │ │
7884
// * * * * * *
7985
// https://www.npmjs.com/package/node-cron
80-
setCronJob(name: string, scheduler: string, func: () => {}) {
81-
const target = this.cronJobList.find((item) => item.name === name);
86+
setCronJob(name: string, scheduler: string, func: () => {}, address: string) {
87+
const target = this.cronJobList.find(
88+
item => item.name === name && item.address === address
89+
);
8290
if (target) {
8391
target?.entity?.stop();
8492
} else {
@@ -89,6 +97,7 @@ export class SchedulerRegistry {
8997
this.cronJobList.push({
9098
name,
9199
entity: task,
100+
address,
92101
});
93102
}
94103
}
@@ -97,4 +106,5 @@ export class SchedulerRegistry {
97106
interface CronJob {
98107
name: string;
99108
entity: ScheduledTask;
109+
address: string; // milvus address
100110
}

0 commit comments

Comments
 (0)