添加最后运行时间和运行时长

This commit is contained in:
hanhh 2021-09-23 13:42:32 +08:00
parent c15ff89746
commit af2f3deeab
6 changed files with 124 additions and 14 deletions

View File

@ -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) => {

View File

@ -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[]) {

View File

@ -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)

View File

@ -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
}

View File

@ -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 (
<span
style={{
textAlign: 'left',
display: 'block',
}}
>
{record.last_execution_time
? new Date(record.last_execution_time * 1000).toLocaleString(
language,
{
hour12: false,
},
)
: '-'}
</span>
);
},
},
{
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 (
<span
style={{
textAlign: 'left',
display: 'block',
}}
>
{record.last_running_time
? diffTime(record.last_running_time)
: '-'}
</span>
);
},
},
{
title: '下次运行时间',
align: 'center' as const,

26
src/utils/date.ts Normal file
View File

@ -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;
}