Rama Chandar
07/17/2025, 11:08 AMMarvin
07/17/2025, 11:08 AMMarvin
07/17/2025, 11:09 AMbatch
REM Set Prefect Cloud API key
set PREFECT_API_KEY=your-api-key-here
2. Working Directory: NSSM should set the working directory explicitly. In NSSM configuration:
- Set "Application Path" to D:\PrefectServer\SharedLibs\venvbiteam\Scripts\python.exe
- Set "Startup Directory" to D:\PrefectServer\SharedLibs
- Set "Arguments" to -m prefect worker start --pool "Worker Pool" --work-queue "default"
3. Modified Batch File: Here's an improved version of your batch file:
batch
@echo off
REM -----------------------------------------------
REM Prefect Cloud worker startup script via NSSM
REM -----------------------------------------------
REM Set working directory to shared D drive location
cd /d "D:\PrefectServer\SharedLibs"
REM Set Prefect Cloud API key (replace with your actual key)
set PREFECT_API_KEY=your-api-key-here
REM Activate virtual environment
call "D:\PrefectServer\SharedLibs\venvbiteam\Scripts\activate.bat"
REM OPTIONAL: Ensure log directory exists
if not exist "D:\PrefectServer\Logs" mkdir "D:\PrefectServer\Logs"
REM Start Prefect Cloud worker using named pool and queue
python -m prefect worker start --pool "Worker Pool" --work-queue "default" > D:\PrefectServer\Logs\prefect_worker.log 2>&1
4. NSSM Configuration Steps:
batch
REM Install the service (run as administrator)
nssm install PrefectWorker "D:\PrefectServer\SharedLibs\venvbiteam\Scripts\python.exe"
nssm set PrefectWorker AppDirectory "D:\PrefectServer\SharedLibs"
nssm set PrefectWorker AppParameters "-m prefect worker start --pool \"Worker Pool\" --work-queue \"default\""
nssm set PrefectWorker AppEnvironment "PREFECT_API_KEY=your-api-key-here"
5. Troubleshooting Steps:
- Check the Windows Event Viewer for service-related errors
- Look at D:\PrefectServer\Logs\prefect_worker.log
- Make sure the service account has proper permissions to:
- Access the virtual environment
- Write to the logs directory
- Access the network for Prefect Cloud communication
6. Additional NSSM Settings to Consider:
- Set appropriate service recovery options:
batch
nssm set PrefectWorker AppRestartDelay 10000
nssm set PrefectWorker AppStopMethodConsole 15000
- Configure stdout/stderr redirection:
batch
nssm set PrefectWorker AppStdout "D:\PrefectServer\Logs\prefect_worker.log"
nssm set PrefectWorker AppStderr "D:\PrefectServer\Logs\prefect_worker_error.log"
Since you mentioned it works when running directly in the terminal but not as a service, the most likely issues are:
1. Missing environment variables (especially PREFECT_API_KEY)
2. Service account permissions
3. Python path resolution in the service context
Try implementing these changes and check the logs. If you're still having issues, please share:
1. Any error messages from the logs
2. The output of nssm get PrefectWorker AppEnvironment
3. Whether the service starts but fails, or fails to start at all