@whyour/qinglong 2.22.0 → 2.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/back/protos/cron.proto +4 -0
  2. package/docs/development/node-cron-migration.md +49 -0
  3. package/docs/development/task-startup.md +78 -0
  4. package/docs/releases/2.22.0.md +8 -0
  5. package/docs/releases/2.23.0.md +33 -0
  6. package/docs/script-history.md +53 -0
  7. package/package.json +9 -8
  8. package/scripts/benchmark-script-history.cjs +39 -0
  9. package/scripts/publish-static.sh +34 -0
  10. package/scripts/sync-static.sh +20 -0
  11. package/scripts/wait-for-mirror.sh +214 -0
  12. package/shell/api.sh +56 -70
  13. package/shell/node_path_cache.sh +10 -2
  14. package/shell/otask.sh +7 -9
  15. package/shell/preload/sitecustomize.js +6 -2
  16. package/shell/preload/sitecustomize.py +4 -2
  17. package/shell/share.sh +1 -1
  18. package/shell/task-timeout.sh +63 -0
  19. package/shell/task.sh +8 -6
  20. package/shell/update.sh +40 -95
  21. package/shell/upgrade.cjs +8 -0
  22. package/static/build/api/config.js +2 -6
  23. package/static/build/api/env.js +15 -14
  24. package/static/build/api/open.js +2 -1
  25. package/static/build/api/script.js +155 -9
  26. package/static/build/api/subscription.js +3 -6
  27. package/static/build/api/system.js +35 -3
  28. package/static/build/config/grpcCerts.js +89 -40
  29. package/static/build/config/subscription.js +19 -11
  30. package/static/build/config/util.js +6 -4
  31. package/static/build/loaders/express.js +2 -0
  32. package/static/build/loaders/initData.js +20 -30
  33. package/static/build/loaders/initTask.js +11 -1
  34. package/static/build/middlewares/uniqueConstraintError.js +15 -0
  35. package/static/build/protos/cron.js +63 -2
  36. package/static/build/schedule/addCron.js +45 -63
  37. package/static/build/schedule/client.js +19 -1
  38. package/static/build/schedule/delCron.js +1 -1
  39. package/static/build/schedule/setConcurrency.js +27 -0
  40. package/static/build/services/config.js +2 -3
  41. package/static/build/services/cron.js +36 -10
  42. package/static/build/services/env.js +1 -1
  43. package/static/build/services/grpc.js +2 -1
  44. package/static/build/services/log.js +1 -1
  45. package/static/build/services/open.js +106 -31
  46. package/static/build/services/schedule.js +20 -10
  47. package/static/build/services/script.js +1 -1
  48. package/static/build/services/scriptHistory.js +30 -0
  49. package/static/build/services/sshKey.js +39 -14
  50. package/static/build/services/subscription.js +104 -53
  51. package/static/build/services/system.js +67 -32
  52. package/static/build/services/user.js +15 -10
  53. package/static/build/shared/cronCommand.js +17 -0
  54. package/static/build/shared/cronSchedule.js +101 -0
  55. package/static/build/shared/cronScheduler.js +73 -0
  56. package/static/build/shared/i18n.js +3 -2
  57. package/static/build/shared/logPath.js +15 -0
  58. package/static/build/shared/loginThrottle.js +45 -0
  59. package/static/build/shared/pLimit.js +19 -6
  60. package/static/build/shared/runCron.js +13 -4
  61. package/static/build/shared/scriptHistory.js +445 -0
  62. package/static/build/shared/shellQuote.js +11 -0
  63. package/static/build/shared/subscriptionMutationLock.js +34 -0
  64. package/static/build/shared/subscriptionPath.js +41 -0
  65. package/static/build/shared/subscriptionStorage.js +98 -0
  66. package/static/build/shared/utils.js +2 -1
  67. package/static/build/token.js +3 -14
  68. package/static/build/validation/schedule.js +3 -18
  69. package/static/dist/292.264757f0.async.js +1 -0
  70. package/static/dist/3512.95e41255.async.js +1 -0
  71. package/static/dist/index.html +2 -2
  72. package/static/dist/preload_helper.01fcd534.js +1 -0
  73. package/static/dist/src__pages__dependence__modal.0fa8b421.async.js +1 -0
  74. package/static/dist/src__pages__diff__index.b94e5dc5.async.js +1 -0
  75. package/static/dist/src__pages__script__editModal.afabf073.async.js +1 -0
  76. package/static/dist/src__pages__script__editNameModal.789ad87b.async.js +1 -0
  77. package/static/dist/src__pages__script__historyDrawer.8c58d958.async.js +1 -0
  78. package/static/dist/src__pages__script__historyDrawer.a449193a.chunk.css +1 -0
  79. package/static/dist/src__pages__script__index.ab360776.chunk.css +1 -0
  80. package/static/dist/src__pages__script__index.fc575945.async.js +1 -0
  81. package/static/dist/src__pages__script__saveModal.8fca1a22.async.js +1 -0
  82. package/static/dist/src__pages__script__saveWithHistory.99ece830.async.js +1 -0
  83. package/static/dist/src__pages__subscription__modal.30868ba8.async.js +1 -0
  84. package/static/dist/{umi.fc019050.js → umi.3a24a9fa.js} +1 -1
  85. package/test/back/advisory-input-security.test.cjs +416 -0
  86. package/test/back/code-scanning-security.test.cjs +189 -0
  87. package/test/back/cron-macros.test.cjs +51 -0
  88. package/test/back/cron-schedule-compatibility.test.cjs +69 -0
  89. package/test/back/cron-scheduler-routing.test.cjs +59 -0
  90. package/test/back/draft-path-security.test.cjs +235 -0
  91. package/test/back/env-import-security.test.cjs +91 -0
  92. package/test/back/execution-lifecycle.test.cjs +1 -1
  93. package/test/back/file-access-scope.test.cjs +180 -0
  94. package/test/back/grpc-mtls-security.test.cjs +136 -0
  95. package/test/back/http-security.test.cjs +105 -5
  96. package/test/back/legacy-cron-recovery.test.cjs +76 -0
  97. package/test/back/linux-mirror-result.test.cjs +176 -0
  98. package/test/back/listen-security.test.cjs +1 -0
  99. package/test/back/mirror-status.test.cjs +257 -0
  100. package/test/back/node-cron-migration.test.cjs +65 -0
  101. package/test/back/node-path-cache.test.cjs +29 -0
  102. package/test/back/node-path-lock.test.cjs +6 -2
  103. package/test/back/preload-lazy-notify.test.cjs +168 -0
  104. package/test/back/queued-cron-invalidation.test.cjs +98 -0
  105. package/test/back/restart-instance-recovery.test.cjs +116 -0
  106. package/test/back/scheduler-bulk-recovery.test.cjs +40 -0
  107. package/test/back/scheduler-concurrency.test.cjs +411 -0
  108. package/test/back/scheduler-reconciliation.test.cjs +58 -4
  109. package/test/back/script-file-access.test.cjs +210 -0
  110. package/test/back/script-history-api.test.cjs +551 -0
  111. package/test/back/script-history.test.cjs +383 -0
  112. package/test/back/shell-api-parsing.test.cjs +4 -4
  113. package/test/back/shell-translation-output.test.cjs +16 -0
  114. package/test/back/shell-upgrade.test.cjs +312 -0
  115. package/test/back/shellApiToken.test.cjs +151 -0
  116. package/test/back/ssh-config-home.test.cjs +113 -0
  117. package/test/back/static-mirror-trigger.test.cjs +441 -0
  118. package/test/back/static-mirror.test.cjs +179 -0
  119. package/test/back/stop-race.test.cjs +5 -1
  120. package/test/back/subscription-mutation-lock.test.cjs +83 -0
  121. package/test/back/subscription-review-regression.test.cjs +607 -0
  122. package/test/back/subscription-storage-regression.test.cjs +553 -0
  123. package/test/back/system-dependence-cache.test.cjs +1 -0
  124. package/test/back/system-export-history.test.cjs +119 -0
  125. package/test/back/systemTokenRenewal.test.cjs +497 -0
  126. package/test/back/task-shell-lifecycle.test.cjs +194 -0
  127. package/test/back/unique-constraint-error.test.cjs +72 -0
  128. package/test/back/upgrade-guards.test.cjs +285 -0
  129. package/test/back/upgrade-lifecycle.test.cjs +288 -0
  130. package/test/back/upgrade-transaction.test.cjs +133 -0
  131. package/test/back/user-security.test.cjs +102 -1
  132. package/test/back/wpush-grpc.test.cjs +1 -0
  133. package/test/fixtures/legacy-cron.json +1371 -0
  134. package/test/front/diff-file-routing.test.cjs +126 -0
  135. package/test/front/script-history-save.test.cjs +121 -0
  136. package/test/helpers/upgrade-fixture.cjs +30 -0
  137. package/version.yaml +14 -19
  138. package/static/dist/2096.6d98fd12.async.js +0 -1
  139. package/static/dist/4859.97d6653c.async.js +0 -1
  140. package/static/dist/6707.801b886b.async.js +0 -1
  141. package/static/dist/preload_helper.cf7de3fa.js +0 -1
  142. package/static/dist/src__pages__dependence__modal.a29c1a0f.async.js +0 -1
  143. package/static/dist/src__pages__diff__index.1714b941.async.js +0 -1
  144. package/static/dist/src__pages__script__editModal.22acdf7c.async.js +0 -1
  145. package/static/dist/src__pages__script__editNameModal.1a521cc0.async.js +0 -1
  146. package/static/dist/src__pages__script__index.60b6f827.chunk.css +0 -1
  147. package/static/dist/src__pages__script__index.a18b2692.async.js +0 -1
  148. package/static/dist/src__pages__script__saveModal.8965b74c.async.js +0 -1
  149. package/static/dist/src__pages__subscription__modal.38991e1e.async.js +0 -1
  150. /package/static/dist/{4859.1b962b29.chunk.css → 292.1b962b29.chunk.css} +0 -0
