fix: optimize the stability of network pre-checks

This commit is contained in:
zhayujie
2026-04-13 10:35:38 +08:00
parent a989d088fd
commit 3649499dba

35
run.sh
View File

@@ -28,17 +28,24 @@ if [ -z "$BASH_VERSION" ]; then
exit 1 exit 1
fi fi
# Fallback for 'timeout' on macOS where GNU Coreutils is not installed by default. # Cross-platform timeout: prefer GNU timeout/gtimeout, fallback to a pure-bash implementation
# If 'gtimeout' (Homebrew's coreutils) is present, we use it. Otherwise, we gracefully # that uses background process + sleep to enforce a hard time limit.
# degrade by using `shift` to discard the time limit argument ($1) and executing the rest of the command. if command -v timeout &> /dev/null; then
if ! command -v timeout &> /dev/null; then _timeout() { timeout "$@"; }
timeout() { elif command -v gtimeout &> /dev/null; then
if command -v gtimeout &> /dev/null; then _timeout() { gtimeout "$@"; }
gtimeout "$@" else
else _timeout() {
shift local secs=$1; shift
"$@" "$@" &
fi local cmd_pid=$!
( sleep "$secs"; kill $cmd_pid 2>/dev/null ) &
local watcher_pid=$!
wait $cmd_pid 2>/dev/null
local exit_code=$?
kill $watcher_pid 2>/dev/null
wait $watcher_pid 2>/dev/null
return $exit_code
} }
fi fi
@@ -187,13 +194,13 @@ clone_project() {
else else
local clone_ok=false local clone_ok=false
# Test GitHub connectivity before attempting clone # Test GitHub connectivity before attempting clone
if curl -s --connect-timeout 5 --max-time 10 https://github.com > /dev/null 2>&1; then if curl -sI --connect-timeout 5 --max-time 10 https://github.com > /dev/null 2>&1; then
echo -e "${YELLOW}🌐 GitHub is reachable, cloning from GitHub...${NC}" echo -e "${YELLOW}🌐 GitHub is reachable, cloning from GitHub...${NC}"
timeout 15 git clone --depth 10 --progress https://github.com/zhayujie/CowAgent.git && clone_ok=true _timeout 15 git clone --depth 10 --progress https://github.com/zhayujie/CowAgent.git && clone_ok=true
fi fi
if [ "$clone_ok" = false ]; then if [ "$clone_ok" = false ]; then
echo -e "${YELLOW}⚠️ GitHub clone failed or timed out, switching to Gitee mirror...${NC}" echo -e "${YELLOW}⚠️ GitHub clone failed or timed out, switching to Gitee mirror...${NC}"
timeout 30 git clone --depth 10 --progress https://gitee.com/zhayujie/CowAgent.git && clone_ok=true _timeout 30 git clone --depth 10 --progress https://gitee.com/zhayujie/CowAgent.git && clone_ok=true
fi fi
if [ "$clone_ok" = false ]; then if [ "$clone_ok" = false ]; then
echo -e "${RED}❌ Project clone failed. Please check network connection.${NC}" echo -e "${RED}❌ Project clone failed. Please check network connection.${NC}"