Skip to content

Commit

Permalink
修复定时任务 task_after 命令无法停止
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Oct 11, 2023
1 parent 99f6073 commit c6ec4aa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions back/config/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ export function psTree(pid: number): Promise<number[]> {

export async function killTask(pid: number) {
const pids = await psTree(pid);
// SIGINT 2 程序终止(interrupt)信号,不会打印额外信息

if (pids.length) {
try {
[pid, ...pids].forEach((x) => {
process.kill(x, 2);
[pid, ...pids].reverse().forEach((x) => {
process.kill(x, 15);
});
} catch (error) { }
} else {
Expand Down
6 changes: 4 additions & 2 deletions shell/otask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ main() {
fi
}

handle_task_before "$@"
handle_task_start "$@"
run_task_before "$@"
main "$@"
handle_task_after "$@"
run_task_after "$@"
handle_task_end "$@"
12 changes: 7 additions & 5 deletions shell/share.sh
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ init_nginx() {
location_url="^~${ql_base_url%*/}"
aliasStr="alias ${dir_static}/dist;"
if ! grep -q "<base href=\"$ql_base_url\">" "${dir_static}/dist/index.html"; then
awk -v text="<base href=\"$ql_base_url\">" '/<link/ && !inserted {print text; inserted=1} 1' "${dir_static}/dist/index.html" > temp.html
awk -v text="<base href=\"$ql_base_url\">" '/<link/ && !inserted {print text; inserted=1} 1' "${dir_static}/dist/index.html" >temp.html
mv temp.html "${dir_static}/dist/index.html"
fi
else
Expand All @@ -438,11 +438,12 @@ init_nginx() {
sed -i "s,IPV4_CONFIG,${ipv4Str},g" /etc/nginx/conf.d/front.conf
}

handle_task_before() {
handle_task_start() {
[[ $ID ]] && update_cron "\"$ID\"" "0" "$$" "$log_path" "$begin_timestamp"

echo -e "## 开始执行... $begin_time\n"
}

run_task_before() {
[[ $is_macos -eq 0 ]] && check_server

. $file_task_before "$@"
Expand All @@ -454,15 +455,17 @@ handle_task_before() {
fi
}

handle_task_after() {
run_task_after() {
. $file_task_after "$@"

if [[ $task_after ]]; then
echo -e "\n执行后置命令\n"
eval "$task_after"
echo -e "\n执行后置命令结束"
fi
}

handle_task_end() {
local etime=$(date "+$time_format")
local end_time=$(format_time "$time_format" "$etime")
local end_timestamp=$(format_timestamp "$time_format" "$etime")
Expand All @@ -471,7 +474,6 @@ handle_task_after() {
[[ "$diff_time" == 0 ]] && diff_time=1

echo -e "\n## 执行结束... $end_time 耗时 $diff_time 秒     "

[[ $ID ]] && update_cron "\"$ID\"" "1" "" "$log_path" "$begin_timestamp" "$diff_time"
}

Expand Down
2 changes: 1 addition & 1 deletion shell/task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dir_shell=$QL_DIR/shell

trap "single_hanle" 2 3 20 15 14 19 1
single_hanle() {
eval handle_task_after "$@" "$cmd"
eval run_task_after "$@" "$cmd"
exit 1
}

Expand Down

0 comments on commit c6ec4aa

Please sign in to comment.