重构任务日志,修复手动停止任务

This commit is contained in:
hanhh 2021-07-04 19:00:02 +08:00
parent c1a3ac6e4d
commit a8a2a54bea
6 changed files with 92 additions and 33 deletions

View File

@ -232,6 +232,8 @@ export default (app: Router) => {
body: Joi.object({ body: Joi.object({
ids: Joi.array().items(Joi.string().required()), ids: Joi.array().items(Joi.string().required()),
status: Joi.string().required(), status: Joi.string().required(),
pid: Joi.string().optional(),
log_path: Joi.string().optional(),
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
@ -241,6 +243,7 @@ export default (app: Router) => {
const data = await cronService.status({ const data = await cronService.status({
...req.body, ...req.body,
status: parseInt(req.body.status), status: parseInt(req.body.status),
pid: parseInt(req.body.pid) || '',
}); });
return res.send({ code: 200, data }); return res.send({ code: 200, data });
} catch (e) { } catch (e) {

View File

@ -10,6 +10,7 @@ export class Crontab {
isSystem?: 1 | 0; isSystem?: 1 | 0;
pid?: number; pid?: number;
isDisabled?: 1 | 0; isDisabled?: 1 | 0;
log_path?: string;
constructor(options: Crontab) { constructor(options: Crontab) {
this.name = options.name; this.name = options.name;
@ -25,6 +26,7 @@ export class Crontab {
this.isSystem = options.isSystem || 0; this.isSystem = options.isSystem || 0;
this.pid = options.pid; this.pid = options.pid;
this.isDisabled = options.isDisabled || 0; this.isDisabled = options.isDisabled || 0;
this.log_path = options.log_path || '';
} }
} }

View File

@ -87,11 +87,18 @@ export default class CronService {
public async status({ public async status({
ids, ids,
status, status,
pid,
log_path,
}: { }: {
ids: string[]; ids: string[];
status: CrontabStatus; status: CrontabStatus;
pid: number;
log_path: string;
}) { }) {
this.cronDb.update({ _id: { $in: ids } }, { $set: { status } }); this.cronDb.update(
{ _id: { $in: ids } },
{ $set: { status, pid, log_path } },
);
} }
public async remove(ids: string[]) { public async remove(ids: string[]) {
@ -158,17 +165,26 @@ export default class CronService {
public async stop(ids: string[]) { public async stop(ids: string[]) {
return new Promise((resolve: any) => { return new Promise((resolve: any) => {
this.cronDb.find({ _id: { $in: ids } }).exec((err, docs: Crontab[]) => { this.cronDb.find({ _id: { $in: ids } }).exec((err, docs: Crontab[]) => {
for (const doc of docs) {
if (doc.pid) {
try {
process.kill(-doc.pid);
} catch (error) {
this.logger.silly(error);
}
}
if (doc.log_path) {
fs.appendFileSync(
`${config.logPath}${doc.log_path}`,
`\n## 执行结束... ${new Date().toLocaleString()} `,
);
}
}
this.cronDb.update( this.cronDb.update(
{ _id: { $in: ids } }, { _id: { $in: ids } },
{ $set: { status: CrontabStatus.idle }, $unset: { pid: true } }, { $set: { status: CrontabStatus.idle }, $unset: { pid: true } },
); );
const pids = docs resolve();
.map((x) => x.pid)
.filter((x) => !!x)
.join('\n');
exec(`echo - e "${pids}" | xargs kill - 9`, (err) => {
resolve();
});
}); });
}); });
} }
@ -194,17 +210,19 @@ export default class CronService {
if (cmdStr.endsWith('.js')) { if (cmdStr.endsWith('.js')) {
cmdStr = `${cmdStr} now`; cmdStr = `${cmdStr} now`;
} }
const cp = exec(cmdStr, (err, stdout, stderr) => {
const cp = spawn(cmdStr, { shell: true, detached: true });
this.cronDb.update(
{ _id },
{ $set: { status: CrontabStatus.running, pid: cp.pid } },
);
cp.on('close', (code) => {
this.cronDb.update( this.cronDb.update(
{ _id }, { _id },
{ $set: { status: CrontabStatus.idle }, $unset: { pid: true } }, { $set: { status: CrontabStatus.idle }, $unset: { pid: true } },
); );
resolve(); resolve();
}); });
this.cronDb.update(
{ _id },
{ $set: { status: CrontabStatus.running, pid: cp.pid } },
);
}); });
} }
@ -238,6 +256,9 @@ export default class CronService {
public async log(_id: string) { public async log(_id: string) {
const doc = await this.get(_id); const doc = await this.get(_id);
if (doc.log_path) {
return getFileContentByName(`${config.logPath}/${doc.log_path}`);
}
const [, commandStr, url] = doc.command.split(' '); const [, commandStr, url] = doc.command.split(' ');
let logPath = this.getKey(commandStr); let logPath = this.getKey(commandStr);
const isQlCommand = doc.command.startsWith('ql '); const isQlCommand = doc.command.startsWith('ql ');

View File

@ -148,9 +148,11 @@ get_user_info() {
fi fi
} }
update_cron_status() { update_cron() {
local ids=$1 local ids="$1"
local status=$2 local status="$2"
local pid="${3:-''}"
local logPath="$4"
local currentTimeStamp=$(date +%s) local currentTimeStamp=$(date +%s)
local api=$( local api=$(
curl -s --noproxy "*" "http://localhost:5600/api/crons/status?t=$currentTimeStamp" \ curl -s --noproxy "*" "http://localhost:5600/api/crons/status?t=$currentTimeStamp" \
@ -162,15 +164,15 @@ update_cron_status() {
-H "Origin: http://localhost:5700" \ -H "Origin: http://localhost:5700" \
-H "Referer: http://localhost:5700/crontab" \ -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\":[$ids],\"status\":\"$status\"}" \ --data-raw "{\"ids\":[$ids],\"status\":\"$status\",\"pid\":\"$pid\",\"log_path\":\"$logPath\"}" \
--compressed --compressed
) )
code=$(echo $api | jq -r .code) code=$(echo $api | jq -r .code)
message=$(echo $api | jq -r .message) message=$(echo $api | jq -r .message)
if [[ $code == 200 ]]; then if [[ $code == 200 ]]; then
echo -e "更新任务状态成功" echo -e "更新任务状态成功" | tee -a $log_path
else else
echo -e "更新任务状态失败(${message})" echo -e "更新任务状态失败(${message})" | tee -a $log_path
fi fi
} }

View File

@ -76,7 +76,6 @@ run_normal() {
local p1=$1 local p1=$1
cd $dir_scripts cd $dir_scripts
define_program "$p1" define_program "$p1"
. $file_task_before
if [[ $p1 == *.js ]]; then if [[ $p1 == *.js ]]; then
if [[ $# -eq 1 ]]; then if [[ $# -eq 1 ]]; then
random_delay random_delay
@ -88,13 +87,16 @@ run_normal() {
log_path="$log_dir/$log_time.log" log_path="$log_dir/$log_time.log"
make_dir "$log_dir" make_dir "$log_dir"
local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1$" | perl -pe "s|.*ID=(.*) $cmd_task $p1$|\1|" | xargs | sed 's/ /","/g') local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1$" | perl -pe "s|.*ID=(.*) $cmd_task $p1$|\1|" | head -1 | awk -F " " '{print $1}')
local begin_time=$(date '+%Y-%m-%d %H:%M:%S') local begin_time=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "## 开始执行... $begin_time\n" | tee -a $log_path echo -e "## 开始执行... $begin_time\n" | tee -a $log_path
[[ $id ]] && update_cron_status "\"$id\"" "0" [[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path"
. $file_task_before
timeout $command_timeout_time $which_program $p1 2>&1 | tee -a $log_path timeout $command_timeout_time $which_program $p1 2>&1 | tee -a $log_path
. $file_task_after . $file_task_after
[[ $id ]] && update_cron_status "\"$id\"" "1" [[ $id ]] && update_cron "\"$id\"" "1" "" "$log_path"
local end_time=$(date '+%Y-%m-%d %H:%M:%S') local end_time=$(date '+%Y-%m-%d %H:%M:%S')
local diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time"))) local diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time")))
echo -e "\n## 执行结束... $end_time 耗时 $diff_time" | tee -a $log_path echo -e "\n## 执行结束... $end_time 耗时 $diff_time" | tee -a $log_path
@ -113,25 +115,54 @@ run_concurrent() {
local array=($(echo $envs | sed 's/&/ /g')) local array=($(echo $envs | sed 's/&/ /g'))
cd $dir_scripts cd $dir_scripts
define_program "$p1" define_program "$p1"
log_time=$(date "+%Y-%m-%d-%H-%M-%S")
log_dir_tmp="${p1##*/}" log_dir_tmp="${p1##*/}"
log_dir="$dir_log/${log_dir_tmp%%.*}" log_dir="$dir_log/${log_dir_tmp%%.*}"
log_path="$log_dir/$log_time.log"
make_dir $log_dir make_dir $log_dir
log_time=$(date "+%Y-%m-%d-%H-%M-%S.%N")
echo -e "\n各账号间已经在后台开始并发执行前台不输入日志日志直接写入文件中。\n" local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1$" | perl -pe "s|.*ID=(.*) $cmd_task $p1$|\1|" | head -1 | awk -F " " '{print $1}')
local begin_time=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "## 开始执行... $begin_time\n" | tee -a $log_path
[[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path"
. $file_task_before
echo -e "\n各账号间已经在后台开始并发执行前台不输入日志日志直接写入文件中。\n" | tee -a $log_path
single_log_time=$(date "+%Y-%m-%d-%H-%M-%S.%N")
for i in "${!array[@]}"; do for i in "${!array[@]}"; do
export ${p3}=${array[i]} export ${p3}=${array[i]}
log_path="$log_dir/${log_time}_$((i+1)).log" single_log_path="$log_dir/${single_log_time}_$((i+1)).log"
timeout $command_timeout_time $which_program $p1 &>$log_path & timeout $command_timeout_time $which_program $p1 &>$single_log_path &
done done
. $file_task_after
[[ $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")))
echo -e "\n## 执行结束... $end_time 耗时 $diff_time" | tee -a $log_path
} }
## 运行其他命令 ## 运行其他命令
run_else() { run_else() {
local log_time=$(date "+%Y-%m-%d-%H-%M-%S") local log_time=$(date "+%Y-%m-%d-%H-%M-%S")
local log_dir="$dir_log/$1" local log_dir_tmp="${1##*/}"
local log_path="$log_dir/$log_time.log" local log_dir="$dir_log/${log_dir_tmp%%.*}"
log_path="$log_dir/$log_time.log"
make_dir "$log_dir" make_dir "$log_dir"
timeout $command_timeout_time "$@" 2>&1 | tee $log_path
local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1$" | perl -pe "s|.*ID=(.*) $cmd_task $p1$|\1|" | head -1 | awk -F " " '{print $1}')
local begin_time=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "## 开始执行... $begin_time\n" | tee -a $log_path
[[ $id ]] && update_cron "\"$id\"" "0" "$$" "$log_path"
. $file_task_before
timeout $command_timeout_time "$@" 2>&1 | tee -a $log_path
. $file_task_after
[[ $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")))
echo -e "\n## 执行结束... $end_time 耗时 $diff_time" | tee -a $log_path
} }
## 命令检测 ## 命令检测

View File

@ -72,7 +72,7 @@ del_cron() {
local ids="" local ids=""
echo -e "开始尝试自动删除失效的定时任务...\n" echo -e "开始尝试自动删除失效的定时任务...\n"
for cron in $(cat $list_drop); do 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|" | xargs | sed 's/ /","/g' | head -1) local id=$(cat $list_crontab_user | grep -E "$cmd_task $cron" | perl -pe "s|.*ID=(.*) $cmd_task $cron\.*|\1|" | head -1 | head -1 | awk -F " " '{print $1}')
if [[ $ids ]]; then if [[ $ids ]]; then
ids="$ids,\"$id\"" ids="$ids,\"$id\""
else else
@ -179,7 +179,7 @@ update_raw() {
echo -e "下载 ${raw_file_name} 成功...\n" echo -e "下载 ${raw_file_name} 成功...\n"
cd $dir_raw cd $dir_raw
local filename="raw_${raw_file_name}" local filename="raw_${raw_file_name}"
local cron_id=$(cat $list_crontab_user | grep -E "$cmd_task $filename" | perl -pe "s|.*ID=(.*) $cmd_task $filename\.*|\1|" | head -1) local cron_id=$(cat $list_crontab_user | grep -E "$cmd_task $filename" | perl -pe "s|.*ID=(.*) $cmd_task $filename\.*|\1|" | head -1 | head -1 | awk -F " " '{print $1}')
cp -f $raw_file_name $dir_scripts/${filename} cp -f $raw_file_name $dir_scripts/${filename}
cron_line=$( cron_line=$(
perl -ne "{ perl -ne "{
@ -376,7 +376,7 @@ gen_list_repo() {
filename=$(basename $file) filename=$(basename $file)
cp -f $file $dir_scripts/${repo}_${filename} cp -f $file $dir_scripts/${repo}_${filename}
echo ${repo}_${filename} >>"$dir_list_tmp/${repo}_scripts.list" echo ${repo}_${filename} >>"$dir_list_tmp/${repo}_scripts.list"
cron_id=$(cat $list_crontab_user | grep -E "$cmd_task ${author}_${filename}" | perl -pe "s|.*ID=(.*) $cmd_task ${author}_${filename}\.*|\1|" | head -1) cron_id=$(cat $list_crontab_user | grep -E "$cmd_task ${author}_${filename}" | perl -pe "s|.*ID=(.*) $cmd_task ${author}_${filename}\.*|\1|" | head -1 | awk -F " " '{print $1}')
if [[ $cron_id ]]; then if [[ $cron_id ]]; then
result=$(update_cron_command_api "$cmd_task ${repo}_${filename}:$cron_id") result=$(update_cron_command_api "$cmd_task ${repo}_${filename}:$cron_id")
fi fi