-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart.bat
More file actions
332 lines (298 loc) · 11.1 KB
/
start.bat
File metadata and controls
332 lines (298 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
@echo off
setlocal EnableDelayedExpansion
:: ============================================================
:: Metadata Editor FastAPI - Start Script (Windows)
:: ============================================================
:: Default configuration
set "PROJECT_DIR=%~dp0"
set "PROJECT_DIR=%PROJECT_DIR:~0,-1%"
set "VENV_DIR=%PROJECT_DIR%\.venv"
set "MAIN_FILE=%PROJECT_DIR%\main.py"
set "PID_FILE=%PROJECT_DIR%\logs\app.pid"
set "LOG_FILE=%PROJECT_DIR%\logs\app.log"
set "LOG_ERR_FILE=%PROJECT_DIR%\logs\app_err.log"
set "DEFAULT_HOST=0.0.0.0"
set "DEFAULT_PORT=8000"
:: Allow override via environment variables
if "%HOST%"=="" set "HOST=%DEFAULT_HOST%"
if "%PORT%"=="" set "PORT=%DEFAULT_PORT%"
if "%CONDA_ENV_NAME%"=="" set "CONDA_ENV_NAME=metadata-editor"
:: Internal state
set "PYTHON_EXEC="
set "ENV_SOURCE="
:: ============================================================
:: Parse arguments
:: ============================================================
if /I "%~1"=="--help" goto :show_help
if /I "%~1"=="-h" goto :show_help
if /I "%~1"=="--check" goto :run_checks
if not "%~1"=="" (
echo [ERROR] Unknown option: %~1
echo [ERROR] Use --help for usage information
exit /b 1
)
goto :main
:: ============================================================
:show_help
:: ============================================================
echo Metadata Editor FastAPI - Start Script (Windows)
echo.
echo Usage: %~nx0 [options]
echo.
echo Options:
echo --help, -h Show this help message
echo --check Run checks only without starting the application
echo.
echo Environment variables:
echo HOST Server host (default: 0.0.0.0)
echo PORT Server port (default: 8000)
echo STORAGE_PATH Path to data storage directory
echo CONDA_ENV_NAME Conda environment name to use (default: metadata-editor)
echo.
echo Python Environment Detection (in priority order):
echo 1. Conda environment named 'metadata-editor' (or %%CONDA_ENV_NAME%%)
echo 2. Currently active conda environment (CONDA_DEFAULT_ENV is set)
echo 3. Virtual environment (.venv\) if present
echo 4. System Python
echo.
echo Examples:
echo %~nx0 Start with default settings
echo set HOST=127.0.0.1 ^& %~nx0 Start on localhost only
echo set PORT=8000 ^& %~nx0 Start on port 8000
echo set CONDA_ENV_NAME=myenv ^& %~nx0 Use a custom conda environment name
echo.
goto :eof
:: ============================================================
:create_directories
:: ============================================================
if not exist "%PROJECT_DIR%\logs" (
mkdir "%PROJECT_DIR%\logs" >nul 2>&1
echo [INFO] Created logs directory
)
if not exist "%PROJECT_DIR%\jobs" (
mkdir "%PROJECT_DIR%\jobs" >nul 2>&1
echo [INFO] Created jobs directory
)
goto :eof
:: ============================================================
:detect_python
:: ============================================================
echo [INFO] Detecting Python environment...
:: --- 1. Try named conda environment ---
where conda >nul 2>&1
if !errorlevel! equ 0 (
echo [INFO] Conda found. Checking for environment: %CONDA_ENV_NAME%
conda env list 2>nul | findstr /C:"%CONDA_ENV_NAME%" >nul 2>&1
if !errorlevel! equ 0 (
echo [INFO] Found conda environment: %CONDA_ENV_NAME%
:: Resolve the actual python.exe path inside the conda env
for /f "delims=" %%P in ('conda run -n %CONDA_ENV_NAME% python -c "import sys, os; print(os.path.normpath(sys.executable))" 2^>nul') do (
set "PYTHON_EXEC=%%P"
)
if not "!PYTHON_EXEC!"=="" (
echo [SUCCESS] Using conda environment '%CONDA_ENV_NAME%'
echo [INFO] Python: !PYTHON_EXEC!
set "ENV_SOURCE=conda:%CONDA_ENV_NAME%"
goto :python_found
) else (
echo [WARNING] Could not resolve Python path in conda env '%CONDA_ENV_NAME%'
)
) else (
echo [WARNING] Conda environment '%CONDA_ENV_NAME%' not found
)
:: --- 2. Try active conda environment ---
if not "%CONDA_DEFAULT_ENV%"=="" (
echo [INFO] Active conda environment detected: %CONDA_DEFAULT_ENV%
for /f "delims=" %%P in ('python -c "import sys, os; print(os.path.normpath(sys.executable))" 2^>nul') do (
set "PYTHON_EXEC=%%P"
)
if not "!PYTHON_EXEC!"=="" (
"!PYTHON_EXEC!" -c "import uvicorn" >nul 2>&1
if !errorlevel! equ 0 (
echo [SUCCESS] Using active conda environment: %CONDA_DEFAULT_ENV%
set "ENV_SOURCE=conda-active:%CONDA_DEFAULT_ENV%"
goto :python_found
) else (
echo [WARNING] uvicorn not found in active conda env '%CONDA_DEFAULT_ENV%'
)
)
)
)
:: --- 3. Try .venv virtual environment ---
if exist "%VENV_DIR%\Scripts\python.exe" (
set "PYTHON_EXEC=%VENV_DIR%\Scripts\python.exe"
"%VENV_DIR%\Scripts\python.exe" -c "import uvicorn" >nul 2>&1
if !errorlevel! equ 0 (
echo [SUCCESS] Using virtual environment: %VENV_DIR%
set "ENV_SOURCE=venv"
goto :python_found
) else (
echo [WARNING] uvicorn not found in .venv - run: %VENV_DIR%\Scripts\pip install -r requirements.txt
set "PYTHON_EXEC="
)
)
:: --- 4. Try system Python ---
for %%P in (python python3) do (
where %%P >nul 2>&1
if !errorlevel! equ 0 (
%%P -c "import uvicorn" >nul 2>&1
if !errorlevel! equ 0 (
for /f "delims=" %%Q in ('%%P -c "import sys, os; print(os.path.normpath(sys.executable))" 2^>nul') do (
set "PYTHON_EXEC=%%Q"
)
echo [SUCCESS] Using system Python: !PYTHON_EXEC!
set "ENV_SOURCE=system"
goto :python_found
)
)
)
echo [ERROR] Could not find Python with uvicorn installed.
echo [ERROR] Please set up an environment using one of these options:
echo.
echo [ERROR] Option A - Conda (recommended for geospatial support):
echo [ERROR] conda create -n metadata-editor python=3.11 -y
echo [ERROR] conda activate metadata-editor
echo [ERROR] conda install -c conda-forge gdal fiona geopandas rasterio pyproj shapely -y
echo [ERROR] pip install -r requirements.txt
echo.
echo [ERROR] Option B - Virtual environment:
echo [ERROR] python -m venv .venv
echo [ERROR] .venv\Scripts\activate
echo [ERROR] pip install -r requirements.txt
exit /b 1
:python_found
goto :eof
:: ============================================================
:check_dependencies
:: ============================================================
echo [INFO] Checking dependencies...
if not exist "%MAIN_FILE%" (
echo [ERROR] Main application file not found: %MAIN_FILE%
exit /b 1
)
"%PYTHON_EXEC%" -c "import uvicorn, fastapi" >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Required packages (uvicorn, fastapi) not found in: %PYTHON_EXEC%
echo [ERROR] Run: pip install -r requirements.txt
exit /b 1
)
echo [SUCCESS] All dependencies are available
goto :eof
:: ============================================================
:check_env_config
:: ============================================================
echo [INFO] Checking environment configuration...
if exist "%PROJECT_DIR%\.env" (
echo [SUCCESS] Found .env configuration file
) else (
echo [WARNING] No .env file found - using default configuration
)
if not "%STORAGE_PATH%"=="" (
if not exist "%STORAGE_PATH%" (
echo [ERROR] STORAGE_PATH directory does not exist: %STORAGE_PATH%
echo [ERROR] Please create the directory or update your .env file
exit /b 1
)
echo [SUCCESS] STORAGE_PATH is valid: %STORAGE_PATH%
) else (
echo [WARNING] STORAGE_PATH not set - path validation is disabled
)
goto :eof
:: ============================================================
:is_app_running
:: ============================================================
if exist "%PID_FILE%" (
set /p EXISTING_PID=<"%PID_FILE%"
if "!EXISTING_PID!"=="" (
del /f "%PID_FILE%" >nul 2>&1
exit /b 1
)
tasklist /fi "PID eq !EXISTING_PID!" 2>nul | findstr /C:"!EXISTING_PID!" >nul 2>&1
if !errorlevel! equ 0 (
exit /b 0
) else (
del /f "%PID_FILE%" >nul 2>&1
exit /b 1
)
)
exit /b 1
:: ============================================================
:start_app
:: ============================================================
echo [INFO] Starting Metadata Editor FastAPI application...
echo [INFO] Host: %HOST%
echo [INFO] Port: %PORT%
echo [INFO] Python: %PYTHON_EXEC%
echo [INFO] Environment: %ENV_SOURCE%
echo [INFO] Log file: %LOG_FILE%
:: Use PowerShell Start-Process to launch uvicorn in the background and capture PID.
:: RedirectStandardOutput / RedirectStandardError require absolute paths.
powershell -NoProfile -Command ^
"$p = Start-Process -FilePath '%PYTHON_EXEC%' ^
-ArgumentList '-m','uvicorn','main:app','--host','%HOST%','--port','%PORT%','--log-level','info' ^
-WorkingDirectory '%PROJECT_DIR%' ^
-RedirectStandardOutput '%LOG_FILE%' ^
-RedirectStandardError '%LOG_ERR_FILE%' ^
-WindowStyle Hidden ^
-PassThru; ^
$p.Id | Out-File -FilePath '%PID_FILE%' -Encoding ascii -NoNewline"
if not exist "%PID_FILE%" (
echo [ERROR] Failed to start application or capture PID
echo [ERROR] Check log for details: %LOG_FILE%
exit /b 1
)
:: Wait briefly for the process to initialise
echo [INFO] Waiting for application to initialise...
timeout /t 3 /nobreak >nul
set /p APP_PID=<"%PID_FILE%"
tasklist /fi "PID eq %APP_PID%" 2>nul | findstr /C:"%APP_PID%" >nul 2>&1
if !errorlevel! equ 0 (
echo.
echo [SUCCESS] Application started successfully^^!
echo [SUCCESS] PID: %APP_PID%
echo [SUCCESS] URL: http://%HOST%:%PORT%
echo [SUCCESS] API Documentation: http://%HOST%:%PORT%/docs
echo [SUCCESS] Log file: %LOG_FILE%
echo.
echo [INFO] To stop the application run: stop.bat
) else (
echo [ERROR] Application failed to start. Check logs:
echo [ERROR] %LOG_FILE%
echo [ERROR] %LOG_ERR_FILE%
del /f "%PID_FILE%" >nul 2>&1
exit /b 1
)
goto :eof
:: ============================================================
:run_checks
:: ============================================================
echo [INFO] === Running checks only ===
call :create_directories
call :detect_python
if !errorlevel! neq 0 exit /b 1
call :check_dependencies
if !errorlevel! neq 0 exit /b 1
call :check_env_config
echo [SUCCESS] All checks passed^^!
goto :eof
:: ============================================================
:main
:: ============================================================
echo [INFO] === Metadata Editor FastAPI - Start Script (Windows) ===
echo [INFO] Project directory: %PROJECT_DIR%
call :is_app_running
if !errorlevel! equ 0 (
echo [WARNING] Application is already running (PID: %EXISTING_PID%)
echo [WARNING] Use stop.bat to stop it first
exit /b 1
)
call :create_directories
call :detect_python
if !errorlevel! neq 0 exit /b 1
call :check_dependencies
if !errorlevel! neq 0 exit /b 1
call :check_env_config
call :start_app
if !errorlevel! neq 0 exit /b 1
echo [SUCCESS] === Application startup completed ===
endlocal