@ -25,6 +25,10 @@ logger = logging.getLogger(__name__)
# Maximum exhibit content size (5 MB)
MAX_EXHIBIT_SIZE = 5 * 1024 * 1024
# Limit concurrent background reindex tasks to prevent event-loop saturation
# when bulk scans trigger hundreds of simultaneous SEC EDGAR fetches.
_BACKGROUND_REINDEX_SEMAPHORE = asyncio . Semaphore ( 3 )
class SECFilingsService :
SUPPORTED_FORM_TYPES : Set [ str ] = {
@ -247,16 +251,17 @@ class SECFilingsService:
self , ticker : str , form_types : Optional [ Set [ str ] ] , lock : asyncio . Lock
) - > None :
""" Re-index a stale ticker in the background with its own DB session. """
async with lock :
try :
from app . core . database import AsyncSessionLocal
async with AsyncSessionLocal ( ) as session :
new_count = await self . index_filings (
session , ticker , form_types = form_types , skip_cache = True
)
logger . info ( f " Background reindex for { ticker } : { new_count } new filings " )
except Exception as e :
logger . warning ( f " Background reindex failed for { ticker } : { e } " )
async with _BACKGROUND_REINDEX_SEMAPHORE :
async with lock :
try :
from app . core . database import AsyncSessionLocal
async with AsyncSessionLocal ( ) as session :
new_count = await self . index_filings (
session , ticker , form_types = form_types , skip_cache = True
)
logger . info ( f " Background reindex for { ticker } : { new_count } new filings " )
except Exception as e :
logger . warning ( f " Background reindex failed for { ticker } : { e } " )
# ------------------------------------------------------------------
# search_filings: query DB with filters