fix: 通知程序改异步后台下载,hook 不再因下载慢阻塞
- exe 缺失时派生脱离 hook 的后台下载进程,立即返回并通过 systemMessage 汇报进度,不受 30s 超时影响 - curl -C - 断点续传,失败保留 .partial 下次接着下 - 原子 mkdir 锁防止多次触发重复下载;进程被硬杀的陈旧锁靠 .partial 闲置自愈 - systemMessage 用 ASCII(中文经 cmd 会被破坏成非法 JSON)
This commit is contained in:
+49
-33
@@ -6,40 +6,56 @@ rem ============================================================
|
||||
setlocal
|
||||
set "BIN=%~dp0..\bin"
|
||||
set "EXE=%BIN%\notify.exe"
|
||||
set "PART=%BIN%\notify.exe.partial"
|
||||
set "LOCK=%BIN%\notify.download.lock"
|
||||
|
||||
rem only bootstrap on first run; the common path runs the exe directly (keeps piped stdin intact)
|
||||
if not exist "%EXE%" call :bootstrap
|
||||
rem hidden self-reinvocation: detached background downloader (see :downloader)
|
||||
if "%~1"=="__download" goto downloader
|
||||
|
||||
if exist "%EXE%" "%EXE%" %*
|
||||
endlocal
|
||||
exit /b
|
||||
|
||||
:bootstrap
|
||||
if not exist "%BIN%" mkdir "%BIN%" 2>nul
|
||||
set "TMP=%BIN%\notify.exe.%RANDOM%.tmp"
|
||||
rem mkdir is atomic; success = this process downloads, failure = someone else is downloading
|
||||
mkdir "%LOCK%" 2>nul
|
||||
if errorlevel 1 goto :waitdl
|
||||
rem double-check in case it just finished
|
||||
if exist "%EXE%" ( rmdir "%LOCK%" 2>nul & exit /b )
|
||||
rem download to temp then atomic rename, so no half-written exe is ever seen
|
||||
curl -fsSL "%DOWNLOAD_URL%" -o "%TMP%"
|
||||
if errorlevel 1 ( del "%TMP%" 2>nul & rmdir "%LOCK%" 2>nul & exit /b )
|
||||
move /y "%TMP%" "%EXE%" >nul
|
||||
rmdir "%LOCK%" 2>nul
|
||||
exit /b
|
||||
|
||||
:waitdl
|
||||
rem did not get the lock; wait for the exe to appear (up to ~60s)
|
||||
set /a _w=0
|
||||
:waitloop
|
||||
if exist "%EXE%" exit /b
|
||||
if %_w% geq 120 (
|
||||
rem timed out; a killed download may have left a stale lock, clear it for next time
|
||||
rmdir "%LOCK%" 2>nul
|
||||
exit /b
|
||||
rem common path: exe present -> run directly (keeps piped stdin intact)
|
||||
if exist "%EXE%" (
|
||||
"%EXE%" %*
|
||||
endlocal & exit /b
|
||||
)
|
||||
ping -n 2 127.0.0.1 >nul
|
||||
set /a _w+=1
|
||||
goto :waitloop
|
||||
|
||||
rem ---- exe missing: never block the hook ----
|
||||
rem kick off the download once (atomic mkdir lock); a detached worker survives the
|
||||
rem hook timeout. then report progress to Claude and return immediately.
|
||||
if not exist "%BIN%" mkdir "%BIN%" 2>nul
|
||||
|
||||
rem self-heal: if a previous worker was hard-killed (shutdown/crash) it may leave a
|
||||
rem stale lock that blocks all retries. reclaim it so -C - can resume the .partial.
|
||||
if exist "%LOCK%" call :reclaim
|
||||
|
||||
rem atomic lock: only the first hook spawns the worker; others fall through and just
|
||||
rem report progress -> no duplicate downloads even when hooks fire concurrently.
|
||||
mkdir "%LOCK%" 2>nul && start "" /b "%~f0" __download <nul >nul 2>&1
|
||||
|
||||
rem downloaded size so far (from the .partial file), shown as X.X MB
|
||||
set "DLBYTES=0"
|
||||
if exist "%PART%" for %%A in ("%PART%") do set "DLBYTES=%%~zA"
|
||||
set /a DLKB=DLBYTES/1024
|
||||
set /a DLMB=DLKB/1024
|
||||
set /a DLF=(DLKB*10/1024)%%10
|
||||
|
||||
rem exit 0 so Claude parses this JSON; systemMessage is shown to the user, the
|
||||
rem notification itself is skipped this time (suppressOutput hides raw stdout)
|
||||
echo {"suppressOutput":true,"systemMessage":"[Claude Code Notify] notifier not ready, downloading in background: %DLMB%.%DLF% MB / ~14 MB done. Works automatically once finished; this notification is skipped."}
|
||||
endlocal & exit /b 0
|
||||
|
||||
:downloader
|
||||
rem detached worker: resume-capable download (-C -), atomic install, always free lock.
|
||||
rem if curl fails (slow/flaky net), the .partial is kept and the next hook resumes it.
|
||||
curl -fsSL -C - "%DOWNLOAD_URL%" -o "%PART%"
|
||||
if not errorlevel 1 move /y "%PART%" "%EXE%" >nul 2>&1
|
||||
rmdir "%LOCK%" 2>nul
|
||||
endlocal & exit /b
|
||||
|
||||
:reclaim
|
||||
rem no .partial yet => worker died before downloading anything; reclaim immediately
|
||||
if not exist "%PART%" ( rmdir "%LOCK%" 2>nul & goto :eof )
|
||||
rem .partial idle for >120s => worker is dead (a live curl writes continuously); reclaim
|
||||
for /f "delims=" %%T in ('powershell -nop -c "[int]((Get-Date)-(Get-Item '%PART%').LastWriteTime).TotalSeconds" 2^>nul') do set "IDLE=%%T"
|
||||
if not defined IDLE goto :eof
|
||||
if %IDLE% geq 120 rmdir "%LOCK%" 2>nul
|
||||
goto :eof
|
||||
|
||||
Reference in New Issue
Block a user