crons.controller.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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';
  7. export class CronsController {
  8. private router: Router;
  9. private schedulerRegistry: SchedulerRegistry;
  10. private cronsService: CronsService;
  11. constructor() {
  12. this.schedulerRegistry = new SchedulerRegistry([]);
  13. this.cronsService = new CronsService(
  14. collectionsService,
  15. this.schedulerRegistry
  16. );
  17. this.router = Router();
  18. }
  19. generateRoutes() {
  20. this.router.put(
  21. '/',
  22. dtoValidationMiddleware(ToggleCronJobByNameDto),
  23. this.toggleCronJobByName.bind(this)
  24. );
  25. return this.router;
  26. }
  27. async toggleCronJobByName(req: Request, res: Response, next: NextFunction) {
  28. const cronData = req.body;
  29. const milvusAddress = (req.headers[MILVUS_ADDRESS] as string) || '';
  30. console.log(cronData, milvusAddress);
  31. try {
  32. const result = await this.cronsService.toggleCronJobByName({
  33. ...cronData,
  34. address: milvusAddress,
  35. });
  36. res.send(result);
  37. } catch (error) {
  38. next(error);
  39. }
  40. }
  41. }