mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-20 16:07:11 +08:00
feat(ql3): observe system crond runs idempotently
This commit is contained in:
@@ -142,11 +142,15 @@ update_cron() {
|
||||
local lastExecutingTime="${5:-0}"
|
||||
local runningTime="${6:-0}"
|
||||
local exitCode="${7:-}"
|
||||
local executionId="${8:-}"
|
||||
local currentTimeStamp=$(date +%s)
|
||||
local dataRaw="{\"ids\":[$ids],\"status\":\"$status\",\"pid\":\"$pid\",\"log_path\":\"$logPath\",\"last_execution_time\":$lastExecutingTime,\"last_running_time\":$runningTime"
|
||||
if [[ -n $exitCode ]]; then
|
||||
dataRaw="${dataRaw},\"exit_code\":$exitCode"
|
||||
fi
|
||||
if [[ -n $executionId ]]; then
|
||||
dataRaw="${dataRaw},\"execution_id\":\"$executionId\""
|
||||
fi
|
||||
dataRaw="${dataRaw}}"
|
||||
local api=$(
|
||||
curl -s --noproxy "*" "http://localhost:${ql_port}/open/crons/status?t=$currentTimeStamp" \
|
||||
@@ -166,6 +170,27 @@ update_cron() {
|
||||
fi
|
||||
}
|
||||
|
||||
create_legacy_system_execution_id() {
|
||||
local startedAt="$1"
|
||||
local uuid=""
|
||||
if [[ "${QL_EXECUTION_ORIGIN:-}" != "scheduled_system" ]] ||
|
||||
[[ ! "$startedAt" =~ ^[0-9]{1,13}$ ]]; then
|
||||
return 0
|
||||
fi
|
||||
if [[ -r /proc/sys/kernel/random/uuid ]]; then
|
||||
IFS= read -r uuid </proc/sys/kernel/random/uuid
|
||||
elif command -v uuidgen >/dev/null 2>&1; then
|
||||
uuid=$(uuidgen)
|
||||
elif command -v node >/dev/null 2>&1; then
|
||||
uuid=$(node -e "process.stdout.write(require('node:crypto').randomUUID())")
|
||||
fi
|
||||
uuid=$(printf '%s' "$uuid" | tr '[:upper:]' '[:lower:]')
|
||||
if [[ ! "$uuid" =~ ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$ ]]; then
|
||||
return 0
|
||||
fi
|
||||
printf 'legacy-system:%s:%s' "$startedAt" "$uuid"
|
||||
}
|
||||
|
||||
notify_api() {
|
||||
local title="$1"
|
||||
local content="$2"
|
||||
|
||||
+2
-2
@@ -377,7 +377,7 @@ clear_env() {
|
||||
handle_task_start() {
|
||||
local error_message=""
|
||||
if [[ $ID ]]; then
|
||||
local error=$(update_cron "\"$ID\"" "0" "$$" "$log_path" "$begin_timestamp")
|
||||
local error=$(update_cron "\"$ID\"" "0" "$$" "$log_path" "$begin_timestamp" "" "" "$execution_id")
|
||||
if [[ $error ]]; then
|
||||
error_message=", 任务状态更新失败(${error})"
|
||||
fi
|
||||
@@ -414,7 +414,7 @@ handle_task_end() {
|
||||
[[ "$diff_time" == 0 ]] && diff_time=1
|
||||
|
||||
if [[ $ID ]]; then
|
||||
local error=$(update_cron "\"$ID\"" "1" "$$" "$log_path" "$begin_timestamp" "$diff_time" "$exit_code")
|
||||
local error=$(update_cron "\"$ID\"" "1" "$$" "$log_path" "$begin_timestamp" "$diff_time" "$exit_code" "$execution_id")
|
||||
if [[ $error ]]; then
|
||||
error_message=", 状态更新失败(${error})"
|
||||
fi
|
||||
|
||||
@@ -126,6 +126,7 @@ format_params() {
|
||||
init_begin_time() {
|
||||
begin_time=$(format_time "$time_format" "$time")
|
||||
begin_timestamp=$(format_timestamp "$time_format" "$time")
|
||||
execution_id=$(create_legacy_system_execution_id "$begin_timestamp")
|
||||
}
|
||||
|
||||
import_config "$@"
|
||||
|
||||
+3
-2
@@ -488,6 +488,7 @@ main() {
|
||||
local time_format="%Y-%m-%d %H:%M:%S"
|
||||
local time=$(date "+$time_format")
|
||||
local begin_timestamp=$(format_timestamp "$time_format" "$time")
|
||||
local execution_id=$(create_legacy_system_execution_id "$begin_timestamp")
|
||||
|
||||
local begin_time=$(format_time "$time_format" "$time")
|
||||
|
||||
@@ -495,7 +496,7 @@ main() {
|
||||
eval echo -e "\#\# 开始执行... $begin_time\\\n" $cmd
|
||||
fi
|
||||
|
||||
[[ $ID ]] && update_cron "\"$ID\"" "0" "$$" "$log_path" "$begin_timestamp"
|
||||
[[ $ID ]] && update_cron "\"$ID\"" "0" "$$" "$log_path" "$begin_timestamp" "" "" "$execution_id"
|
||||
|
||||
case $p1 in
|
||||
update)
|
||||
@@ -558,7 +559,7 @@ main() {
|
||||
local end_time=$(format_time "$time_format" "$etime")
|
||||
local end_timestamp=$(format_timestamp "$time_format" "$etime")
|
||||
local diff_time=$(($end_timestamp - $begin_timestamp))
|
||||
[[ $ID ]] && update_cron "\"$ID\"" "1" "$$" "$log_path" "$begin_timestamp" "$diff_time"
|
||||
[[ $ID ]] && update_cron "\"$ID\"" "1" "$$" "$log_path" "$begin_timestamp" "$diff_time" "" "$execution_id"
|
||||
|
||||
if [[ "$p1" != "repo" ]] && [[ "$p1" != "raw" ]]; then
|
||||
eval echo -e "\\\n\#\# 执行结束... $end_time 耗时 $diff_time 秒 " $cmd
|
||||
|
||||
Reference in New Issue
Block a user