From dbfaddafca84ec910b0eee57172ba7b05deac1dc Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 1 Apr 2023 16:30:55 +0800 Subject: [PATCH] Validate cron before submit --- server/model/maintenance.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/server/model/maintenance.js b/server/model/maintenance.js index 189a513a..e3ce5d32 100644 --- a/server/model/maintenance.js +++ b/server/model/maintenance.js @@ -164,6 +164,7 @@ class Maintenance extends BeanModel { if (bean.strategy === "cron") { bean.duration = obj.durationMinutes * 60; bean.cron = obj.cron; + this.validateCron(bean.cron); } if (bean.strategy.startsWith("recurring-")) { @@ -172,11 +173,21 @@ class Maintenance extends BeanModel { bean.weekdays = JSON.stringify(obj.weekdays); bean.days_of_month = JSON.stringify(obj.daysOfMonth); await bean.generateCron(); + this.validateCron(bean.cron); } - return bean; } + /** + * Throw error if cron is invalid + * @param cron + * @returns {Promise} + */ + static async validateCron(cron) { + let job = new Cron(cron, () => {}); + job.stop(); + } + /** * Run the cron */