From af2f3deeab10cc40b36edf72cbc1fc54e85ce751 Mon Sep 17 00:00:00 2001
From: hanhh <18330117883@163.com>
Date: Thu, 23 Sep 2021 13:42:32 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=80=E5=90=8E=E8=BF=90?=
=?UTF-8?q?=E8=A1=8C=E6=97=B6=E9=97=B4=E5=92=8C=E8=BF=90=E8=A1=8C=E6=97=B6?=
=?UTF-8?q?=E9=95=BF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
back/api/cron.ts | 2 ++
back/services/cron.ts | 26 +++++++++++++++---
shell/api.sh | 4 ++-
shell/task.sh | 26 +++++++++++-------
src/pages/crontab/index.tsx | 54 +++++++++++++++++++++++++++++++++++++
src/utils/date.ts | 26 ++++++++++++++++++
6 files changed, 124 insertions(+), 14 deletions(-)
create mode 100644 src/utils/date.ts
diff --git a/back/api/cron.ts b/back/api/cron.ts
index 75b46150..2260d60c 100644
--- a/back/api/cron.ts
+++ b/back/api/cron.ts
@@ -270,6 +270,8 @@ export default (app: Router) => {
status: Joi.string().required(),
pid: Joi.string().optional(),
log_path: Joi.string().optional(),
+ last_running_time: Joi.string().optional(),
+ last_execution_time: Joi.string().optional(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
diff --git a/back/services/cron.ts b/back/services/cron.ts
index b1b68564..ebe94e6e 100644
--- a/back/services/cron.ts
+++ b/back/services/cron.ts
@@ -90,16 +90,34 @@ export default class CronService {
status,
pid,
log_path,
+ last_running_time,
+ last_execution_time,
}: {
ids: string[];
status: CrontabStatus;
pid: number;
log_path: string;
+ last_running_time: string;
+ last_execution_time: string;
}) {
- this.cronDb.update(
- { _id: { $in: ids } },
- { $set: { status, pid, log_path } },
- );
+ return new Promise((resolve) => {
+ this.cronDb.update(
+ { _id: { $in: ids } },
+ {
+ $set: {
+ status,
+ pid,
+ log_path,
+ last_running_time,
+ last_execution_time,
+ },
+ },
+ { multi: true, returnUpdatedDocs: true },
+ (err) => {
+ resolve(null);
+ },
+ );
+ });
}
public async remove(ids: string[]) {
diff --git a/shell/api.sh b/shell/api.sh
index a9609c02..996ca1af 100755
--- a/shell/api.sh
+++ b/shell/api.sh
@@ -153,6 +153,8 @@ update_cron() {
local status="$2"
local pid="${3:-''}"
local logPath="$4"
+ local lastExecutingTime="$5"
+ local runningTime="$6"
local currentTimeStamp=$(date +%s)
local api=$(
curl -s --noproxy "*" "http://0.0.0.0:5600/api/crons/status?t=$currentTimeStamp" \
@@ -164,7 +166,7 @@ update_cron() {
-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 "{\"ids\":[$ids],\"status\":\"$status\",\"pid\":\"$pid\",\"log_path\":\"$logPath\"}" \
+ --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)
diff --git a/shell/task.sh b/shell/task.sh
index 49916976..5aa6017a 100755
--- a/shell/task.sh
+++ b/shell/task.sh
@@ -92,19 +92,21 @@ run_normal() {
make_dir "$log_dir"
local begin_time=$(date '+%Y-%m-%d %H:%M:%S')
+ local begin_timestamp=$(date "+%s" -d "$begin_time")
eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd
[[ -f $task_error_log_path ]] && eval cat $task_error_log_path $cmd
local id=$(cat $list_crontab_user | grep -E "$cmd_task $first_param" | perl -pe "s|.*ID=(.*) $cmd_task $first_param\.*|\1|" | head -1 | awk -F " " '{print $1}')
- [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path"
+ [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path" "$begin_timestamp"
eval . $file_task_before "$@" $cmd
eval timeout -k 10s $command_timeout_time $which_program $first_param $cmd
eval . $file_task_after "$@" $cmd
- [[ $id ]] && update_cron "\"$id\"" "1" "" "$log_path"
local end_time=$(date '+%Y-%m-%d %H:%M:%S')
- local diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time")))
+ local end_timestamp=$(date "+%s" -d "$end_time")
+ local diff_time=$(( $end_timestamp - $begin_timestamp ))
+ [[ $id ]] && update_cron "\"$id\"" "1" "" "$log_path" "$begin_timestamp" "$diff_time"
eval echo -e "\\\n\#\# 执行结束... $end_time 耗时 $diff_time 秒" $cmd
}
@@ -128,11 +130,13 @@ run_concurrent() {
make_dir $log_dir
local begin_time=$(date '+%Y-%m-%d %H:%M:%S')
+ local begin_timestamp=$(date "+%s" -d "$begin_time")
+
eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd
[[ -f $task_error_log_path ]] && eval cat $task_error_log_path $cmd
local id=$(cat $list_crontab_user | grep -E "$cmd_task $first_param" | perl -pe "s|.*ID=(.*) $cmd_task $first_param\.*|\1|" | head -1 | awk -F " " '{print $1}')
- [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path"
+ [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path" "$begin_timestamp"
eval . $file_task_before "$@" $cmd
local envs=$(eval echo "\$${third_param}")
@@ -152,9 +156,10 @@ run_concurrent() {
done
eval . $file_task_after "$@" $cmd
- [[ $id ]] && update_cron "\"$id\"" "1" "" "$log_path"
local end_time=$(date '+%Y-%m-%d %H:%M:%S')
- local diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time")))
+ local end_timestamp=$(date "+%s" -d "$end_time")
+ local diff_time=$(( $end_timestamp - $begin_timestamp ))
+ [[ $id ]] && update_cron "\"$id\"" "1" "" "$log_path" "$begin_timestamp" "$diff_time"
eval echo -e "\\\n\#\# 执行结束... $end_time 耗时 $diff_time 秒" $cmd
}
@@ -169,19 +174,22 @@ run_else() {
make_dir "$log_dir"
local begin_time=$(date '+%Y-%m-%d %H:%M:%S')
+ local begin_timestamp=$(date "+%s" -d "$begin_time")
+
eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd
[[ -f $task_error_log_path ]] && eval cat $task_error_log_path $cmd
local id=$(cat $list_crontab_user | grep -E "$cmd_task $first_param" | perl -pe "s|.*ID=(.*) $cmd_task $first_param\.*|\1|" | head -1 | awk -F " " '{print $1}')
- [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path"
+ [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path" "$begin_timestamp"
eval . $file_task_before "$@" $cmd
eval timeout -k 10s $command_timeout_time "$@" $cmd
eval . $file_task_after "$@" $cmd
- [[ $id ]] && update_cron "\"$id\"" "1" "" "$log_path"
local end_time=$(date '+%Y-%m-%d %H:%M:%S')
- local diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time")))
+ local end_timestamp=$(date "+%s" -d "$end_time")
+ local diff_time=$(( $end_timestamp - $begin_timestamp ))
+ [[ $id ]] && update_cron "\"$id\"" "1" "" "$log_path" "$begin_timestamp" "$diff_time"
eval echo -e "\\\n\#\# 执行结束... $end_time 耗时 $diff_time 秒" $cmd
}
diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx
index 3119dfcc..4dc0858d 100644
--- a/src/pages/crontab/index.tsx
+++ b/src/pages/crontab/index.tsx
@@ -33,6 +33,7 @@ import { request } from '@/utils/http';
import CronModal from './modal';
import CronLogModal from './logModal';
import cron_parser from 'cron-parser';
+import { diffTime } from '@/utils/date';
const { Text } = Typography;
const { Search } = Input;
@@ -123,6 +124,59 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
multiple: 1,
},
},
+ {
+ title: '最后运行时间',
+ align: 'center' as const,
+ sorter: {
+ compare: (a: any, b: any) => {
+ return a.last_execution_time - b.last_execution_time;
+ },
+ },
+ render: (text: string, record: any) => {
+ const language = navigator.language || navigator.languages[0];
+ return (
+
+ {record.last_execution_time
+ ? new Date(record.last_execution_time * 1000).toLocaleString(
+ language,
+ {
+ hour12: false,
+ },
+ )
+ : '-'}
+
+ );
+ },
+ },
+ {
+ title: '最后运行时长',
+ align: 'center' as const,
+ sorter: {
+ compare: (a: any, b: any) => {
+ return a.last_running_time - b.last_running_time;
+ },
+ },
+ render: (text: string, record: any) => {
+ const language = navigator.language || navigator.languages[0];
+ return (
+
+ {record.last_running_time
+ ? diffTime(record.last_running_time)
+ : '-'}
+
+ );
+ },
+ },
{
title: '下次运行时间',
align: 'center' as const,
diff --git a/src/utils/date.ts b/src/utils/date.ts
new file mode 100644
index 00000000..59d71a16
--- /dev/null
+++ b/src/utils/date.ts
@@ -0,0 +1,26 @@
+export function diffTime(num: number) {
+ const diff = num * 1000;
+
+ const days = Math.floor(diff / (24 * 3600 * 1000));
+
+ const leave1 = diff % (24 * 3600 * 1000);
+ const hours = Math.floor(leave1 / (3600 * 1000));
+
+ const leave2 = leave1 % (3600 * 1000);
+ const minutes = Math.floor(leave2 / (60 * 1000));
+
+ const leave3 = leave2 % (60 * 1000);
+ const seconds = Math.round(leave3 / 1000);
+
+ let returnStr = seconds + '秒';
+ if (minutes > 0) {
+ returnStr = minutes + '分' + returnStr;
+ }
+ if (hours > 0) {
+ returnStr = hours + '小时' + returnStr;
+ }
+ if (days > 0) {
+ returnStr = days + '天' + returnStr;
+ }
+ return returnStr;
+}