From a2e82cf7c4b13b1f56d3bdb8ceb9123fb423b29f Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 10 May 2021 18:04:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=81=9C=E6=AD=A2=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/cron.ts | 27 +++++++++++++++------------ shell/api.sh | 12 +++++++----- shell/update.sh | 14 +++++++++++--- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/back/services/cron.ts b/back/services/cron.ts index 96f90d29..d9ac9003 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -22,12 +22,20 @@ export default class CronService { return this.cronDb; } + private isSixCron(cron: Crontab) { + const { schedule } = cron; + if (schedule.split(' ').length === 6) { + return true; + } + return false; + } + public async create(payload: Crontab): Promise { const tab = new Crontab(payload); tab.created = new Date().valueOf(); tab.saved = false; const doc = await this.insert(tab); - await this.set_crontab(); + await this.set_crontab(this.isSixCron(doc)); return doc; } @@ -76,7 +84,7 @@ export default class CronService { public async remove(ids: string[]) { this.cronDb.remove({ _id: { $in: ids } }, { multi: true }); - await this.set_crontab(); + await this.set_crontab(true); } public async crontabs(searchText?: string): Promise { @@ -126,14 +134,7 @@ export default class CronService { for (let i = 0; i < docs.length; i++) { const doc = docs[i]; if (doc.pid) { - exec(`kill -9 ${doc.pid}`, (err, stdout, stderr) => { - let logFile = `${config.manualLogPath}${doc._id}.log`; - this.cronDb.update( - { _id: doc._id }, - { $set: { status: CrontabStatus.idle } }, - ); - fs.appendFileSync(logFile, `\n\n${stderr}\n${stdout}...`); - }); + exec(`kill -9 ${doc.pid}`); } } }); @@ -223,7 +224,7 @@ export default class CronService { return crontab_job_string; } - private async set_crontab() { + private async set_crontab(needReloadSchedule: boolean = false) { const tabs = await this.crontabs(); var crontab_string = ''; tabs.forEach((tab) => { @@ -246,7 +247,9 @@ export default class CronService { fs.writeFileSync(config.crontabFile, crontab_string); execSync(`crontab ${config.crontabFile}`); - exec(`pm2 reload schedule`); + if (needReloadSchedule) { + exec(`pm2 reload schedule`); + } this.cronDb.update({}, { $set: { saved: true } }, { multi: true }); } diff --git a/shell/api.sh b/shell/api.sh index ff16fb2f..cd3be60a 100755 --- a/shell/api.sh +++ b/shell/api.sh @@ -74,10 +74,10 @@ update_cron_api() { } del_cron_api() { - local id=$1 + local ids=$1 local currentTimeStamp=$(date +%s) local api=$( - curl -s "http://localhost:5600/api/crons/$id?t=$currentTimeStamp" \ + curl -s "http://localhost:5600/api/crons?t=$currentTimeStamp" \ -X 'DELETE' \ -H "Accept: application/json" \ -H "Authorization: Bearer $token" \ @@ -85,14 +85,16 @@ del_cron_api() { -H "Content-Type: application/json;charset=UTF-8" \ -H "Origin: http://localhost:5700" \ -H "Referer: http://localhost:5700/crontab" \ - -H "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" + -H "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" \ + --data-raw "[$ids]" \ + --compressed ) code=$(echo $api | jq -r .code) message=$(echo $api | jq -r .message) if [[ $code == 200 ]]; then - echo -e "$name -> 删除成功" + echo -e "删除成功" else - echo -e "$name -> 删除失败(${message})" + echo -e "删除失败(${message})" fi } diff --git a/shell/update.sh b/shell/update.sh index f2d50f7f..182c4337 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -109,14 +109,22 @@ del_cron() { local list_drop=$1 local author=$2 local detail="" + local ids="" echo -e "开始尝试自动删除失效的定时任务...\n" for cron in $(cat $list_drop); do local id=$(cat $list_crontab_user | grep -E "$cmd_task $cron$" | perl -pe "s|.*ID=(.*) $cmd_task $cron$|\1|") - result=$(del_cron_api "$id") - echo -e "$result" + if [[ $ids ]]; then + ids="$ids,\"$id\"" + else + ids="\"$id\"" + fi + cron_name=$(grep "new Env" "$dir_scripts/${cron}" | awk -F "'|\"" '{print $2}' | head -1) + [[ -z $cron_name ]] && cron_name="$cron" + detail="${detail}\n${cron_name}" rm -f "$dir_scripts/${cron}" - detail="${detail}\n${result}" done + result=$(del_cron_api "$ids") + detail="${result}\n${detail}" notify "删除失效任务通知" "$detail" }