-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_dev.ps1
More file actions
229 lines (192 loc) · 8.89 KB
/
start_dev.ps1
File metadata and controls
229 lines (192 loc) · 8.89 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
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Start crypto-rebal development server with optional features
.DESCRIPTION
Starts Uvicorn server with configurable options:
- Crypto-Toolbox: 0=Flask proxy (legacy), 1=FastAPI native (Playwright)
- Scheduler: Enable/disable periodic tasks (P&L snapshots, OHLCV updates)
- Reload: Enable/disable hot reload (auto-disabled with Scheduler or Playwright)
.PARAMETER CryptoToolboxMode
Crypto-Toolbox mode: 0=Flask proxy (legacy), 1=FastAPI native (new)
.PARAMETER EnableScheduler
Enable task scheduler (P&L snapshots, OHLCV updates, warmers)
Note: Disables hot reload to prevent double execution
.PARAMETER Reload
Enable hot reload (--reload flag). Auto-disabled if Scheduler or Playwright enabled.
.PARAMETER Port
Server port (default: 8080)
.PARAMETER Workers
(DEPRECATED: Single worker mode always used for Playwright compatibility)
.EXAMPLE
.\start_dev.ps1
# Start with FastAPI native, no scheduler, hot reload enabled
.EXAMPLE
.\start_dev.ps1 -EnableScheduler
# Start with scheduler enabled (no hot reload)
.EXAMPLE
.\start_dev.ps1 -CryptoToolboxMode 0 -Reload
# Start with Flask proxy and hot reload
.EXAMPLE
.\start_dev.ps1 -EnableScheduler -Port 8081
# Start with scheduler on custom port
#>
param(
[int]$CryptoToolboxMode = $(if ($env:CRYPTO_TOOLBOX_NEW) { [int]$env:CRYPTO_TOOLBOX_NEW } else { 1 }),
[switch]$EnableScheduler = $false,
[switch]$Reload = $false,
[int]$Port = 8080,
[int]$Workers = 1
)
# Check virtual environment exists
if (-not (Test-Path ".venv\Scripts\python.exe")) {
Write-Host "`n❌ Virtual environment not found!" -ForegroundColor Red
Write-Host " Please create it first:" -ForegroundColor Yellow
Write-Host " 1. python -m venv .venv" -ForegroundColor Gray
Write-Host " 2. .venv\Scripts\Activate.ps1" -ForegroundColor Gray
Write-Host " 3. pip install -r requirements.txt`n" -ForegroundColor Gray
exit 1
}
# Check and start Redis
Write-Host "🔍 Checking Redis..." -ForegroundColor Cyan
# Try localhost first
$redisRunning = Test-NetConnection -ComputerName localhost -Port 6379 -InformationLevel Quiet -WarningAction SilentlyContinue
$redisHost = "localhost"
if ($redisRunning) {
Write-Host "✅ Redis is running on localhost" -ForegroundColor Green
$env:REDIS_URL = "redis://localhost:6379/0"
Write-Host " Using REDIS_URL=$env:REDIS_URL" -ForegroundColor Gray
}
else {
# Try WSL2 Redis
try {
wsl --status 2>$null | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host " Starting Redis via WSL2..." -ForegroundColor Gray
# Start Redis (requires NOPASSWD sudoers config)
# To configure: wsl -d Ubuntu sudo visudo
# Add line: %sudo ALL=(ALL) NOPASSWD: /usr/sbin/service redis-server *
wsl -d Ubuntu bash -c "sudo service redis-server start" 2>$null
# Get WSL2 IP address
$wslIP = wsl -d Ubuntu hostname -I 2>$null | ForEach-Object { $_.Trim().Split()[0] }
if ($wslIP) {
Write-Host " WSL2 IP: $wslIP" -ForegroundColor Gray
# Wait for Redis to start and test WSL2 IP
Start-Sleep -Milliseconds 500
$redisRunning = Test-NetConnection -ComputerName $wslIP -Port 6379 -InformationLevel Quiet -WarningAction SilentlyContinue
if ($redisRunning) {
Write-Host "✅ Redis started on WSL2" -ForegroundColor Green
$redisHost = $wslIP
# Set REDIS_URL to WSL2 IP (accessible from Windows)
$env:REDIS_URL = "redis://${wslIP}:6379/0"
Write-Host " Using REDIS_URL=$env:REDIS_URL (WSL2 IP)" -ForegroundColor Gray
}
else {
Write-Host "⚠️ Redis not accessible - server will run in degraded mode" -ForegroundColor Yellow
}
}
}
else {
Write-Host "⚠️ WSL2 not available - Redis not started" -ForegroundColor Yellow
}
}
catch {
Write-Host "⚠️ Could not start Redis - continuing without it" -ForegroundColor Yellow
}
}
# Validate Playwright installation if using new mode
if ($CryptoToolboxMode -eq 1) {
Write-Host "🎭 Checking Playwright installation..." -ForegroundColor Cyan
$playwrightCheck = & .venv\Scripts\python.exe -c "try:
from playwright.async_api import async_playwright
print('OK')
except ImportError:
print('MISSING')" 2>$null
if ($playwrightCheck -ne "OK") {
Write-Host "❌ Playwright not installed!" -ForegroundColor Red
Write-Host " Install with: pip install playwright && playwright install chromium" -ForegroundColor Yellow
exit 1
}
Write-Host "✅ Playwright ready" -ForegroundColor Green
}
# Determine reload mode
$UseReload = $Reload -and -not $EnableScheduler -and ($CryptoToolboxMode -ne 1)
# Display configuration
Write-Host "`n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Blue
Write-Host "🚀 Starting Crypto Rebal Development Server" -ForegroundColor Cyan
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Blue
# Crypto-Toolbox mode
if ($CryptoToolboxMode -eq 1) {
Write-Host "📦 Crypto-Toolbox: FastAPI native (Playwright)" -ForegroundColor Green
}
else {
Write-Host "📦 Crypto-Toolbox: Flask proxy (legacy)" -ForegroundColor Yellow
Write-Host " ⚠️ Make sure Flask server is running on port 8001" -ForegroundColor Yellow
}
# Scheduler mode
if ($EnableScheduler) {
Write-Host "⏰ Task Scheduler: ENABLED" -ForegroundColor Green
Write-Host " • P&L snapshots (intraday 15min, EOD 23:59)" -ForegroundColor Gray
Write-Host " • OHLCV updates (daily 03:10, hourly :05)" -ForegroundColor Gray
Write-Host " • Staleness monitor (hourly :15)" -ForegroundColor Gray
Write-Host " • API warmers (every 10min)" -ForegroundColor Gray
Write-Host " • Crypto-Toolbox indicators (2x daily: 08:00, 20:00)" -ForegroundColor Gray
}
else {
Write-Host "⏰ Task Scheduler: DISABLED" -ForegroundColor Yellow
Write-Host " Run manual scripts for P&L/OHLCV updates" -ForegroundColor Gray
}
# Reload mode
if ($UseReload) {
Write-Host "🔄 Hot Reload: ENABLED" -ForegroundColor Green
}
else {
Write-Host "🔄 Hot Reload: DISABLED" -ForegroundColor Yellow
if ($EnableScheduler) {
Write-Host " (auto-disabled: prevents double execution with scheduler)" -ForegroundColor Gray
}
elseif ($CryptoToolboxMode -eq 1) {
Write-Host " (auto-disabled: required for Playwright on Windows)" -ForegroundColor Gray
}
}
Write-Host "`n🌐 Server: http://localhost:$Port" -ForegroundColor Cyan
Write-Host "📚 API Docs: http://localhost:$Port/docs" -ForegroundColor Cyan
Write-Host "🩺 Scheduler Health: http://localhost:$Port/api/scheduler/health" -ForegroundColor Cyan
Write-Host "👷 Workers: 1 (single worker mode for Playwright compatibility)" -ForegroundColor Cyan
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`n" -ForegroundColor Blue
# Set environment variables
$env:CRYPTO_TOOLBOX_NEW = $CryptoToolboxMode
if ($EnableScheduler) {
$env:RUN_SCHEDULER = "1"
Write-Host "✅ Environment: RUN_SCHEDULER=1" -ForegroundColor Green
}
else {
$env:RUN_SCHEDULER = "0"
}
# Start server
Write-Host "🔌 Checking if port $Port is available..." -ForegroundColor Cyan
Start-Sleep -Milliseconds 200 # Brief pause to allow ports to be released
$portInUse = $null
try {
$portInUse = Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue
}
catch {
# Ignore errors, assume port is free
}
if ($portInUse) {
Write-Host "`n❌ Port $Port is already in use by another process!" -ForegroundColor Red
Write-Host " Please stop the existing process or use a different port." -ForegroundColor Yellow
Write-Host " To find the process, run: Get-Process -Id (Get-NetTCPConnection -LocalPort $Port).OwningProcess" -ForegroundColor Gray
Write-Host " Example: .\start_dev.ps1 -Port 8001`n" -ForegroundColor Gray
exit 1
}
Write-Host "🚀 Starting Uvicorn...`n" -ForegroundColor Cyan
if ($UseReload) {
# For --reload, uvicorn uses single worker (--workers flag is incompatible)
& .venv\Scripts\python.exe -m uvicorn api.main:app --reload --port $Port
}
else {
# Without --reload, single worker mode for Playwright compatibility
# Start-Process -Wait ensures the PowerShell script doesn't exit prematurely.
Start-Process -FilePath ".venv\Scripts\python.exe" -ArgumentList "-m uvicorn api.main:app --port $Port" -NoNewWindow -Wait
}