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

84
tests/test_db_outage.py Normal file
View File

@@ -0,0 +1,84 @@
"""A short database outage during a redeploy: bounded retries, one report.
Run from the repository root: python3 -m unittest tests.test_db_outage -v.
The worker imports psycopg and boto3, so a host without the API image's
dependencies cannot import it; CI runs this in the api container.
"""
import unittest
from unittest import mock
import psycopg
from app import worker
from app.core import auth
class WorkerDatabaseOutageTest(unittest.TestCase):
class StopLoop(Exception):
pass
class Connection:
def __enter__(self):
return self
def __exit__(self, *exc_info):
return False
def execute(self, *args, **kwargs):
return self
def fetchone(self):
return None
def setUp(self):
worker.db_down_since = None
self.addCleanup(setattr, worker, 'db_down_since', None)
def test_one_error_and_one_info_with_growing_sleeps(self):
outage = psycopg.OperationalError('connection refused')
with mock.patch.object(worker, 'connect',
side_effect=[outage, outage, outage, self.Connection()]), \
mock.patch.object(worker, 'last_cleanup', worker.time.monotonic()), \
mock.patch.object(worker, 'cleanup'), \
mock.patch.object(worker, 'tiny_keepalive'), \
mock.patch.object(worker, 'reconcile_payments'), \
mock.patch.object(worker, 'resolve_names'), \
mock.patch.object(worker.mail, 'send_pending'), \
mock.patch.object(worker.time, 'sleep',
side_effect=[None, None, None, self.StopLoop]) as sleep, \
mock.patch.object(worker.logging, 'error') as error, \
mock.patch.object(worker.logging, 'info') as info, \
mock.patch.object(worker.logging, 'debug') as debug:
with self.assertRaises(self.StopLoop):
worker.loop()
self.assertEqual(error.call_count, 1)
self.assertIn('Database unavailable', error.call_args[0][0])
self.assertEqual(info.call_count, 1)
self.assertIn('Database back after', info.call_args[0][0])
self.assertEqual(debug.call_count, 2)
self.assertEqual([call.args[0] for call in sleep.call_args_list[:3]],
[1, 2, 4])
class AuditPersistenceOutageTest(unittest.TestCase):
def setUp(self):
auth._persistence_error_at = None
self.addCleanup(setattr, auth, '_persistence_error_at', None)
def test_persistence_error_is_logged_at_most_once_per_minute(self):
logger = auth.logging.getLogger('dtf.security')
outage = psycopg.OperationalError('database is down')
with mock.patch.object(auth, 'connect', side_effect=outage), \
mock.patch.object(auth.time, 'monotonic',
side_effect=[100.0, 130.0, 161.0]), \
mock.patch.object(logger, 'error') as error, \
mock.patch.object(logger, 'warning') as warning:
auth.audit('first')
auth.audit('second')
auth.audit('third')
self.assertEqual(error.call_count, 2)
self.assertEqual(warning.call_count, 3)
if __name__ == '__main__':
unittest.main()