install-service.bat 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "%NSSM%" install Bambuddy "%PYTHON%" "-m uvicorn backend.app.main:app --host 0.0.0.0 --port %PORT% --loop asyncio"
  29. if errorlevel 1 (
  30. echo [install-service] nssm install failed
  31. exit /b 1
  32. )
  33. REM Service configuration
  34. "%NSSM%" set Bambuddy AppDirectory "%APP_DIR%"
  35. "%NSSM%" set Bambuddy DisplayName "Bambuddy"
  36. "%NSSM%" set Bambuddy Description "Bambuddy — local-first Bambu Lab printer manager"
  37. "%NSSM%" set Bambuddy Start SERVICE_AUTO_START
  38. REM Environment: point DATA_DIR + LOG_DIR at ProgramData, prepend our
  39. REM bin/ to PATH so ffmpeg/ffprobe are found by the shutil.which() lookup
  40. REM in backend/app/services/layer_timelapse.py.
  41. "%NSSM%" set Bambuddy AppEnvironmentExtra ^
  42. "DATA_DIR=%DATA_DIR%" ^
  43. "LOG_DIR=%LOG_DIR%" ^
  44. "PORT=%PORT%" ^
  45. "PATH=%BIN_DIR%;%PATH%"
  46. REM Stdout / stderr capture. Rotate at 10MB.
  47. "%NSSM%" set Bambuddy AppStdout "%LOG_DIR%\service-stdout.log"
  48. "%NSSM%" set Bambuddy AppStderr "%LOG_DIR%\service-stderr.log"
  49. "%NSSM%" set Bambuddy AppRotateFiles 1
  50. "%NSSM%" set Bambuddy AppRotateOnline 1
  51. "%NSSM%" set Bambuddy AppRotateBytes 10485760
  52. REM Run as LocalSystem (default). Required for binding 322/990/8883 if
  53. REM the user later enables the Virtual Printer feature. Most non-VP
  54. REM workloads would work as a less-privileged account, but service
  55. REM identity changes are disruptive — pick the broader one once.
  56. REM Start the service. If it fails to start, NSSM exits non-zero and
  57. REM Inno Setup will surface this to the user.
  58. "%NSSM%" start Bambuddy
  59. if errorlevel 1 (
  60. echo [install-service] nssm start failed — check %LOG_DIR%\service-stderr.log
  61. exit /b 1
  62. )
  63. echo [install-service] Bambuddy service registered and started on port %PORT%
  64. endlocal
  65. exit /b 0