fix: CNPJ name lookups run on their own thread, so a slow registry never delays orders
All checks were successful
Build and deploy / Validate source (push) Successful in 13s
Build and deploy / Integration suite on a real stack (push) Successful in 3m36s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m10s

The worker looked names up inside its main loop, which also sends orders to
Tiny and asks Mercado Pago about payments. Without internet, or with the
registry slow, five lookups could hold that loop for most of a minute.
They now run on a thread of their own, one batch at a time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-05 12:31:25 -03:00
parent b3c5f63553
commit 8997e22f5e
2 changed files with 14 additions and 5 deletions

View File

@@ -21,7 +21,7 @@ def digits(value):
def fetch(cnpj, transport=None):
"""The registry's names for one CNPJ. An unknown CNPJ has none, and is not asked again."""
with httpx.Client(timeout=10, transport=transport) as http:
with httpx.Client(timeout=8, transport=transport) as http:
response = http.get(API + cnpj, headers={'Accept': 'application/json', 'User-Agent': 'dtf-sistema'})
if response.status_code == 404:
return {'legal_name': None, 'trade_name': None}

View File

@@ -24,6 +24,7 @@ last_tick = 0.0
last_cleanup = 0.0
last_reconcile = 0.0
last_names = 0.0
names_thread = None
last_tiny_keepalive = 0.0
storage = LocalS3Storage()
scan_thread = None
@@ -90,13 +91,21 @@ def reconcile_payments():
payments.reconcile(connect, payment)
def resolve_names():
"""Every minute: the company names of a few new customers' CNPJs."""
global last_names
if time.monotonic()-last_names < 60:
"""Every minute: the company names of a few new customers' CNPJs, on a
thread of their own, so a slow registry never holds up orders."""
global last_names, names_thread
if time.monotonic()-last_names < 60 or (names_thread and names_thread.is_alive()):
return
last_names = time.monotonic()
from . import cnpj
cnpj.resolve(connect)
def run():
try:
cnpj.resolve(connect)
except Exception:
logging.exception('CNPJ name lookup failed')
names_thread = threading.Thread(target=run, daemon=True)
names_thread.start()
def loop():
global last_cleanup