fix: a request during a database restart answers 503, and the logs stay quiet

Sentry DTF-API-8/9 (stack restart, 16/11 09:15): the startup retry kept
the API up, but a board request in the ~7 s without the database got a
500, and the render thread logged its error every 2 s. A request that
hits psycopg.OperationalError now answers 503 "O servidor está
reiniciando" with Retry-After (the web does not report it); the render
thread warns once per outage and backs off; the worker's first outage
line and the audit's connection outage are warnings.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-16 09:32:50 -03:00
parent 00c7fa3dfa
commit bcba1fbe67
6 changed files with 64 additions and 8 deletions

View File

@@ -46,13 +46,14 @@ class WorkerDatabaseOutageTest(unittest.TestCase):
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, 'warning') as warning, \
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])
# A warning, not an error: a restart is expected (Sentry DTF-API-8).
self.assertEqual(warning.call_count, 1)
self.assertIn('Database unavailable', warning.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)
@@ -76,8 +77,32 @@ class AuditPersistenceOutageTest(unittest.TestCase):
auth.audit('first')
auth.audit('second')
auth.audit('third')
self.assertEqual(error.call_count, 2)
self.assertEqual(warning.call_count, 3)
# A connection outage: the event lines and, once a minute, a warning.
self.assertEqual(error.call_count, 0)
self.assertEqual(warning.call_count, 5)
def test_any_other_persistence_failure_is_an_error(self):
logger = auth.logging.getLogger('dtf.security')
with mock.patch.object(auth, 'connect', side_effect=RuntimeError('bad table')), \
mock.patch.object(auth.time, 'monotonic', return_value=100.0), \
mock.patch.object(logger, 'error') as error, \
mock.patch.object(logger, 'warning'):
auth.audit('first')
self.assertEqual(error.call_count, 1)
class RequestDuringOutageTest(unittest.TestCase):
def test_a_request_gets_503_with_retry_after(self):
"""Sentry DTF-API-9: the board answered 500 while the database restarted."""
import asyncio
from app import app as module
request = mock.Mock(method='GET', url=mock.Mock(path='/api/operator/board'))
with mock.patch.object(module.logging, 'warning') as warning:
response = asyncio.run(module.database_unavailable(request, psycopg.OperationalError('down')))
self.assertEqual(response.status_code, 503)
self.assertEqual(response.headers['retry-after'], '5')
self.assertIn(b'reiniciando', response.body)
self.assertEqual(warning.call_count, 1)
if __name__ == '__main__':