File size: 1,850 Bytes
8f82127
041d5cf
8f82127
 
 
 
041d5cf
f8a97e6
8f82127
041d5cf
f8a97e6
041d5cf
8f82127
e38ad60
f8a97e6
8f82127
 
041d5cf
f8a97e6
041d5cf
8f82127
f8a97e6
8f82127
 
041d5cf
f8a97e6
8f82127
041d5cf
8f82127
 
041d5cf
 
 
8f82127
 
 
 
f8a97e6
 
 
 
 
 
041d5cf
 
 
 
 
8f82127
f8a97e6
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
# app.py — Hugging Face Space entrypoint (FastAPI + background indexer)

import os
import threading
import time
from src.api import app as fastapi_app
from src.indexer import realtime_loop
from src.db import init_db

# ----------------------------------------------------------------------
# Default environment variables (override via HF Space Secrets/Settings)
# ----------------------------------------------------------------------
os.environ.setdefault("POLYGON_RPC", "https://polygon-rpc.com/")
os.environ.setdefault("DB_PATH", "/tmp/pol_indexer.sqlite")
os.environ.setdefault("SCHEMA_PATH", "/tmp/schema.sql")
os.environ.setdefault("CONFIRMATIONS", "12")

# ----------------------------------------------------------------------
# Indexer background loop wrapper
# ----------------------------------------------------------------------
def _run_indexer_forever():
    """Run the indexer loop forever with auto-retry on crash."""
    while True:
        try:
            print("[Indexer] Starting realtime loop...")
            realtime_loop()   # <- if your loop takes args, pass them here
        except Exception as e:
            print(f"[Indexer] crashed with error: {e}")
            time.sleep(10)

# ----------------------------------------------------------------------
# FastAPI app served by Hugging Face
# ----------------------------------------------------------------------
app = fastapi_app

@app.on_event("startup")
def _startup():
    """Initialize DB schema and launch the indexer thread."""
    try:
        init_db()  # make sure schema and tables exist
    except Exception as e:
        print(f"[Startup] init_db failed: {e}")

    t = threading.Thread(
        target=_run_indexer_forever,
        name="IndexerThread",
        daemon=True
    )
    t.start()
    print("[Startup] Indexer background thread launched")