Files
chuan 5fad621130 fix: 通知程序改异步后台下载,hook 不再因下载慢阻塞
- exe 缺失时派生脱离 hook 的后台下载进程,立即返回并通过 systemMessage 汇报进度,不受 30s 超时影响
- curl -C - 断点续传,失败保留 .partial 下次接着下
- 原子 mkdir 锁防止多次触发重复下载;进程被硬杀的陈旧锁靠 .partial 闲置自愈
- systemMessage 用 ASCII(中文经 cmd 会被破坏成非法 JSON)
2026-06-24 01:18:41 +08:00

46 lines
2.0 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# ============================================================
# 下载地址(首次运行从这里拉取 notify.exe)—— 按需修改
DOWNLOAD_URL="https://git.pchuan.top/cc-tools/notify/releases/download/v1.0.0/notify.exe"
# ============================================================
DIR="$(cd "$(dirname "$0")" && pwd)"
BIN="$DIR/../bin"
EXE="$BIN/notify.exe"
PART="$BIN/notify.exe.partial"
LOCK="$BIN/notify.download.lock"
# 常规路径:exe 已就绪 -> 直接转发参数与 stdin(保持管道完整)
if [ -f "$EXE" ]; then
exec "$EXE" "$@"
fi
# ---- exe 缺失:绝不阻塞 hook ----
mkdir -p "$BIN" 2>/dev/null
# 自愈:上次下载进程被硬杀(关机/崩溃)可能留下陈旧锁,挡住所有重试。
# .partial 不存在(没真正开始)或 >2 分钟没增长(进程已死)则回收锁,让 -C - 续传。
if [ -d "$LOCK" ]; then
if [ ! -f "$PART" ]; then
rmdir "$LOCK" 2>/dev/null
elif [ -n "$(find "$PART" -mmin +2 2>/dev/null)" ]; then
rmdir "$LOCK" 2>/dev/null
fi
fi
# 原子锁:只有第一个 hook 派生唯一的后台下载进程;并发/后续 hook 抢锁失败
# -> 不重复下载,只汇报进度。下载脱离 hook 后台进行,不受 30s 超时影响。
if mkdir "$LOCK" 2>/dev/null; then
# 断点续传 -C -;失败保留 .partial 供下次续传;无论成败都释放锁
nohup sh -c "curl -fsSL -C - '$DOWNLOAD_URL' -o '$PART' && mv -f '$PART' '$EXE' && chmod +x '$EXE'; rmdir '$LOCK' 2>/dev/null" >/dev/null 2>&1 &
fi
# 已下载大小(取 .partial 字节数),汇报给 Claude
DLBYTES=0
[ -f "$PART" ] && DLBYTES=$(wc -c < "$PART" 2>/dev/null | tr -d ' ')
DLMB=$(awk "BEGIN{printf \"%.1f\", $DLBYTES/1048576}")
# exit 0 让 Claude 解析此 JSONsystemMessage 展示给用户,本次通知跳过
printf '{"suppressOutput":true,"systemMessage":"[Claude Code Notify] notifier not ready, downloading in background: %s MB / ~14 MB done. Works automatically once finished; this notification is skipped."}\n' "$DLMB"
exit 0