From 7bce5c4f6a1357c3db9e46562bcf2036d018b5a2 Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 13 Feb 2023 23:50:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=A2=9E=E5=8A=A0=E5=85=B3?= =?UTF-8?q?=E8=81=94=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/cron.ts | 2 ++ back/config/subscription.ts | 2 +- back/data/cron.ts | 3 +++ back/loaders/db.ts | 3 +++ back/services/schedule.ts | 8 ++++---- back/services/sshKey.ts | 1 - shell/api.sh | 28 +++++++++++++++------------- shell/share.sh | 2 +- shell/update.sh | 4 ++-- 9 files changed, 31 insertions(+), 22 deletions(-) diff --git a/back/api/cron.ts b/back/api/cron.ts index 275ac595..f47d4307 100644 --- a/back/api/cron.ts +++ b/back/api/cron.ts @@ -160,6 +160,7 @@ export default (app: Router) => { schedule: Joi.string().required(), name: Joi.string().optional(), labels: Joi.array().optional(), + sub_id: Joi.number().optional().allow(null), }), }), async (req: Request, res: Response, next: NextFunction) => { @@ -316,6 +317,7 @@ export default (app: Router) => { command: Joi.string().required(), schedule: Joi.string().required(), name: Joi.string().optional().allow(null), + sub_id: Joi.number().optional().allow(null), id: Joi.number().required(), }), }), diff --git a/back/config/subscription.ts b/back/config/subscription.ts index 450bb75f..a9d24572 100644 --- a/back/config/subscription.ts +++ b/back/config/subscription.ts @@ -17,7 +17,7 @@ export function formatUrl(doc: Subscription) { } export function formatCommand(doc: Subscription, url?: string) { - let command = 'ql '; + let command = `SUB_ID=${doc.id} ql `; let _url = url || formatUrl(doc).url; const { type, diff --git a/back/data/cron.ts b/back/data/cron.ts index 2675ba18..82502f09 100644 --- a/back/data/cron.ts +++ b/back/data/cron.ts @@ -17,6 +17,7 @@ export class Crontab { labels?: string[]; last_running_time?: number; last_execution_time?: number; + sub_id?: number; constructor(options: Crontab) { this.name = options.name; @@ -37,6 +38,7 @@ export class Crontab { this.labels = options.labels || []; this.last_running_time = options.last_running_time || 0; this.last_execution_time = options.last_execution_time || 0; + this.sub_id = options.sub_id; } } @@ -72,4 +74,5 @@ export const CrontabModel = sequelize.define('Crontab', { labels: DataTypes.JSON, last_running_time: DataTypes.NUMBER, last_execution_time: DataTypes.NUMBER, + sub_id: { type: DataTypes.NUMBER, allowNull: true }, }); diff --git a/back/loaders/db.ts b/back/loaders/db.ts index 2a2b52ac..ebdd7d10 100644 --- a/back/loaders/db.ts +++ b/back/loaders/db.ts @@ -46,6 +46,9 @@ export default async () => { 'alter table Subscriptions add column autoDelCron NUMBER', ); } catch (error) {} + try { + await sequelize.query('alter table Crontabs add column sub_id NUMBER'); + } catch (error) {} // 2.10-2.11 升级 const cronDbFile = path.join(config.rootPath, 'db/crontab.db'); diff --git a/back/services/schedule.ts b/back/services/schedule.ts index cfaa28b5..b8b91012 100644 --- a/back/services/schedule.ts +++ b/back/services/schedule.ts @@ -63,8 +63,8 @@ export default class ScheduleService { }); cp.stderr.on('data', async (data) => { - this.logger.error( - '执行任务 %s 失败,时间:%s, 错误信息:%j', + this.logger.info( + '[执行任务失败] %s,时间:%s, 错误信息:%j', command, new Date().toLocaleString(), data.toString(), @@ -74,7 +74,7 @@ export default class ScheduleService { cp.on('error', async (err) => { this.logger.error( - '创建任务 %s 失败,时间:%s, 错误信息:%j', + '[创建任务失败] %s,时间:%s, 错误信息:%j', command, new Date().toLocaleString(), err, @@ -84,7 +84,7 @@ export default class ScheduleService { cp.on('exit', async (code, signal) => { this.logger.info( - `任务 ${command} 进程id: ${cp.pid} 退出,退出码 ${code}`, + `[任务退出] ${command} 进程id: ${cp.pid},退出码 ${code}`, ); }); diff --git a/back/services/sshKey.ts b/back/services/sshKey.ts index 7db49654..040c8b4e 100644 --- a/back/services/sshKey.ts +++ b/back/services/sshKey.ts @@ -110,7 +110,6 @@ export default class SshKeyService { (doc.pull_option as any).private_key, ); const config = this.generateSingleSshConfig(alias, host, proxy); - console.log(config); result.push(config); } } diff --git a/shell/api.sh b/shell/api.sh index 55a6acfd..e2315cc5 100755 --- a/shell/api.sh +++ b/shell/api.sh @@ -19,10 +19,12 @@ add_cron_api() { local schedule=$(echo "$1" | awk -F ":" '{print $1}') local command=$(echo "$1" | awk -F ":" '{print $2}') local name=$(echo "$1" | awk -F ":" '{print $3}') + local sub_id=$(echo "$1" | awk -F ":" '{print $4}') else local schedule=$1 local command=$2 local name=$3 + local sub_id=$4 fi local api=$( @@ -34,11 +36,11 @@ add_cron_api() { -H "Origin: http://0.0.0.0:5700" \ -H "Referer: http://0.0.0.0:5700/crontab" \ -H "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" \ - --data-raw "{\"name\":\"$name\",\"command\":\"$command\",\"schedule\":\"$schedule\"}" \ + --data-raw "{\"name\":\"$name\",\"command\":\"$command\",\"schedule\":\"$schedule\",\"sub_id\":$sub_id}" \ --compressed ) - code=$(echo $api | jq -r .code) - message=$(echo $api | jq -r .message) + code=$(echo "$api" | jq -r .code) + message=$(echo "$api" | jq -r .message) if [[ $code == 200 ]]; then echo -e "$name -> 添加成功" else @@ -73,8 +75,8 @@ update_cron_api() { --data-raw "{\"name\":\"$name\",\"command\":\"$command\",\"schedule\":\"$schedule\",\"id\":\"$id\"}" \ --compressed ) - code=$(echo $api | jq -r .code) - message=$(echo $api | jq -r .message) + code=$(echo "$api" | jq -r .code) + message=$(echo "$api" | jq -r .message) if [[ $code == 200 ]]; then echo -e "$name -> 更新成功" else @@ -105,8 +107,8 @@ update_cron_command_api() { --data-raw "{\"command\":\"$command\",\"id\":\"$id\"}" \ --compressed ) - code=$(echo $api | jq -r .code) - message=$(echo $api | jq -r .message) + code=$(echo "$api" | jq -r .code) + message=$(echo "$api" | jq -r .message) if [[ $code == 200 ]]; then echo -e "$command -> 更新成功" else @@ -130,8 +132,8 @@ del_cron_api() { --data-raw "[$ids]" \ --compressed ) - code=$(echo $api | jq -r .code) - message=$(echo $api | jq -r .message) + code=$(echo "$api" | jq -r .code) + message=$(echo "$api" | jq -r .message) if [[ $code == 200 ]]; then echo -e "成功" else @@ -160,8 +162,8 @@ update_cron() { --data-raw "{\"ids\":[$ids],\"status\":\"$status\",\"pid\":\"$pid\",\"log_path\":\"$logPath\",\"last_execution_time\":$lastExecutingTime,\"last_running_time\":$runningTime}" \ --compressed ) - code=$(echo $api | jq -r .code) - message=$(echo $api | jq -r .message) + code=$(echo "$api" | jq -r .code) + message=$(echo "$api" | jq -r .message) if [[ $code != 200 ]]; then echo -e "\n## 更新任务状态失败(${message})\n" >>$dir_log/$log_path fi @@ -184,8 +186,8 @@ notify_api() { --data-raw "{\"title\":\"$title\",\"content\":\"$content\"}" \ --compressed ) - code=$(echo $api | jq -r .code) - message=$(echo $api | jq -r .message) + code=$(echo "$api" | jq -r .code) + message=$(echo "$api" | jq -r .message) if [[ $code == 200 ]]; then echo -e "通知发送成功" else diff --git a/shell/share.sh b/shell/share.sh index 4a5ffdfd..5d733a9e 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -326,7 +326,7 @@ git_pull_scripts() { local branch="$2" local proxy="$3" cd $dir_work - echo -e "开始更新仓库:$dir_work\n" + echo -e "开始更新仓库:$dir_work" set_proxy "$proxy" git fetch --all diff --git a/shell/update.sh b/shell/update.sh index cef03ce6..accd340d 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -97,7 +97,7 @@ add_cron() { [[ -z $cron_line ]] && cron_line=$(grep "cron:" $file | awk -F ":" '{print $2}' | head -1 | xargs) [[ -z $cron_line ]] && cron_line=$(grep "cron " $file | awk -F "cron \"" '{print $2}' | awk -F "\" " '{print $1}' | head -1 | xargs) [[ -z $cron_line ]] && cron_line="$default_cron" - result=$(add_cron_api "$cron_line:$cmd_task $file:$cron_name") + result=$(add_cron_api "$cron_line:$cmd_task $file:$cron_name:$SUB_ID") echo -e "$result" if [[ $detail ]]; then detail="${detail}${result}\n" @@ -181,7 +181,7 @@ update_raw() { [[ -z $cron_line ]] && cron_line=$(grep "cron " $raw_file_name | awk -F "cron \"" '{print $2}' | awk -F "\" " '{print $1}' | head -1 | xargs) [[ -z $cron_line ]] && cron_line="$default_cron" if [[ -z $cron_id ]]; then - result=$(add_cron_api "$cron_line:$cmd_task $filename:$cron_name") + result=$(add_cron_api "$cron_line:$cmd_task $filename:$cron_name:$SUB_ID") echo -e "$result\n" notify_api "新增任务通知" "\n$result" # update_cron_api "$cron_line:$cmd_task $filename:$cron_name:$cron_id"