From a28cd165c4147a5caab4efb3e5c516a45480385f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Wed, 18 Nov 2026 10:56:29 -0300 Subject: [PATCH] fix: a delivery Tiny refuses reaches Sentry on its first failure and every tenth (order #42) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tiny refused #42 fourteen times ("Forma de recebimento não está habilitada": the customer-credit forma, CORREÇÃO DTF, is disabled in Dropstar's Tiny) and only Integrações showed it. The outbox now logs the refusal as an error on the first failure and every tenth retry, one message template so Sentry keeps one issue per kind. Co-Authored-By: Claude Opus 5.5 (1M context) --- app/worker.py | 8 ++++++++ tests/test_worker_drain.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/worker.py b/app/worker.py index d666575..277d3bc 100644 --- a/app/worker.py +++ b/app/worker.py @@ -78,6 +78,14 @@ def tick(): except Exception as exc: 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'])) + # Sentry hears of a refused delivery on its first failure and + # every tenth after, not only Integrações (the Tiny refused + # #42 fourteen times unseen, 2026-11-18). One message + # template, so Sentry keeps it as one issue per kind. + attempt = job['attempts'] + 1 + if attempt == 1 or attempt % 10 == 0: + logging.error('Outbox delivery to %s failed (%s, attempt %s): %s', + job['provider'], job['event_key'], attempt, str(exc)[:500]) last_tick = time.monotonic() return processed diff --git a/tests/test_worker_drain.py b/tests/test_worker_drain.py index 4dfef87..a66bcc4 100644 --- a/tests/test_worker_drain.py +++ b/tests/test_worker_drain.py @@ -23,5 +23,29 @@ class WorkerDrainTest(unittest.TestCase): self.assertEqual(tick.call_count, 50) + +class RefusedDeliveryTest(unittest.TestCase): + """A delivery the provider refuses reaches Sentry (logging.error) on its + first failure and every tenth after (#42, 2026-11-18).""" + def tick_with(self, attempts): + job = {'id': 1, 'provider': 'tiny', 'event_key': 'o:paid:tiny', 'payload': {}, 'attempts': attempts} + conn = mock.MagicMock() + conn.execute.return_value.fetchone.return_value = job + connect = mock.MagicMock() + connect.return_value.__enter__.return_value = conn + refusing = mock.MagicMock() + refusing.deliver.side_effect = RuntimeError('Forma de recebimento não está habilitada') + with mock.patch.object(worker, 'connect', connect), \ + mock.patch.dict(worker.adapters, {'tiny': refusing}), \ + mock.patch.object(worker.logging, 'error') as error: + self.assertTrue(worker.tick()) + return error + + def test_first_and_every_tenth_failure_are_reported(self): + self.assertEqual(self.tick_with(0).call_count, 1) + self.assertEqual(self.tick_with(9).call_count, 1) + self.assertEqual(self.tick_with(4).call_count, 0) + + if __name__ == '__main__': unittest.main()