From 1b39d3ab48833b3c01f7f20dc18d043a3353f48b Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 20 Jul 2024 17:28:48 +0800 Subject: [PATCH] =?UTF-8?q?JavaScript=20=E5=92=8C=20Python=20=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E8=84=9A=E6=9C=AC=E5=89=8D=E6=89=A7=E8=A1=8C=20task?= =?UTF-8?q?=5Fbefore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shell/otask.sh | 33 ++++++++++----------------------- shell/preload/sitecustomize.js | 23 +++++++++++++++++++++++ shell/preload/sitecustomize.py | 25 +++++++++++++++++++++++++ shell/share.sh | 6 ++---- 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/shell/otask.sh b/shell/otask.sh index 7b74b82f303..9084e4fdf93 100755 --- a/shell/otask.sh +++ b/shell/otask.sh @@ -69,23 +69,6 @@ run_nohup() { nohup node $file_name &>$log_path & } -check_server() { - if [[ $cpu_warn ]] && [[ $mem_warn ]] && [[ $disk_warn ]]; then - local top_result=$(top -b -n 1) - cpu_use=$(echo "$top_result" | grep CPU | grep -v -E 'grep|PID' | awk '{print $2}' | cut -f 1 -d "%" | head -n 1) - - mem_free=$(free -m | grep "Mem" | awk '{print $3}' | head -n 1) - mem_total=$(free -m | grep "Mem" | awk '{print $2}' | head -n 1) - mem_use=$(printf "%d%%" $((mem_free * 100 / mem_total)) | cut -f 1 -d "%" | head -n 1) - - disk_use=$(df -P | grep /dev | grep -v -E '(tmp|boot|shm)' | awk '{print $5}' | cut -f 1 -d "%" | head -n 1) - if [[ $cpu_use -gt $cpu_warn ]] && [[ $cpu_warn ]] || [[ $mem_free -lt $mem_warn ]] || [[ $disk_use -gt $disk_warn ]]; then - local resource=$(echo "$top_result" | grep -v -E 'grep|Mem|idle|Load|tr' | awk '{$2="";$3="";$4="";$5="";$7="";print $0}' | head -n 10 | tr '\n' '|' | sed s/\|/\\\\n/g) - notify_api "服务器资源异常警告" "当前CPU占用 $cpu_use% 内存占用 $mem_use% 磁盘占用 $disk_use% \n资源占用详情 \n\n $resource" - fi - fi -} - env_str_to_array() { . $file_env local IFS="&" @@ -117,7 +100,7 @@ run_normal() { if [[ $isJsOrPythonFile == 'false' ]]; then clear_non_sh_env fi - configDir="${dir_config}" $timeoutCmd $which_program $file_param "${script_params[@]}" + fileName="${file_param}" taskBefore="${file_task_before}" configDir="${dir_config}" $timeoutCmd $which_program $file_param "${script_params[@]}" } handle_env_split() { @@ -160,7 +143,7 @@ run_concurrent() { export "${env_param}=${array[$i - 1]}" clear_non_sh_env fi - eval configDir="${dir_config}" envParam="${env_param}" numParam="${i}" $timeoutCmd $which_program $file_param "${script_params[@]}" &>$single_log_path & + eval fileName="${file_param}" taskBefore="${file_task_before}" configDir="${dir_config}" envParam="${env_param}" numParam="${i}" $timeoutCmd $which_program $file_param "${script_params[@]}" &>$single_log_path & done wait @@ -205,7 +188,7 @@ run_designated() { file_param=${file_param/$relative_path\//} fi - configDir="${dir_config}" envParam="${env_param}" numParam="${num_param}" $timeoutCmd $which_program $file_param "${script_params[@]}" + fileName="${file_param}" taskBefore="${file_task_before}" configDir="${dir_config}" envParam="${env_param}" numParam="${num_param}" $timeoutCmd $which_program $file_param "${script_params[@]}" } ## 运行其他命令 @@ -225,8 +208,7 @@ run_else() { $timeoutCmd $which_program $file_param "$@" } -## 命令检测 -main() { +check_file() { isJsOrPythonFile="false" if [[ $1 == *.js ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.ts ]]; then isJsOrPythonFile="true" @@ -240,7 +222,9 @@ main() { . $file_env fi fi +} +main() { if [[ $1 == *.js ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.sh ]] || [[ $1 == *.ts ]]; then if [[ $1 == *.sh ]]; then timeoutCmd="" @@ -276,7 +260,10 @@ main() { } handle_task_start "${task_shell_params[@]}" -run_task_before "${task_shell_params[@]}" +check_file "${task_shell_params[@]}" +if [[ $isJsOrPythonFile == 'false' ]]; then + run_task_before "${task_shell_params[@]}" +fi main "${task_shell_params[@]}" run_task_after "${task_shell_params[@]}" clear_env diff --git a/shell/preload/sitecustomize.js b/shell/preload/sitecustomize.js index aeb147ba995..6f6e1b30efd 100644 --- a/shell/preload/sitecustomize.js +++ b/shell/preload/sitecustomize.js @@ -1,3 +1,4 @@ +const { execSync } = require('child_process'); require(`./env.js`); function expandRange(rangeStr, max) { @@ -17,6 +18,28 @@ function expandRange(rangeStr, max) { } function run() { + try { + const splitStr = '__sitecustomize__'; + let command = `bash -c "source ${process.env.taskBefore} ${process.env.fileName}`; + if (process.env.task_before) { + command = `${command} && echo -e '执行前置命令\n' && eval "${process.env.task_before}" && echo -e '\n执行前置命令结束\n'`; + } + const res = execSync( + `${command} && echo "${splitStr}" && NODE_OPTIONS= node -p 'JSON.stringify(process.env)'"`, + { + encoding: 'utf-8', + }, + ); + const [output, envStr] = res.split(splitStr); + const json = JSON.parse(envStr.trim()); + for (const key in json) { + process.env[key] = json[key]; + } + console.log(output); + } catch (error) { + console.log(`run task before error `, error); + } + if (process.env.envParam && process.env.numParam) { const { envParam, numParam } = process.env; const array = (process.env[envParam] || '').split('&'); diff --git a/shell/preload/sitecustomize.py b/shell/preload/sitecustomize.py index b415877e540..3275a417d38 100644 --- a/shell/preload/sitecustomize.py +++ b/shell/preload/sitecustomize.py @@ -1,6 +1,8 @@ import os import re import env +import subprocess +import json def try_parse_int(value): @@ -30,6 +32,29 @@ def expand_range(range_str, max_value): def run(): + try: + split_str = "__sitecustomize__" + command = f'bash -c "source {os.getenv("taskBefore")} {os.getenv("fileName")}' + + if os.getenv("task_before"): + command += f" && echo -e '执行前置命令\n' && eval \"{os.getenv('task_before')}\" && echo -e '\n执行前置命令结束\n'" + + python_command = "PYTHONPATH= python3 -c 'import os, json; print(json.dumps(dict(os.environ)))'" + command += f' && echo "{split_str}" && {python_command}"' + + res = subprocess.check_output(command, shell=True, encoding="utf-8") + output, env_str = res.split(split_str) + + env_json = json.loads(env_str.strip()) + + for key, value in env_json.items(): + os.environ[key] = value + + print(output) + + except subprocess.CalledProcessError as error: + print(f"run task before error: {error}") + env_param = os.getenv("envParam") num_param = os.getenv("numParam") diff --git a/shell/share.sh b/shell/share.sh index bbf00ea3466..7afa627260c 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -459,13 +459,11 @@ handle_task_start() { } run_task_before() { - [[ $is_macos -eq 0 ]] && check_server - . $file_task_before "$@" if [[ $task_before ]]; then echo -e "执行前置命令\n" - eval "$task_before" + eval "$task_before" "$@" echo -e "\n执行前置命令结束\n" fi } @@ -475,7 +473,7 @@ run_task_after() { if [[ $task_after ]]; then echo -e "\n执行后置命令\n" - eval "$task_after" + eval "$task_after" "$@" echo -e "\n执行后置命令结束" fi }