|
1 | 1 | (defn run-process |
2 | 2 | ([argv] (run-process argv {})) |
3 | | - ([argv {:keys [dir env inherit?] :or {env {}}}] |
| 3 | + ([argv {:keys [dir env inherit? timeout-ms] :or {env {}}}] |
4 | 4 | (let [opts (cond-> {:env (merge (into {} (System/getenv)) env)} |
5 | 5 | dir (assoc :dir (str dir)) |
6 | 6 | inherit? (assoc :out :inherit :err :inherit) |
7 | 7 | (not inherit?) (assoc :out :string :err :string)) |
8 | | - result @(process/process argv opts)] |
9 | | - {:exit (:exit result) :out (or (:out result) "") :err (or (:err result) "")}))) |
| 8 | + proc (process/process argv opts) |
| 9 | + result (if (and timeout-ms (pos? timeout-ms)) |
| 10 | + (let [outcome (deref proc timeout-ms ::timeout)] |
| 11 | + (if (= outcome ::timeout) |
| 12 | + (do (process/destroy-tree proc) (deref proc) {:exit 124 :timed-out? true}) |
| 13 | + outcome)) |
| 14 | + @proc)] |
| 15 | + (cond-> {:exit (:exit result) :out (or (:out result) "") :err (or (:err result) "")} |
| 16 | + (:timed-out? result) (assoc :timed-out? true :err (str "timed out after " timeout-ms "ms")))))) |
10 | 17 |
|
11 | 18 | (defn run-checked [argv options label] |
12 | 19 | (let [result (run-process argv options)] |
|
62 | 69 | bin)))) |
63 | 70 |
|
64 | 71 | (defn write-progress [message] (binding [*out* *err*] (println (str "[ccusage-perf] " message)))) |
65 | | -(defn git-sha [directory] |
66 | | - (str/trim (:out (run-checked ["git" "rev-parse" "HEAD"] {:dir directory} "git rev-parse HEAD")))) |
| 72 | +(def curl-timeout-flags ["--connect-timeout" "10" "--max-time" "60"]) |
| 73 | +(defn git-sha [directory timeout-ms] |
| 74 | + (str/trim (:out (run-checked ["git" "rev-parse" "HEAD"] {:dir directory :timeout-ms timeout-ms} "git rev-parse HEAD")))) |
67 | 75 | (defn package-url-ready? [url] |
68 | | - (zero? (:exit (run-process ["curl" "--fail" "--head" "--location" "--silent" "--output" "/dev/null" url])))) |
| 76 | + (zero? (:exit (run-process (into ["curl" "--fail" "--head" "--location" "--silent"] |
| 77 | + (concat curl-timeout-flags ["--output" "/dev/null" url])))))) |
69 | 78 | (defn wait-for-package-url [url timeout-ms] |
70 | 79 | (let [deadline (+ (System/currentTimeMillis) timeout-ms)] |
71 | 80 | (loop [] |
|
83 | 92 | (json/generate-string {:private true :dependencies {:ccusage url}} {:pretty true})) |
84 | 93 | (write-progress (str label " package install started: " url)) |
85 | 94 | (let [started (System/currentTimeMillis) |
86 | | - result (run-process ["bun" "install" "--no-progress"] {:dir install-dir}) |
| 95 | + result (run-process ["bun" "install" "--no-progress"] {:dir install-dir :timeout-ms timeout-ms}) |
87 | 96 | elapsed (- (System/currentTimeMillis) started)] |
88 | 97 | (when-not (zero? (:exit result)) |
89 | 98 | (fail! (str label " package install failed: " |
|
98 | 107 | (defn packed-tarball-path [destination filename] |
99 | 108 | (if (fs/absolute? filename) filename (path destination filename))) |
100 | 109 |
|
101 | | -(defn packed-tarball-size [repo-dir] |
| 110 | +(defn packed-tarball-size [repo-dir timeout-ms] |
102 | 111 | (let [package-dir (path repo-dir "apps" "ccusage") |
103 | 112 | package-json-path (path package-dir "package.json") |
104 | 113 | original (slurp package-json-path) |
105 | 114 | destination (temp-dir "ccusage-pack.")] |
106 | 115 | (try |
107 | | - (let [result (run-process ["pnpm" "pack" "--json" "--pack-destination" (str destination)] {:dir package-dir})] |
| 116 | + (let [result (run-process ["pnpm" "pack" "--json" "--pack-destination" (str destination)] {:dir package-dir :timeout-ms timeout-ms})] |
108 | 117 | (when-not (zero? (:exit result)) |
109 | 118 | (fail! (str "pnpm pack failed: " (str/trim (or (not-empty (:err result)) (:out result)))) |
110 | 119 | {:kind :package-metadata-failed :exit (:exit result)})) |
|
120 | 129 | (defn remote-tarball-size [url] |
121 | 130 | (let [output (fs/create-temp-file {:prefix "ccusage-package."})] |
122 | 131 | (try |
123 | | - (run-checked ["curl" "--fail" "--location" "--silent" "--output" (str output) url] |
| 132 | + (run-checked (into ["curl" "--fail" "--location" "--silent"] |
| 133 | + (concat curl-timeout-flags ["--output" (str output) url])) |
124 | 134 | {} (str "Failed to fetch " url)) |
125 | 135 | (optional-file-size output) |
126 | 136 | (finally (fs/delete-if-exists output))))) |
|
0 commit comments