Stop losing notifications on Olist/n8n hiccups and redeploys
- Olist lookups: 20s timeout so one hung request can't stall the serial queue; retry temporary failures (codigo_erro 6/11/35/99, timeouts, 5xx) with backoff instead of forwarding orders without phone or forma_envio. Log "[Notification at risk]" when that still happens. - n8n forwarding: timeout plus retries, only when n8n certainly didn't run the workflow (connection refused, 502/503, 404 while restarting), so retries can't send a customer the same WhatsApp twice. Stop logging the start of N8N_AUTH_TOKEN. - On SIGTERM, stop accepting requests and finish webhooks already acked to Tiny (up to SHUTDOWN_GRACE_MS, default 50s). - Keep stock_memory.json and stock_log.csv on the data-runtime volume so stock stats survive redeploys. - Add a node:test suite (npm test) covering the payload n8n reads, the status cache, burst queueing, retries, stock files and shutdown drain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
27
test/live-queue.test.ts
Normal file
27
test/live-queue.test.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { setTestEnv, mockAxiosPost, tinyOrder, fakeRes, orderWebhook, isTinyCall, silenceConsole } from './helpers';
|
||||
|
||||
// Real spacing between Olist calls, default max queue wait.
|
||||
setTestEnv({ TINY_LIVE_REQUEST_DELAY_MS: '250' });
|
||||
const { handleTinyOrderUpdate } = require('../src/controllers/webhook.controller');
|
||||
|
||||
const calls = mockAxiosPost(async (url, data) => {
|
||||
if (!isTinyCall(url)) return { status: 200, data: {} };
|
||||
return tinyOrder(new URLSearchParams(data.toString()).get('id')!);
|
||||
});
|
||||
|
||||
test('a burst of webhooks is queued and every order is forwarded with Olist data', async () => {
|
||||
const capture = silenceConsole();
|
||||
try {
|
||||
const ids = [1, 2, 3, 4, 5, 6, 7, 8].map(i => 2000 + i);
|
||||
await Promise.all(ids.map(id => handleTinyOrderUpdate(orderWebhook(id, 'pronto_envio', 'Pronto para envio'), fakeRes())));
|
||||
|
||||
const forwarded = calls.filter(c => !isTinyCall(c.url)).map(c => c.data);
|
||||
assert.equal(forwarded.length, ids.length);
|
||||
assert.ok(forwarded.every(p => p.fone && p.forma_envio === 'X'), 'every order keeps phone and forma_envio');
|
||||
assert.ok(!capture.warnings.some(w => w.includes('too busy')), 'no order skipped the Olist lookup');
|
||||
} finally {
|
||||
capture.restore();
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user