Merge Critical Corrections plan 002: two API processes, worker drains its queue
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 6m11s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 59s
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 6m11s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 59s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -53,10 +53,13 @@ def cleanup():
|
||||
c.execute("DELETE FROM dtf_local.security_events WHERE created_at<now()-interval '30 days'")
|
||||
|
||||
def tick():
|
||||
"""Deliver at most one outbox job; True when there was one, False otherwise."""
|
||||
global last_tick
|
||||
processed = False
|
||||
with connect() as c:
|
||||
job = c.execute('SELECT * FROM dtf_local.outbox WHERE delivered_at IS NULL AND available_at <= now() ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1').fetchone()
|
||||
if job:
|
||||
processed = True
|
||||
try:
|
||||
receipt = adapters[job['provider']].deliver(job['event_key'], job['payload'])
|
||||
c.execute('UPDATE dtf_local.outbox SET delivered_at=now(), receipt=%s, attempts=attempts+1, last_error=NULL WHERE id=%s', (Jsonb(receipt),job['id']))
|
||||
@@ -64,6 +67,14 @@ def tick():
|
||||
delay = min(1800, 2**min(job['attempts']+1, 10))
|
||||
c.execute("UPDATE dtf_local.outbox SET attempts=attempts+1, last_error=%s, available_at=now() + %s * interval '1 second' WHERE id=%s", (str(exc),delay,job['id']))
|
||||
last_tick = time.monotonic()
|
||||
return processed
|
||||
|
||||
def drain(limit=50):
|
||||
"""Deliver up to `limit` outbox jobs in one pass, so a backlog leaves at
|
||||
once instead of one job per second (Critical Corrections item 9)."""
|
||||
for _ in range(limit):
|
||||
if not tick():
|
||||
return
|
||||
|
||||
def tiny_keepalive():
|
||||
"""Keep a connected Tiny authorised even while no orders are sent.
|
||||
@@ -118,7 +129,7 @@ def loop():
|
||||
global last_cleanup
|
||||
while True:
|
||||
try:
|
||||
tick()
|
||||
drain()
|
||||
if time.monotonic()-last_cleanup > 60:
|
||||
cleanup()
|
||||
# Approved quotes unpaid for 24 h expire on their own (front 9).
|
||||
|
||||
Reference in New Issue
Block a user