Setting the file. One moment. Progress Log · Wix Replatform · wix/skills · Skills Docs — line 97
This file
- Number
- 47.28
- Position
- 28 of 34
- Type
- Shell
- Size
- 4 KB
- Lines
- 140
lib/progress-log.sh
Shell·140 lines·4 KB
=
0
8progress_seq_state=""
9progress_started=0
10progress_terminal=0
11progress_heartbeat_pid=""
12progress_heartbeat_message="Still running"
13
14progress_slugify() {
15 printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//'
16}
17
18progress_init() {
19 if [[ -z "$progress_run_id" ]]; then
20 local slug
21 slug="$(progress_slugify "$progress_script")"
22 [[ -n "$slug" ]] || slug="script"
23 progress_run_id="${slug}-$(date +%s)-$(od -An -N2 -tx1 /dev/urandom | tr -d ' \n')"
24 fi
25 if [[ -z "$progress_seq_state" ]]; then
26 progress_seq_state="${TMPDIR:-/tmp}/rp-progress-${progress_run_id}.seq"
27 fi
28}
29
30progress_json_escape() {
31 local value="${1:-}"
32 value="${value//\\/\\\\}"
33 value="${value//\"/\\\"}"
34 value="${value//$'\n'/\\n}"
35 value="${value//$'\r'/\\r}"
36 value="${value//$'\t'/\\t}"
37 printf '%s' "$value"
38}
39
40progress_status_for_event() {
41 case "$1" in
42 complete) printf 'completed' ;;
43 error) printf 'failed' ;;
44 start|progress|heartbeat|warning) printf 'running' ;;
45 *) printf 'running' ;;
46 esac
47}
48
49progress_next_seq() {
50 progress_init
51 local lock="${progress_seq_state}.lock"
52 while ! mkdir "$lock" 2>/dev/null; do
53 sleep 0.05
54 done
55 local current=0
56 if [[ -f "$progress_seq_state" ]]; then
57 current="$(cat "$progress_seq_state")"
58 fi
59 progress_seq=$((current + 1))
60 printf '%s\n' "$progress_seq" > "$progress_seq_state"
61 rmdir "$lock"
62}
63
64progress_write() {
65 local event="$1"
66 local message="$2"
67 local fields="${3:-}"
68 [[ -n "$progress_log_path" ]] || return 0
69 progress_init
70 mkdir -p "$(dirname "$progress_log_path")"
71 progress_next_seq
72 local status ts line
73 status="$(progress_status_for_event "$event")"
74 ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
75 line="{\"ts\":\"$ts\",\"runId\":\"$(progress_json_escape "$progress_run_id")\""
76 if [[ -n "$progress_parent_run_id" ]]; then
77 line="$line,\"parentRunId\":\"$(progress_json_escape "$progress_parent_run_id")\""
78 fi
79 line="$line,\"script\":\"$(progress_json_escape "$progress_script")\",\"event\":\"$event\",\"status\":\"$status\",\"seq\":$progress_seq"
80 if [[ -n "$fields" ]]; then
81 line="$line,$fields"
82 fi
83 line="$line,\"message\":\"$(progress_json_escape "$message")\"}"
84 printf '%s\n' "$line" >> "$progress_log_path"
85 [[ "$event" == "start" ]] && progress_started=1
86 if [[ "$event" == "complete" || "$event" == "error" ]]; then
87 progress_terminal=1
88 progress_heartbeat_stop
89 fi
90}
91
92progress_start() { progress_write start "$1" "${2:-}"; }
93progress_progress() { progress_write progress "$1" "${2:-}"; }
94progress_heartbeat_line() { progress_write heartbeat "${1:-$progress_heartbeat_message}" "${2:-}"; }
95progress_warn() { progress_write warning "$1" "${2:-}"; }
96progress_error() { progress_write error "$1" "${2:-}"; }
97progress_complete() { progress_write complete "$1" "${2:-}"; }
98
99progress_heartbeat_start() {
100 local interval="${1:-30}"
101 progress_heartbeat_message="${2:-Still running}"
102 [[ -n "$progress_log_path" ]] || return 0
103 if [[ -n "$progress_heartbeat_pid" ]]; then
104 progress_error "heartbeat already active"
105 return 1
106 fi
107 (
108 while sleep "$interval"; do
109 progress_heartbeat_line "$progress_heartbeat_message"
110 done
111 ) &
112 progress_heartbeat_pid="$!"
113}
114
115progress_heartbeat_stop() {
116 if [[ -n "${progress_heartbeat_pid:-}" ]]; then
117 kill "$progress_heartbeat_pid" 2>/dev/null || true
118 wait "$progress_heartbeat_pid" 2>/dev/null || true
119 progress_heartbeat_pid=""
120 fi
121}
122
123progress_child_env() {
124 progress_init
125 if [[ -n "$progress_log_path" ]]; then
126 printf 'PROGRESS_LOG=%q PARENT_RUN_ID=%q ' "$progress_log_path" "$progress_run_id"
127 fi
128}
129
130progress_on_exit() {
131 local code=$?
132 progress_heartbeat_stop
133 if [[ "$progress_started" == "1" && "$progress_terminal" == "0" && "$code" != "0" ]]; then
134 progress_error "Process exited before writing a terminal progress record" "\"exitCode\":$code"
135 fi
136}
137
138trap progress_on_exit EXIT
139trap 'progress_heartbeat_stop; exit 130' INT
140trap 'progress_heartbeat_stop; exit 143' TERM