fix: a redeploy's short database outage no longer takes the API down

Sentry DTF-API-2..7 (redeploy of 5a7fc0f, 13/11): the name db did not
resolve for about 12 s; the new API exited at startup and the worker and
audit() logged the error every second. The API now waits up to 90 s with
a backoff, the worker reports an outage once and backs off to 15 s, the
audit persistence error is logged at most once a minute, and api, site
and kanban update start-first so the old container serves meanwhile.

Built by Bobina.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-13 15:25:29 -03:00
parent 5a7fc0f378
commit 445d686f43
6 changed files with 142 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ import os
import threading
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
import psycopg
from psycopg.types.json import Jsonb
from . import mail, observability, unpaid
from .adapters import FakeTiny, LocalS3Storage, require_runtime
@@ -27,6 +28,7 @@ last_reconcile = 0.0
last_names = 0.0
names_thread = None
last_tiny_keepalive = 0.0
db_down_since = None
storage = LocalS3Storage()
scan_thread = None
render_thread = None
@@ -126,7 +128,8 @@ def resolve_names():
names_thread.start()
def loop():
global last_cleanup
global last_cleanup, db_down_since
delay = 1
while True:
try:
drain()
@@ -140,8 +143,22 @@ def loop():
tiny_keepalive()
reconcile_payments()
resolve_names()
except psycopg.OperationalError as exc:
if db_down_since is None:
db_down_since = time.monotonic()
logging.error('Database unavailable, retrying: %s', exc)
else:
logging.debug('Database still unavailable, retrying in %s s', delay)
time.sleep(delay)
delay = min(delay * 2, 15)
continue
except Exception:
logging.exception('Local worker tick failed')
else:
if db_down_since is not None:
logging.info('Database back after %s s', int(time.monotonic()-db_down_since))
db_down_since = None
delay = 1
time.sleep(1)
class Health(BaseHTTPRequestHandler):