feat(tasks): T55-T58 implement background tasks for OpenRouter sync

- T55: Setup APScheduler with AsyncIOScheduler and @scheduled_job decorator
- T56: Implement hourly usage stats sync from OpenRouter API
- T57: Implement daily API key validation job
- T58: Implement weekly cleanup job for old usage stats
- Add usage_stats_retention_days config option
- Integrate scheduler with FastAPI lifespan events
- Add 26 unit tests for scheduler, sync, and cleanup tasks
- Add apscheduler to requirements.txt

The background tasks now automatically:
- Sync usage stats every hour from OpenRouter
- Validate API keys daily at 2 AM UTC
- Clean up old data weekly on Sunday at 3 AM UTC
This commit is contained in:
Luca Sacchi Ricciardi
2026-04-07 17:41:24 +02:00
parent 19a2c527a1
commit 3ae5d736ce
21 changed files with 3104 additions and 7 deletions

View File

@@ -160,11 +160,29 @@
- [ ] T53: Implementare router /keys
- [ ] T54: Aggiungere HTMX per azioni CRUD
### ⚙️ Background Tasks (T55-T58) - 0/4 completati
- [ ] T55: Configurare APScheduler
- [ ] T56: Implementare task sync usage stats
- [ ] T57: Implementare task validazione key
- [ ] T58: Integrare scheduler in startup app
### ⚙️ Background Tasks (T55-T58) - 4/4 completati
- [x] T55: Configurare APScheduler - ✅ Completato (2026-04-07 20:30)
- Creato: AsyncIOScheduler singleton con timezone UTC
- Creato: Decorator @scheduled_job per registrare task
- Integrato: FastAPI lifespan per startup/shutdown
- Test: 10 test passanti
- [x] T56: Implementare task sync usage stats - ✅ Completato (2026-04-07 20:30)
- Task: sync_usage_stats ogni ora (IntervalTrigger)
- Features: Decripta key, chiama OpenRouter /usage, upsert in UsageStats
- Rate limiting: 0.35s tra richieste (20 req/min)
- Date range: ultimi 7 giorni
- Test: 6 test passanti
- [x] T57: Implementare task validazione key - ✅ Completato (2026-04-07 20:30)
- Task: validate_api_keys giornaliero alle 2:00 AM (CronTrigger)
- Features: Decripta key, chiama OpenRouter /auth/key, disattiva key invalide
- Test: 4 test passanti
- [x] T58: Implementare task cleanup dati vecchi - ✅ Completato (2026-04-07 20:30)
- Task: cleanup_old_usage_stats settimanale domenica 3:00 AM
- Features: Rimuove UsageStats più vecchi di 365 giorni (configurabile)
- Test: 6 test passanti
**Progresso sezione:** 100% (4/4 task)
**Test totali tasks:** 26 test passanti
### 🔒 Sicurezza & Hardening (T59-T63) - 0/5 completati
- [ ] T59: Implementare security headers middleware