Skip to content

Commit

Permalink
JavaScript 和 Python 执行脚本前执行 task_before
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Jul 20, 2024
1 parent f47a788 commit 1b39d3a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
33 changes: 10 additions & 23 deletions shell/otask.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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="&"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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[@]}"
}

## 运行其他命令
Expand All @@ -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"
Expand All @@ -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=""
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions shell/preload/sitecustomize.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { execSync } = require('child_process');
require(`./env.js`);

function expandRange(rangeStr, max) {
Expand All @@ -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('&');
Expand Down
25 changes: 25 additions & 0 deletions shell/preload/sitecustomize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import re
import env
import subprocess
import json


def try_parse_int(value):
Expand Down Expand Up @@ -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")

Expand Down
6 changes: 2 additions & 4 deletions shell/share.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 1b39d3a

Please sign in to comment.