fix: deleting a cancelled order keeps its Tiny cancellation, and production without the real Tiny holds the sends
Some checks failed
Some checks failed
- Deleting an order removed its Tiny events, delivered or not: cancelled and deleted before the worker sent it (or with Tiny down), the order stayed open in Tiny. A real order's events now stay; only a test order's go. - With TINY_ADAPTER missing from the production stack, the simulator marked paid orders delivered with made-up receipts, and they had to be queued again by hand. Production now holds the sends (the outbox retries and Sentry is told); with the real Tiny back, the worker sends them at once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,12 +17,37 @@ from .scanning import ClamAV, scan_loop
|
||||
|
||||
load_secret_files()
|
||||
require_runtime()
|
||||
adapters = {'tiny': FakeTiny()}
|
||||
# Not yet confirmed against the client's account; see app/tiny.py.
|
||||
if os.environ.get('TINY_ADAPTER') == 'tiny':
|
||||
from .tiny import TinyOrders
|
||||
from .cnpj import company
|
||||
adapters['tiny'] = TinyOrders(names=company)
|
||||
# Production without the real Tiny (TINY_ADAPTER missing from the stack):
|
||||
# the sends wait and Sentry is told, instead of the simulator marking real
|
||||
# orders delivered with made-up receipts that had to be queued again by hand.
|
||||
HELD = 'Tiny desligado em produção (TINY_ADAPTER não é tiny): o envio espera'
|
||||
|
||||
|
||||
class HeldTiny:
|
||||
def deliver(self, event_key, payload):
|
||||
raise RuntimeError(HELD)
|
||||
|
||||
|
||||
def tiny_adapter(adapter=None, environment=None):
|
||||
"""The Tiny the outbox delivers to: the real one when configured; in
|
||||
production otherwise none (HeldTiny); elsewhere the simulator."""
|
||||
adapter = os.environ.get('TINY_ADAPTER') if adapter is None else adapter
|
||||
environment = os.environ.get('APP_ENV', 'local') if environment is None else environment
|
||||
if adapter == 'tiny':
|
||||
from .tiny import TinyOrders
|
||||
from .cnpj import company
|
||||
return TinyOrders(names=company)
|
||||
return HeldTiny() if environment == 'production' else FakeTiny()
|
||||
|
||||
|
||||
adapters = {'tiny': tiny_adapter()}
|
||||
|
||||
|
||||
def release_held(c):
|
||||
"""With the real Tiny back, the sends held meanwhile go at once instead
|
||||
of after their back-off."""
|
||||
return c.execute('''UPDATE dtf_local.outbox SET available_at=now() WHERE provider='tiny'
|
||||
AND delivered_at IS NULL AND last_error=%s''', (HELD,)).rowcount
|
||||
last_tick = 0.0
|
||||
last_cleanup = 0.0
|
||||
last_reconcile = 0.0
|
||||
@@ -199,6 +224,14 @@ class Health(BaseHTTPRequestHandler):
|
||||
|
||||
if __name__ == '__main__':
|
||||
observability.init('worker')
|
||||
if isinstance(adapters['tiny'], HeldTiny):
|
||||
logging.error('Worker started in production without the real Tiny: Tiny sends are held')
|
||||
elif not isinstance(adapters['tiny'], FakeTiny):
|
||||
try:
|
||||
with connect() as c:
|
||||
release_held(c)
|
||||
except Exception:
|
||||
logging.exception('Could not release the Tiny sends held while Tiny was off')
|
||||
scan_thread = threading.Thread(target=scan_loop,args=(storage,),daemon=True)
|
||||
scan_thread.start()
|
||||
render_thread = threading.Thread(target=render_loop,args=(storage,),daemon=True)
|
||||
|
||||
Reference in New Issue
Block a user