package/shell/api.sh CHANGED
@@ -26,6 +26,28 @@ get_token() {
26
26
  create_token
27
27
  }
28
28
 
29
+ # Check the cached token before every authenticated API request. A rejected
30
+ # token is refreshed and retried once; other failures are never retried.
31
+ # Arguments are curl options, including the URL, method and optional body.
32
+ ql_api_request() {
33
+ local api=""
34
+ local attempt
35
+ get_token || return $?
36
+ for attempt in 1 2; do
37
+ api=$(curl -s --noproxy "*" \
38
+ -H "Authorization: Bearer ${__ql_token__}" \
39
+ -H "Content-Type: application/json;charset=UTF-8" \
40
+ "$@" \
41
+ --compressed)
42
+ if [[ "$attempt" == 2 || "$api" == '{"code":200}' ]] ||
43
+ [[ "$(jq -r '.code' <<< "$api" 2>/dev/null)" != 401 ]]; then
44
+ break
45
+ fi
46
+ create_token || return $?
47
+ done
48
+ printf '%s' "$api"
49
+ }
50
+
29
51
  # Read the status response once; preserve multiline error messages and the
30
52
  # legacy code/message variables used by the shell API callers.
31
53
  ql_parse_status_response() {
@@ -63,13 +85,10 @@ add_cron_api() {
63
85
  sub_id="null"
64
86
  fi
65
87
 
66
- local api=$(
67
- curl -s --noproxy "*" "http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
68
- -H "Authorization: Bearer ${__ql_token__}" \
69
- -H "Content-Type: application/json;charset=UTF-8" \
70
- --data-raw "{\"name\":\"${name//\"/\\\"}\",\"command\":\"${command//\"/\\\"}\",\"schedule\":\"$schedule\",\"sub_id\":$sub_id}" \
71
- --compressed
72
- )
88
+ local api=$(ql_api_request \
89
+ "http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
90
+ --data-raw "{\"name\":\"${name//\"/\\\"}\",\"command\":\"${command//\"/\\\"}\",\"schedule\":\"$schedule\",\"sub_id\":$sub_id}" \
91
+ -X POST)
73
92
  code=$(echo "$api" | jq -r .code)
74
93
  message=$(echo "$api" | jq -r .message)
75
94
  if [[ $code == 200 ]]; then
@@ -93,14 +112,10 @@ update_cron_api() {
93
112
  local id="$4"
94
113
  fi
95
114
 
96
- local api=$(
97
- curl -s --noproxy "*" "http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
98
- -X 'PUT' \
99
- -H "Authorization: Bearer ${__ql_token__}" \
100
- -H "Content-Type: application/json;charset=UTF-8" \
101
- --data-raw "{\"name\":\"${name//\"/\\\"}\",\"command\":\"${command//\"/\\\"}\",\"schedule\":\"$schedule\",\"id\":\"$id\"}" \
102
- --compressed
103
- )
115
+ local api=$(ql_api_request \
116
+ "http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
117
+ -X PUT \
118
+ --data-raw "{\"name\":\"${name//\"/\\\"}\",\"command\":\"${command//\"/\\\"}\",\"schedule\":\"$schedule\",\"id\":\"$id\"}")
104
119
  code=$(echo "$api" | jq -r .code)
105
120
  message=$(echo "$api" | jq -r .message)
106
121
  if [[ $code == 200 ]]; then
@@ -120,14 +135,10 @@ update_cron_command_api() {
120
135
  local id="$2"
121
136
  fi
122
137
 
123
- local api=$(
124
- curl -s --noproxy "*" "http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
125
- -X 'PUT' \
126
- -H "Authorization: Bearer ${__ql_token__}" \
127
- -H "Content-Type: application/json;charset=UTF-8" \
128
- --data-raw "{\"command\":\"${command//\"/\\\"}\",\"id\":\"$id\"}" \
129
- --compressed
130
- )
138
+ local api=$(ql_api_request \
139
+ "http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
140
+ -X PUT \
141
+ --data-raw "{\"command\":\"${command//\"/\\\"}\",\"id\":\"$id\"}")
131
142
  code=$(echo "$api" | jq -r .code)
132
143
  message=$(echo "$api" | jq -r .message)
133
144
  if [[ $code == 200 ]]; then
@@ -140,14 +151,10 @@ update_cron_command_api() {
140
151
  del_cron_api() {
141
152
  local ids="$1"
142
153
  local currentTimeStamp=$(date +%s)
143
- local api=$(
144
- curl -s --noproxy "*" "http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
145
- -X 'DELETE' \
146
- -H "Authorization: Bearer ${__ql_token__}" \
147
- -H "Content-Type: application/json;charset=UTF-8" \
148
- --data-raw "[$ids]" \
149
- --compressed
150
- )
154
+ local api=$(ql_api_request \
155
+ "http://localhost:${ql_port}/open/crons?t=$currentTimeStamp" \
156
+ -X DELETE \
157
+ --data-raw "[$ids]")
151
158
  code=$(echo "$api" | jq -r .code)
152
159
  message=$(echo "$api" | jq -r .message)
153
160
  if [[ $code == 200 ]]; then
@@ -171,14 +178,10 @@ update_cron() {
171
178
  dataRaw="${dataRaw},\"exit_code\":$exitCode"
172
179
  fi
173
180
  dataRaw="${dataRaw}}"
174
- local api=$(
175
- curl -s --noproxy "*" "http://localhost:${ql_port}/open/crons/status?t=$currentTimeStamp" \
176
- -X 'PUT' \
177
- -H "Authorization: Bearer ${__ql_token__}" \
178
- -H "Content-Type: application/json;charset=UTF-8" \
179
- --data-raw "$dataRaw" \
180
- --compressed
181
- )
181
+ local api=$(ql_api_request \
182
+ "http://localhost:${ql_port}/open/crons/status?t=$currentTimeStamp" \
183
+ -X PUT \
184
+ --data-raw "$dataRaw")
182
185
  ql_parse_status_response "$api" || true
183
186
  if [[ $code != 200 ]]; then
184
187
  if [[ ! $message ]]; then
@@ -192,14 +195,10 @@ notify_api() {
192
195
  local title="$1"
193
196
  local content="$2"
194
197
  local currentTimeStamp=$(date +%s)
195
- local api=$(
196
- curl -s --noproxy "*" "http://localhost:${ql_port}/open/system/notify?t=$currentTimeStamp" \
197
- -X 'PUT' \
198
- -H "Authorization: Bearer ${__ql_token__}" \
199
- -H "Content-Type: application/json;charset=UTF-8" \
200
- --data-raw "{\"title\":\"${title//\"/\\\"}\",\"content\":\"${content//\"/\\\"}\"}" \
201
- --compressed
202
- )
198
+ local api=$(ql_api_request \
199
+ "http://localhost:${ql_port}/open/system/notify?t=$currentTimeStamp" \
200
+ -X PUT \
201
+ --data-raw "{\"title\":\"${title//\"/\\\"}\",\"content\":\"${content//\"/\\\"}\"}")
203
202
  code=$(echo "$api" | jq -r .code)
204
203
  message=$(echo "$api" | jq -r .message)
205
204
  if [[ $code == 200 ]]; then
@@ -212,12 +211,9 @@ notify_api() {
212
211
  find_cron_api() {
213
212
  local params="$1"
214
213
  local currentTimeStamp=$(date +%s)
215
- local api=$(
216
- curl -s --noproxy "*" "http://localhost:${ql_port}/open/crons/detail?$params&t=$currentTimeStamp" \
217
- -H "Authorization: Bearer ${__ql_token__}" \
218
- -H "Content-Type: application/json;charset=UTF-8" \
219
- --compressed
220
- )
214
+ local api=$(ql_api_request \
215
+ "http://localhost:${ql_port}/open/crons/detail?$params&t=$currentTimeStamp" \
216
+ -X GET)
221
217
  data=$(echo "$api" | jq -r .data)
222
218
  if [[ $data == 'null' ]]; then
223
219
  echo -e ""
@@ -231,14 +227,10 @@ update_auth_config() {
231
227
  local body="$1"
232
228
  local tip="$2"
233
229
  local currentTimeStamp=$(date +%s)
234
- local api=$(
235
- curl -s --noproxy "*" "http://localhost:${ql_port}/open/system/auth/reset?t=$currentTimeStamp" \
236
- -X 'PUT' \
237
- -H "Authorization: Bearer ${__ql_token__}" \
238
- -H "Content-Type: application/json;charset=UTF-8" \
239
- --data-raw "{$body}" \
240
- --compressed
241
- )
230
+ local api=$(ql_api_request \
231
+ "http://localhost:${ql_port}/open/system/auth/reset?t=$currentTimeStamp" \
232
+ -X PUT \
233
+ --data-raw "{$body}")
242
234
  code=$(echo "$api" | jq -r .code)
243
235
  message=$(echo "$api" | jq -r .message)
244
236
  if [[ $code == 200 ]]; then
@@ -254,14 +246,10 @@ record_cron_stat() {
254
246
  local elapsed="${3:-0}"
255
247
  [[ $ref_id ]] && [[ $ref_id -gt 0 ]] 2>/dev/null || return
256
248
 
257
- local api=$(
258
- curl -s --noproxy "*" "http://localhost:${ql_port:-5700}/open/dashboard/record" \
249
+ local api=$(ql_api_request \
250
+ "http://localhost:${ql_port:-5700}/open/dashboard/record" \
259
251
  -X POST \
260
- -H "Authorization: Bearer ${__ql_token__}" \
261
- -H "Content-Type: application/json;charset=UTF-8" \
262
- --data-raw "{\"ref_id\":$ref_id,\"code\":$exit_code,\"elapsed\":$elapsed}" \
263
- --compressed
264
- )
252
+ --data-raw "{\"ref_id\":$ref_id,\"code\":$exit_code,\"elapsed\":$elapsed}")
265
253
  ql_parse_status_response "$api" || true
266
254
  if [[ $code != 200 ]]; then
267
255
  if [[ ! $message ]]; then
@@ -270,5 +258,3 @@ record_cron_stat() {
270
258
  echo -e "${message}"
271
259
  fi
272
260
  }
273
-
274
- get_token
@@ -65,7 +65,9 @@ ql_read_node_path_cache() {
65
65
  # the caller's descriptors. A crashed refresher releases the kernel lock.
66
66
  ql_refresh_node_global_path() (
67
67
  local cache="$1" key="$2" now result previous_umask
68
- local lock="${cache}.lock"
68
+ # Keep one persistent lock per owner. Removing per-key lock files could
69
+ # split concurrent waiters across different inodes during cache cleanup.
70
+ local lock="${dir_tmp}/pnpm-root-${EUID}.lock"
69
71
  previous_umask=$(umask)
70
72
  umask 077
71
73
  if type -P flock &>/dev/null && mkdir -p -- "$dir_tmp" 2>/dev/null; then
@@ -93,6 +95,10 @@ ql_refresh_node_global_path() (
93
95
 
94
96
  # Private lock creation must not change pnpm's inherited creation mask.
95
97
  umask "$previous_umask"
98
+ # Warm lookups never scan the directory. Retire old environment records on
99
+ # refresh, while preserving the shared lock and unrelated cache files.
100
+ find "$dir_tmp" -maxdepth 1 -type f -user "$EUID" \
101
+ -name "pnpm-root-${EUID}-*.cache" -mmin +60 -delete 2>/dev/null || true
96
102
  now=${EPOCHSECONDS:-$(date +%s)}
97
103
  result=$(pnpm root -g 9>&- 2>/dev/null) || return $?
98
104
  # Never cache failed, empty, multiline or non-absolute answers.
@@ -119,8 +125,10 @@ ql_get_node_global_path() {
119
125
  fi
120
126
 
121
127
  local key cache
122
- cache="${dir_tmp}/pnpm-root-${EUID}.cache"
123
128
  key=$(ql_node_path_cache_key) || { pnpm root -g 2>/dev/null; return $?; }
129
+ # Node and crond may have different working directories/configuration.
130
+ # Preserve both discoveries instead of evicting each other on every run.
131
+ cache="${dir_tmp}/pnpm-root-${EUID}-${key// /-}.cache"
124
132
  if ql_read_node_path_cache "$cache" "$key"; then
125
133
  return 0
126
134
  fi
package/shell/otask.sh CHANGED
@@ -84,7 +84,9 @@ env_str_to_array() {
84
84
  }
85
85
 
86
86
  clear_non_sh_env() {
87
- if [[ $file_param != *.sh ]]; then
87
+ # Only automatic Node/Python preloads reconstruct the generated environment.
88
+ # Explicit interpreters and other commands must keep the shell-loaded values.
89
+ if [[ $isJsOrPythonFile == 'true' ]]; then
88
90
  clear_env
89
91
  fi
90
92
  }
@@ -162,7 +164,7 @@ run_normal() {
162
164
  if [[ $isJsOrPythonFile == 'false' ]]; then
163
165
  clear_non_sh_env
164
166
  fi
165
- $timeoutCmd $which_program $file_param "${script_params[@]}"
167
+ run_task_command $which_program $file_param "${script_params[@]}"
166
168
  }
167
169
 
168
170
  handle_env_split() {
@@ -201,7 +203,7 @@ run_concurrent() {
201
203
  export "${env_param}=${array[$i - 1]}"
202
204
  clear_non_sh_env
203
205
  fi
204
- eval envParam="${env_param}" numParam="${i}" $timeoutCmd $which_program $file_param "${script_params[@]}" &>$single_log_path &
206
+ eval envParam="${env_param}" numParam="${i}" run_task_command $which_program $file_param "${script_params[@]}" &>$single_log_path &
205
207
  done
206
208
 
207
209
  wait
@@ -241,7 +243,7 @@ run_designated() {
241
243
 
242
244
  enter_script_workdir
243
245
 
244
- envParam="${env_param}" numParam="${num_param}" $timeoutCmd $which_program $file_param "${script_params[@]}"
246
+ envParam="${env_param}" numParam="${num_param}" run_task_command $which_program $file_param "${script_params[@]}"
245
247
  }
246
248
 
247
249
  ## 运行其他命令
@@ -297,7 +299,7 @@ run_else() {
297
299
  fi
298
300
 
299
301
  clear_non_sh_env
300
- $timeoutCmd $which_program $file_param "$@"
302
+ run_task_command $which_program $file_param "$@"
301
303
  }
302
304
 
303
305
  check_file() {
@@ -334,10 +336,6 @@ check_nounset() {
334
336
 
335
337
  main() {
336
338
  if [[ $1 == *.js ]] || [[ $1 == *.mjs ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.sh ]] || [[ $1 == *.ts ]]; then
337
- if [[ $1 == *.sh ]]; then
338
- timeoutCmd=""
339
- fi
340
-
341
339
  case $# in
342
340
  1)
343
341
  run_normal "$1"
@@ -159,9 +159,13 @@ try {
159
159
 
160
160
  run();
161
161
 
162
- const { sendNotify } = require('./__ql_notify__.js');
163
162
  global.QLAPI = {
164
- notify: sendNotify,
163
+ notify(...args) {
164
+ // Keep notification dependencies out of ordinary script startup.
165
+ // require caches the module after the first notification.
166
+ const { sendNotify } = require('./__ql_notify__.js');
167
+ return sendNotify.apply(this, args);
168
+ },
165
169
  ...client,
166
170
  };
167
171
  } catch (error) {
@@ -129,10 +129,12 @@ try:
129
129
 
130
130
  run()
131
131
 
132
- from __ql_notify__ import send
133
-
134
132
  class BaseApi(Client):
135
133
  def notify(self, *args, **kwargs):
134
+ # Most scripts never send a notification. Python caches this
135
+ # module after the first call, including its channel dependencies.
136
+ from __ql_notify__ import send
137
+
136
138
  return send(*args, **kwargs)
137
139
 
138
140
  QLAPI = BaseApi()
package/shell/share.sh CHANGED
@@ -110,7 +110,7 @@ t() {
110
110
  fi
111
111
  [[ -z $msg ]] && msg="$key"
112
112
  # shellcheck disable=SC2059
113
- printf "$msg\n" "$@"
113
+ printf -- "$msg\n" "$@"
114
114
  }
115
115
 
116
116
  set_proxy() {
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Fork from the current Bash state: an external `bash -c` would lose private
4
+ # before-hook variables, arrays and functions used by sourced Shell scripts.
5
+ run_task_command() {
6
+ local timeout_seconds
7
+ if [[ -z ${command_timeout_time:-} ]]; then
8
+ "$@"
9
+ return $?
10
+ fi
11
+ timeout_seconds=$(awk -v value="$command_timeout_time" 'BEGIN {
12
+ if (value !~ /^([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[smhd]?$/) exit 1
13
+ unit = substr(value, length(value), 1)
14
+ factor = unit == "d" ? 86400 : unit == "h" ? 3600 : unit == "m" ? 60 : 1
15
+ printf "%.9f", (value + 0) * factor
16
+ }') || { printf 'Invalid task timeout: %s\n' "$command_timeout_time" >&2; return 125; }
17
+ if [[ $timeout_seconds == '0.000000000' ]]; then
18
+ "$@"
19
+ return $?
20
+ fi
21
+ (
22
+ timeout_dir=$(mktemp -d "${TMPDIR:-/tmp}/ql-task-timeout.XXXXXXXX") || exit 125
23
+ # Both children get private process groups. The timer and the wrapper must
24
+ # survive terminating the task, and descendants must not outlive a timeout.
25
+ set -m
26
+ ( set +m; "$@" ) <&0 &
27
+ timeout_worker=$!
28
+ (
29
+ set +m
30
+ : > "$timeout_dir/ready"
31
+ sleep "$timeout_seconds" || exit 125
32
+ : > "$timeout_dir/expired"
33
+ kill -TERM -- "-$timeout_worker" 2>/dev/null || :
34
+ sleep 0.5
35
+ kill -KILL -- "-$timeout_worker" 2>/dev/null || :
36
+ ) &
37
+ timeout_watcher=$!
38
+ set +m
39
+ trap 'kill -KILL -- "-$timeout_watcher" 2>/dev/null || :
40
+ if kill -TERM -- "-$timeout_worker" 2>/dev/null; then sleep 0.1; fi
41
+ kill -KILL -- "-$timeout_worker" 2>/dev/null || :
42
+ wait "$timeout_worker" 2>/dev/null || :
43
+ wait "$timeout_watcher" 2>/dev/null || :
44
+ rm -rf -- "$timeout_dir"' EXIT
45
+ trap 'exit 130' INT
46
+ trap 'exit 143' TERM
47
+ trap 'exit 129' HUP
48
+ trap 'exit 131' QUIT
49
+ # Do not clean up a timer whose process group has not initialized yet.
50
+ while [[ ! -f "$timeout_dir/ready" ]]; do
51
+ kill -0 "$timeout_watcher" 2>/dev/null || exit 125
52
+ sleep 0.001
53
+ done
54
+ wait "$timeout_worker"
55
+ timeout_code=$?
56
+ if [[ -f "$timeout_dir/expired" ]]; then
57
+ # Let the timer finish escalation even if the task leader exited first.
58
+ wait "$timeout_watcher" 2>/dev/null || :
59
+ exit 124
60
+ fi
61
+ exit "$timeout_code"
62
+ )
63
+ }
package/shell/task.sh CHANGED
@@ -3,6 +3,7 @@
3
3
  dir_shell=$QL_DIR/shell
4
4
  . $dir_shell/share.sh
5
5
  . $dir_shell/api.sh
6
+ . $dir_shell/task-timeout.sh
6
7
 
7
8
  trap 'single_hanle SIGINT' INT
8
9
  trap 'single_hanle SIGTERM' TERM
@@ -18,6 +19,12 @@ single_hanle() {
18
19
  exit 1
19
20
  }
20
21
 
22
+ run_shell_script() {
23
+ # Preserve inherited variables/functions, but contain exit, exec and shell
24
+ # options so the task wrapper can run after hooks and report completion.
25
+ ( . "$@" )
26
+ }
27
+
21
28
  define_program() {
22
29
  local file_param=$1
23
30
  if [[ $file_param == *.js ]] || [[ $file_param == *.mjs ]]; then
@@ -25,7 +32,7 @@ define_program() {
25
32
  elif [[ $file_param == *.py ]] || [[ $file_param == *.pyc ]]; then
26
33
  which_program="python3"
27
34
  elif [[ $file_param == *.sh ]]; then
28
- which_program="."
35
+ which_program="run_shell_script"
29
36
  elif [[ $file_param == *.ts ]]; then
30
37
  which_program="ts-node-transpile-only"
31
38
  else
@@ -106,11 +113,6 @@ format_params() {
106
113
  mtime_format="%Y-%m-%d %H:%M:%S.%3N"
107
114
  fi
108
115
  timeoutCmd=""
109
- if [[ $command_timeout_time ]]; then
110
- if type timeout &>/dev/null; then
111
- timeoutCmd="timeout --foreground -s 2 -k 10s $command_timeout_time "
112
- fi
113
- fi
114
116
  # params=$(echo "$@" | sed -E 's/([^ ])&([^ ])/\1\\\&\2/g')
115
117
 
116
118
  # 分割 task 内置参数和脚本参数
package/shell/update.sh CHANGED
@@ -283,98 +283,44 @@ usage() {
283
283
  }
284
284
 
285
285
  reload_qinglong() {
286
- echo -e "[reload_qinglong] deleting Triggered at $(date)" >>${dir_log}/reload.log
287
- sleep 3
288
- delete_pm2
289
- echo -e "[reload_qinglong] deleted Triggered at $(date)" >>${dir_log}/reload.log
290
-
291
286
  local reload_target="${1}"
292
- local primary_branch="master"
293
- if [[ "${QL_BRANCH}" == "develop" ]] || [[ "${QL_BRANCH}" == "debian" ]] || [[ "${QL_BRANCH}" == "debian-dev" ]]; then
294
- primary_branch="${QL_BRANCH}"
295
- fi
296
-
297
287
  if [[ "$reload_target" == 'system' ]]; then
298
- rm -rf ${dir_root}/back ${dir_root}/cli ${dir_root}/docker ${dir_root}/sample ${dir_root}/shell ${dir_root}/src
299
- mv -f ${dir_tmp}/qinglong-${primary_branch}/* ${dir_root}/
300
- rm -rf $dir_static/*
301
- mv -f ${dir_tmp}/qinglong-static-${primary_branch}/* ${dir_static}/
302
- cp -f $file_config_sample $dir_config/config.sample.sh
288
+ QlPort="${ql_port:-${QlPort:-5700}}" QlBaseUrl="${ql_base_url:-${QlBaseUrl:-/}}" node "$dir_shell/upgrade.cjs" reload
289
+ return $?
303
290
  fi
304
291
 
292
+ # Data import and a plain service restart retain their existing behavior.
293
+ echo -e "[reload_qinglong] deleting Triggered at $(date)" >>"${dir_log}/reload.log"
294
+ sleep 3
295
+ delete_pm2
305
296
  if [[ "$reload_target" == 'data' ]]; then
306
- rm -rf ${dir_data}/*
307
- mv -f ${dir_tmp}/data/* ${dir_data}/
297
+ rm -rf "${dir_data:?}"/*
298
+ mv -f "$dir_tmp"/data/* "$dir_data"/ || return $?
308
299
  fi
309
- echo -e "[reload_qinglong] starting Triggered at $(date)" >>${dir_log}/reload.log
310
300
  reload_pm2
311
- echo -e "[reload_qinglong] started Triggered at $(date)\n" >>${dir_log}/reload.log
312
301
  }
313
302
 
314
303
  ## 更新 qinglong
315
304
  update_qinglong() {
316
- rm -rf ${dir_tmp}/*
317
305
  local mirror="gitee"
318
- local downloadQLUrl="https://gitee.com/whyour/qinglong/repository/archive"
319
- local downloadStaticUrl="https://gitee.com/whyour/qinglong-static/repository/archive"
320
- local githubStatus=$(curl -s --noproxy "*" -m 2 -IL "https://google.com" | grep 200)
321
- if [[ ! -z $githubStatus ]]; then
306
+ if curl --fail --silent --noproxy "*" --max-time 2 --head "https://github.com" >/dev/null; then
322
307
  mirror="github"
323
- downloadQLUrl="https://github.com/whyour/qinglong/archive/refs/heads"
324
- downloadStaticUrl="https://github.com/whyour/qinglong-static/archive/refs/heads"
325
- fi
326
- t '使用 %s 源更新...\n' "${mirror}"
327
-
328
- local primary_branch="master"
329
- if [[ "${QL_BRANCH}" == "develop" ]] || [[ "${QL_BRANCH}" == "debian" ]] || [[ "${QL_BRANCH}" == "debian-dev" ]]; then
330
- primary_branch="${QL_BRANCH}"
331
- fi
332
-
333
- wget -cqO "${dir_tmp}/ql.zip" "${downloadQLUrl}/${primary_branch}.zip"
334
- exit_status=$?
335
-
336
- if [[ $exit_status -eq 0 ]]; then
337
- t '更新青龙源文件成功...\n'
338
-
339
- unzip -oq ${dir_tmp}/ql.zip -d ${dir_tmp}
340
-
341
- update_qinglong_static
342
- else
343
- t '更新青龙源文件失败,请检查网络...\n'
344
- fi
345
- }
346
-
347
- update_qinglong_static() {
348
- wget -cqO "${dir_tmp}/static.zip" "${downloadStaticUrl}/${primary_branch}.zip"
349
- exit_status=$?
350
-
351
- if [[ $exit_status -eq 0 ]]; then
352
- t '更新青龙静态资源成功...\n'
353
- unzip -oq ${dir_tmp}/static.zip -d ${dir_tmp}
354
-
355
- check_update_dep
356
- else
357
- t '更新青龙静态资源失败,请检查网络...\n'
358
308
  fi
309
+ t '使用 %s 源更新...\n' "$mirror"
310
+ QlPort="${ql_port:-${QlPort:-5700}}" QlBaseUrl="${ql_base_url:-${QlBaseUrl:-/}}" node "$dir_shell/upgrade.cjs" update "$mirror" "$needRestart"
359
311
  }
360
312
 
361
- check_update_dep() {
362
- t '\n开始检测依赖...\n'
363
- if [[ $(diff $dir_root/package.json ${dir_tmp}/qinglong-${primary_branch}/package.json) ]]; then
364
- npm_install_2 "${dir_tmp}/qinglong-${primary_branch}"
365
- fi
366
-
367
- if [[ $exit_status -eq 0 ]]; then
368
- t '\n依赖检测安装成功...\n'
369
- t '更新包下载成功...\n'
370
-
371
- if [[ "$needRestart" == 'true' ]]; then
372
- reload_qinglong "system"
373
- fi
313
+ # Preserve the updater's status through both tee and the final timing output.
314
+ run_update_logged() (
315
+ set -o pipefail
316
+ if [[ "$real_time" == 'true' ]]; then
317
+ "$@"
318
+ elif [[ "$no_tee" == 'true' ]]; then
319
+ "$@" >>"$file_path" 2>&1
374
320
  else
375
- t '\n依赖检测安装失败,请检查网络...\n'
321
+ "$@" 2>&1 | tee -a "$file_path"
376
322
  fi
377
- }
323
+ )
378
324
 
379
325
  ## 对比脚本
380
326
  diff_scripts() {
@@ -429,23 +375,19 @@ gen_list_repo() {
429
375
 
430
376
  cd ${repo_path}
431
377
 
432
- local cmd="find ."
433
- local index=0
434
- if [[ $6 ]]; then
435
- file_extensions="$6"
436
- if [[ $file_extensions =~ "|" ]]; then
437
- file_extensions=$(echo $file_extensions | sed 's/|/ /g')
378
+ local extension_string="${6:-$file_extensions}"
379
+ extension_string="${extension_string//|/ }"
380
+ extension_string="${extension_string//$'\n'/ }"
381
+ local -a extensions=() find_args=()
382
+ IFS=$' \t\n' read -r -a extensions <<< "$extension_string"
383
+ local extension
384
+ for extension in "${extensions[@]}"; do
385
+ if [[ ${#find_args[@]} -gt 0 ]]; then
386
+ find_args+=(-o)
438
387
  fi
439
- fi
440
- for extension in $file_extensions; do
441
- if [[ $index -eq 0 ]]; then
442
- cmd="${cmd} -name \"*.${extension}\""
443
- else
444
- cmd="${cmd} -o -name \"*.${extension}\""
445
- fi
446
- let index+=1
388
+ find_args+=(-name "*.${extension}")
447
389
  done
448
- files=$(eval $cmd | sed 's/^..//')
390
+ files=$(find . "${find_args[@]}" | sed 's/^..//')
449
391
  if [[ $path ]]; then
450
392
  files=$(echo "$files" | egrep "$path")
451
393
  fi
@@ -458,7 +400,7 @@ gen_list_repo() {
458
400
 
459
401
  if [[ $dependence ]]; then
460
402
  cd ${repo_path}
461
- results=$(eval $cmd | sed 's/^..//' | egrep "$dependence")
403
+ results=$(find . "${find_args[@]}" | sed 's/^..//' | egrep "$dependence")
462
404
  for _file in ${results}; do
463
405
  file_path=$(dirname $_file)
464
406
  make_dir "${dir_scripts}/${uniq_path}/${file_path}"
@@ -529,7 +471,7 @@ main() {
529
471
 
530
472
  if [[ "$p1" == "log" ]]; then
531
473
  show_service_logs "${@:2}"
532
- return $?
474
+ return 0
533
475
  fi
534
476
 
535
477
  local log_dir="${p1}"
@@ -558,14 +500,17 @@ main() {
558
500
 
559
501
  [[ $ID ]] && update_cron "\"$ID\"" "0" "$$" "$log_path" "$begin_timestamp"
560
502
 
503
+ local command_status=0
561
504
  case $p1 in
562
505
  update)
563
506
  fix_config
564
507
  local needRestart=${p2:-"true"}
565
- eval update_qinglong $cmd
508
+ run_update_logged update_qinglong
509
+ command_status=$?
566
510
  ;;
567
511
  reload)
568
- eval reload_qinglong "$p2" $cmd
512
+ run_update_logged reload_qinglong "$p2"
513
+ command_status=$?
569
514
  ;;
570
515
  extra)
571
516
  eval run_extra_shell $cmd
@@ -624,9 +569,9 @@ main() {
624
569
  if [[ "$p1" != "repo" ]] && [[ "$p1" != "raw" ]]; then
625
570
  eval echo -e "\\\n\#\# 执行结束... $end_time 耗时 $diff_time 秒     " $cmd
626
571
  fi
572
+ return "$command_status"
627
573
  }
628
574
 
629
575
  import_config "$@"
630
576
  main "$@"
631
-
632
- exit 0
577
+ exit $?
@@ -0,0 +1,8 @@
1
+ // The image updater and local CLI share one implementation and build contract.
2
+ const upgrade = require('../cli/src/internal/maintenance/upgradeArtifacts.cjs');
3
+ module.exports = upgrade;
4
+ if (require.main === module)
5
+ upgrade.main().catch((error) => {
6
+ console.error(`更新失败: ${error.message}`);
7
+ process.exitCode = 1;
8
+ });