install-service.bat 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. @echo off
  2. REM Register Bambuddy as a Windows service via NSSM.
  3. REM
  4. REM Called from Inno Setup's [Run] section. Arguments:
  5. REM %1 = install dir (e.g. C:\Program Files\Bambuddy)
  6. REM %2 = data dir (e.g. C:\ProgramData\Bambuddy)
  7. REM %3 = port (e.g. 8000)
  8. REM
  9. REM If the service already exists (re-install / upgrade), remove and
  10. REM re-create it so config changes from this build apply.
  11. setlocal
  12. set "INSTALL_DIR=%~1"
  13. set "DATA_ROOT=%~2"
  14. set "PORT=%~3"
  15. set "NSSM=%INSTALL_DIR%\bin\nssm.exe"
  16. set "PYTHON=%INSTALL_DIR%\python\python.exe"
  17. set "APP_DIR=%INSTALL_DIR%\app"
  18. set "BIN_DIR=%INSTALL_DIR%\bin"
  19. set "DATA_DIR=%DATA_ROOT%\data"
  20. set "LOG_DIR=%DATA_ROOT%\logs"
  21. REM Stop and remove any previous registration. Errors are non-fatal —
  22. REM "service not found" returns non-zero and we want to proceed.
  23. "%NSSM%" stop Bambuddy 2>nul
  24. "%NSSM%" remove Bambuddy confirm 2>nul
  25. REM Register the service. NSSM wraps uvicorn so Windows treats it as a
  26. REM proper service (autostart, recovery, supervised restart).
  27. REM --loop asyncio required: uvloop can truncate VP FTP uploads (#1896).
  28. REM --timeout-graceful-shutdown required: uvicorn otherwise waits forever for
  29. REM in-flight requests, and an MJPEG camera stream is a response that never
  30. REM completes — one open camera tile hangs the stop until NSSM force-kills,
  31. REM skipping the WAL checkpoint and the MQTT / virtual-printer teardown.
  32. "%NSSM%" install Bambuddy "%PYTHON%" "-m uvicorn backend.app.main:app --host 0.0.0.0 --port %PORT% --loop asyncio --timeout-graceful-shutdown 5"
  33. if errorlevel 1 (
  34. echo [install-service] nssm install failed
  35. exit /b 1
  36. )
  37. REM Service configuration
  38. "%NSSM%" set Bambuddy AppDirectory "%APP_DIR%"
  39. "%NSSM%" set Bambuddy DisplayName "Bambuddy"
  40. "%NSSM%" set Bambuddy Description "Bambuddy — local-first Bambu Lab printer manager"
  41. "%NSSM%" set Bambuddy Start SERVICE_AUTO_START
  42. REM Shutdown behaviour. NSSM's stop sequence is Ctrl-C, then WM_CLOSE, then a
  43. REM thread message, then TerminateProcess — each with a 1500 ms default wait.
  44. REM Uvicorn shuts down on the Ctrl-C, but needs longer than 1.5 seconds to
  45. REM finish: it drains in-flight requests (bounded at 5s by the flag above) and
  46. REM then runs the app teardown — WAL checkpoint, MQTT disconnect, virtual-
  47. REM printer stop. At the default timeout Windows force-killed it mid-teardown.
  48. REM
  49. REM Skip=6 drops the WM_CLOSE (2) and thread-message (4) methods: uvicorn is a
  50. REM console app with no window and no message loop, so both were only burning
  51. REM another 3 seconds before the kill. Ctrl-C is the one that works.
  52. "%NSSM%" set Bambuddy AppStopMethodSkip 6
  53. "%NSSM%" set Bambuddy AppStopMethodConsole 15000
  54. REM Environment: point DATA_DIR + LOG_DIR at ProgramData, prepend our
  55. REM bin/ to PATH so ffmpeg/ffprobe are found by the shutil.which() lookup
  56. REM in backend/app/services/layer_timelapse.py.
  57. "%NSSM%" set Bambuddy AppEnvironmentExtra ^
  58. "DATA_DIR=%DATA_DIR%" ^
  59. "LOG_DIR=%LOG_DIR%" ^
  60. "PORT=%PORT%" ^
  61. "PATH=%BIN_DIR%;%PATH%"
  62. REM Stdout / stderr capture. Rotate at 10MB.
  63. "%NSSM%" set Bambuddy AppStdout "%LOG_DIR%\service-stdout.log"
  64. "%NSSM%" set Bambuddy AppStderr "%LOG_DIR%\service-stderr.log"
  65. "%NSSM%" set Bambuddy AppRotateFiles 1
  66. "%NSSM%" set Bambuddy AppRotateOnline 1
  67. "%NSSM%" set Bambuddy AppRotateBytes 10485760
  68. REM Run as LocalSystem (default). Required for binding 322/990/8883 if
  69. REM the user later enables the Virtual Printer feature. Most non-VP
  70. REM workloads would work as a less-privileged account, but service
  71. REM identity changes are disruptive — pick the broader one once.
  72. REM Start the service. If it fails to start, NSSM exits non-zero and
  73. REM Inno Setup will surface this to the user.
  74. "%NSSM%" start Bambuddy
  75. if errorlevel 1 (
  76. echo [install-service] nssm start failed — check %LOG_DIR%\service-stderr.log
  77. exit /b 1
  78. )
  79. echo [install-service] Bambuddy service registered and started on port %PORT%
  80. endlocal
  81. exit /b 0