-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner_api_fastapi.py
More file actions
77 lines (61 loc) · 2.52 KB
/
scanner_api_fastapi.py
File metadata and controls
77 lines (61 loc) · 2.52 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
"""FastAPI replacement for scanner API endpoints (parallel to existing Flask app).
This file exposes key endpoints and uses the same worker/task-store/runtime
mechanisms but runs under Uvicorn/ASGI for a single async runtime.
"""
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
import uvicorn
import logging
from datetime import datetime
import threading
from workers.task_store import save_runtime, load_runtime
import workers.task_store as task_store
app = FastAPI()
logger = logging.getLogger(__name__)
@app.get('/health')
async def health():
return {'status': 'healthy', 'service': 'scanner-api-fastapi', 'timestamp': datetime.now().isoformat()}
@app.post('/api/scanner/scan')
async def trigger_scan(request: Request):
data = await request.json()
# determine parallel/exchanges
exchange_param = data.get('exchange', 'kucoinfutures')
parallel_mode = data.get('parallel', False)
if isinstance(exchange_param, list):
exchanges = exchange_param
parallel_mode = True
else:
exchanges = [exchange_param]
try:
from workers.tasks import run_scan_task
res = run_scan_task.apply_async(args=[data])
return JSONResponse({'status': 'accepted', 'task_id': res.id}, status_code=202)
except Exception:
# fallback: run scan synchronously in background thread
from scan_runner import run_scan
t = threading.Thread(target=run_scan, args=(data,), daemon=True)
t.start()
return JSONResponse({'status': 'accepted', 'task_id': None, 'note': 'background thread'}, status_code=202)
@app.get('/api/scanner/status')
async def get_status():
rt_ts = load_runtime('last_scan_timestamp')
rt_signals = load_runtime('last_scan_signals') or []
return {
'status': 'active',
'last_scan': rt_ts,
'results_count': len(rt_signals)
}
@app.post('/api/scanner/continuous/start')
async def start_continuous(body: dict):
symbols = body.get('symbols', [])
exchanges = body.get('exchanges', [])
config = body.get('config', {})
cmd = {'action': 'start', 'symbols': symbols, 'exchanges': exchanges, 'config': config}
save_runtime('continuous_scanner:command', cmd)
return {'status': 'queued'}
@app.post('/api/scanner/continuous/stop')
async def stop_continuous():
save_runtime('continuous_scanner:command', {'action': 'stop'})
return {'status': 'queued'}
if __name__ == '__main__':
uvicorn.run('scanner_api_fastapi:app', host='0.0.0.0', port=5001, reload=False)