From 517fdcc8ec2beb0f0174009f6bc9b3c90e465d75 Mon Sep 17 00:00:00 2001 From: hanhh <18330117883@163.com> Date: Wed, 7 Jul 2021 18:26:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dspawn=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E4=B8=AD=E6=96=AD=EF=BC=8C=E4=BF=AE=E5=A4=8Dtoken=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E4=BB=BB=E5=8A=A1=E7=8A=B6=E6=80=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/loaders/express.ts | 7 +++++++ back/services/cron.ts | 11 +++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/back/loaders/express.ts b/back/loaders/express.ts index 4cdd7f12..92714249 100644 --- a/back/loaders/express.ts +++ b/back/loaders/express.ts @@ -30,6 +30,13 @@ export default ({ app }: { app: Application }) => { if (!headerToken && req.path && req.path === '/api/login') { return next(); } + const remoteAddress = req.socket.remoteAddress; + if ( + remoteAddress === '::ffff:127.0.0.1' && + req.path === '/api/crons/status' + ) { + return next(); + } const err: any = new Error('UnauthorizedError'); err['status'] = 401; next(err); diff --git a/back/services/cron.ts b/back/services/cron.ts index fbccceaf..23fc0941 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -211,18 +211,17 @@ export default class CronService { cmdStr = `${cmdStr} now`; } - const cp = spawn(cmdStr, { shell: true, detached: true }); - this.cronDb.update( - { _id }, - { $set: { status: CrontabStatus.running, pid: cp.pid } }, - ); - cp.on('close', (code) => { + const cp = exec(cmdStr, (err, stdout, stderr) => { this.cronDb.update( { _id }, { $set: { status: CrontabStatus.idle }, $unset: { pid: true } }, ); resolve(); }); + this.cronDb.update( + { _id }, + { $set: { status: CrontabStatus.running, pid: cp.pid } }, + ); }); }