refactor: implement exponential backoff patterns and extract magic numbers to constants

- Extract HTTP timeout constant (60s for large file downloads)
- Extract S3/MinIO retry configuration (3 retries, 1s-8s exponential backoff)
- Extract database polling retry configuration (5 retries, 100ms-2s exponential backoff)
- Extract webhook and lock polling delays to named constants
- Extract cache TTL values (5min for messages, 30min for updates) in Baileys service
- Implement exponential backoff for S3/MinIO downloads following webhook controller pattern
- Implement exponential backoff for database polling removing fixed delays
- Add deletion event lock to prevent race conditions with duplicate webhooks
- Process deletion events immediately (no delay) to fix Chatwoot local storage red error
- Make i18n translations path configurable via TRANSLATIONS_BASE_DIR env variable
- Add detailed logging for deletion events debugging

Addresses code review suggestions from Sourcery AI and Copilot AI:
- Magic numbers extracted to well-documented constants
- Retry configurations consolidated and clearly separated by use case
- S3/MinIO retry uses longer delays (external storage)
- Database polling uses shorter delays (internal operations)
- Fixes Chatwoot local storage deletion error (red message issue)
- Maintains full compatibility with S3/MinIO storage (tested)

Breaking changes: None - all changes are internal improvements
This commit is contained in:
Anderson Silva
2025-10-06 15:10:38 -03:00
parent 6e1d027750
commit e13434804c
3 changed files with 111 additions and 35 deletions

View File

@@ -3,9 +3,19 @@ import fs from 'fs';
import i18next from 'i18next';
import path from 'path';
// Detect if running from dist/ (production) or src/ (development)
const isProduction = fs.existsSync(path.join(process.cwd(), 'dist'));
const baseDir = isProduction ? 'dist' : 'src/utils';
// Make translations base directory configurable via environment variable
const envBaseDir = process.env.TRANSLATIONS_BASE_DIR;
let baseDir: string;
if (envBaseDir) {
// Use explicitly configured base directory
baseDir = envBaseDir;
} else {
// Fallback to auto-detection if env variable is not set
const isProduction = fs.existsSync(path.join(process.cwd(), 'dist'));
baseDir = isProduction ? 'dist' : 'src/utils';
}
const translationsPath = path.join(process.cwd(), baseDir, 'translations');
const languages = ['en', 'pt-BR', 'es'];