Skip to content

Commit

Permalink
Merge pull request #8 from nameczz/nameczz
Browse files Browse the repository at this point in the history
hide system view and fix cron job
  • Loading branch information
shanghaikid authored Dec 21, 2021
2 parents d7232a3 + 480cca5 commit 713b771
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
9 changes: 1 addition & 8 deletions client/src/plugins/system/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 4 additions & 1 deletion express/src/__tests__/crons/crons.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
20 changes: 13 additions & 7 deletions express/src/crons/crons.controller.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,7 +21,7 @@ export class CronsController {

generateRoutes() {
this.router.put(
"/",
'/',
dtoValidationMiddleware(ToggleCronJobByNameDto),
this.toggleCronJobByName.bind(this)
);
Expand All @@ -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);
Expand Down
36 changes: 23 additions & 13 deletions express/src/crons/crons.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -42,29 +46,31 @@ 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();
}

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)
Expand All @@ -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 {
Expand All @@ -89,6 +97,7 @@ export class SchedulerRegistry {
this.cronJobList.push({
name,
entity: task,
address,
});
}
}
Expand All @@ -97,4 +106,5 @@ export class SchedulerRegistry {
interface CronJob {
name: string;
entity: ScheduledTask;
address: string; // milvus address
}

0 comments on commit 713b771

Please sign in to comment.