From 8174762c183f891732f76194749639dca3ad99ec Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 8 May 2025 01:43:14 +0800 Subject: [PATCH] =?UTF-8?q?demo=20=E7=8E=AF=E5=A2=83=E4=B8=8D=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=BF=90=E8=A1=8C=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/user.ts | 4 ++-- back/config/util.ts | 4 ++++ back/services/cron.ts | 26 ++++++++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/back/api/user.ts b/back/api/user.ts index f0a31a30..9da5ef0d 100644 --- a/back/api/user.ts +++ b/back/api/user.ts @@ -8,6 +8,7 @@ import path from 'path'; import { v4 as uuidV4 } from 'uuid'; import rateLimit from 'express-rate-limit'; import config from '../config'; +import { isDemoEnv } from '../config/util'; const route = Router(); const storage = multer.diskStorage({ @@ -72,9 +73,8 @@ export default (app: Router) => { }), }), async (req: Request, res: Response, next: NextFunction) => { - const logger: Logger = Container.get('logger'); try { - if (process.env.DeployEnv === 'demo') { + if (isDemoEnv()) { return res.send({ code: 450, message: '未知错误' }); } const userService = Container.get(UserService); diff --git a/back/config/util.ts b/back/config/util.ts index 2731307e..86f6826e 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -618,3 +618,7 @@ export function getUninstallCommand( return `${baseCommands[type]} ${name.trim()}`; } + +export function isDemoEnv() { + return process.env.DeployEnv === 'demo'; +} diff --git a/back/services/cron.ts b/back/services/cron.ts index 2a625be2..1d70660d 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -11,6 +11,7 @@ import { killTask, getUniqPath, safeJSONParse, + isDemoEnv, } from '../config/util'; import { Op, where, col as colFn, FindOptions, fn, Order } from 'sequelize'; import path from 'path'; @@ -53,6 +54,10 @@ export default class CronService { tab.saved = false; const doc = await this.insert(tab); + if (isDemoEnv()) { + return doc; + } + if (this.isNodeCron(doc) && !this.isSpecialSchedule(doc.schedule)) { await cronClient.addCron([ { @@ -79,7 +84,7 @@ export default class CronService { tab.saved = false; const newDoc = await this.updateDb(tab); - if (doc.isDisabled === 1) { + if (doc.isDisabled === 1 || isDemoEnv()) { return newDoc; } @@ -528,7 +533,7 @@ export default class CronService { await CrontabModel.update({ isDisabled: 0 }, { where: { id: ids } }); const docs = await CrontabModel.findAll({ where: { id: ids } }); const sixCron = docs - .filter((x) => this.isNodeCron(x)) + .filter((x) => this.isNodeCron(x) && !this.isSpecialSchedule(x.schedule)) .map((doc) => ({ name: doc.name || '', id: String(doc.id), @@ -536,6 +541,10 @@ export default class CronService { command: this.makeCommand(doc), extra_schedules: doc.extra_schedules || [], })); + + if (isDemoEnv()) { + return; + } await cronClient.addCron(sixCron); await this.setCrontab(); } @@ -609,11 +618,9 @@ export default class CronService { const tabs = data ?? (await this.crontabs()); var crontab_string = ''; tabs.data.forEach((tab) => { - const _schedule = tab.schedule && tab.schedule.split(/ +/); if ( tab.isDisabled === 1 || - _schedule!.length !== 5 || - tab.extra_schedules?.length || + this.isNodeCron(tab) || this.isSpecialSchedule(tab.schedule) ) { crontab_string += '# '; @@ -671,13 +678,11 @@ export default class CronService { public async autosave_crontab() { const tabs = await this.crontabs(); - this.setCrontab(tabs); - const regularCrons = tabs.data .filter( (x) => - this.isNodeCron(x) && x.isDisabled !== 1 && + this.isNodeCron(x) && !this.isSpecialSchedule(x.schedule), ) .map((doc) => ({ @@ -687,7 +692,12 @@ export default class CronService { command: this.makeCommand(doc), extra_schedules: doc.extra_schedules || [], })); + + if (isDemoEnv()) { + return; + } await cronClient.addCron(regularCrons); + this.setCrontab(tabs); } public async bootTask() {