From fd26507bc59c651eabe378dede75f3baf5bd543e Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 6 May 2021 21:38:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dcron=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/cron.ts | 27 +++++++++++++++++---------- back/loaders/express.ts | 6 +++++- back/services/cron.ts | 10 +++++----- package.json | 2 +- src/pages/crontab/modal.tsx | 2 +- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/back/api/cron.ts b/back/api/cron.ts index 2f6811ef..4955476e 100644 --- a/back/api/cron.ts +++ b/back/api/cron.ts @@ -1,10 +1,9 @@ import { Router, Request, Response, NextFunction } from 'express'; import { Container } from 'typedi'; import { Logger } from 'winston'; -import * as fs from 'fs'; -import config from '../config'; import CronService from '../services/cron'; import { celebrate, Joi } from 'celebrate'; +import cron_parser from 'cron-parser'; const route = Router(); export default (app: Router) => { @@ -32,15 +31,19 @@ export default (app: Router) => { body: Joi.object({ command: Joi.string().required(), schedule: Joi.string().required(), - name: Joi.string(), + name: Joi.string().optional(), }), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { - const cronService = Container.get(CronService); - const data = await cronService.create(req.body); - return res.send({ code: 200, data }); + if (cron_parser.parseExpression(req.body.schedule).hasNext()) { + const cronService = Container.get(CronService); + const data = await cronService.create(req.body); + return res.send({ code: 200, data }); + } else { + return res.send({ code: 400, message: 'param schedule error' }); + } } catch (e) { logger.error('🔥 error: %o', e); return next(e); @@ -134,16 +137,20 @@ export default (app: Router) => { body: Joi.object({ command: Joi.string().required(), schedule: Joi.string().required(), - name: Joi.string(), + name: Joi.string().optional(), _id: Joi.string().required(), }), }), async (req: Request, res: Response, next: NextFunction) => { const logger: Logger = Container.get('logger'); try { - const cronService = Container.get(CronService); - const data = await cronService.update(req.body); - return res.send({ code: 200, data }); + if (cron_parser.parseExpression(req.body.schedule).hasNext()) { + const cronService = Container.get(CronService); + const data = await cronService.update(req.body); + return res.send({ code: 200, data }); + } else { + return res.send({ code: 400, message: 'param schedule error' }); + } } catch (e) { logger.error('🔥 error: %o', e); return next(e); diff --git a/back/loaders/express.ts b/back/loaders/express.ts index 6856fd2b..35b16192 100644 --- a/back/loaders/express.ts +++ b/back/loaders/express.ts @@ -49,7 +49,10 @@ export default ({ app }: { app: Application }) => { next: NextFunction, ) => { if (err.name === 'UnauthorizedError') { - return res.status(err.status).send({ message: err.message }).end(); + return res + .status(err.status) + .send({ code: 401, message: err.message }) + .end(); } return next(err); }, @@ -64,6 +67,7 @@ export default ({ app }: { app: Application }) => { ) => { res.status(err.status || 500); res.json({ + code: err.status || 500, message: err.message, }); }, diff --git a/back/services/cron.ts b/back/services/cron.ts index 80d1a3cd..da933818 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -229,11 +229,11 @@ export default class CronService { var command = line.replace(regex, '').trim(); var schedule = line.replace(command, '').trim(); - var is_valid = false; - try { - is_valid = cron_parser.parseString(line).expressions.length > 0; - } catch (e) {} - if (command && schedule && is_valid) { + if ( + command && + schedule && + cron_parser.parseExpression(schedule).hasNext() + ) { var name = namePrefix + '_' + index; this.cronDb.findOne({ command, schedule }, (err, doc) => { diff --git a/package.json b/package.json index 9ec88463..076acce4 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "body-parser": "^1.19.0", "celebrate": "^13.0.3", "cors": "^2.8.5", - "cron-parser": "^3.3.0", + "cron-parser": "^3.5.0", "dotenv": "^8.2.0", "express": "^4.17.1", "express-jwt": "^6.0.0", diff --git a/src/pages/crontab/modal.tsx b/src/pages/crontab/modal.tsx index 81a28674..6163afb7 100644 --- a/src/pages/crontab/modal.tsx +++ b/src/pages/crontab/modal.tsx @@ -80,7 +80,7 @@ const CronModal = ({ { required: true }, { validator: (rule, value) => { - if (cronParse.parseString(value).expressions.length > 0) { + if (cronParse.parseExpression(value).hasNext()) { return Promise.resolve(); } else { return Promise.reject('Cron表达式格式有误');