You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: prevent duplicate log clearing tasks with Redis locks and optimistic UI updates
This commit addresses the reviewer's concern about duplicate task submissions
by implementing a multi-layered prevention strategy.
## Backend improvements:
1. **Redis-based task deduplication**
- Added Redis lock mechanism in ConversationService.clear_conversations()
- Lock key format: `clear_conversations:{app_id}:{mode}[:{conversation_ids_hash}]`
- Lock expires in 10 minutes, preventing stale locks
- Returns 409 Conflict if clearing task already in progress
2. **New exception handling**
- Added ConversationClearInProgressError exception
- Controllers return 409 status with clear error message
- Lock is automatically released on task completion or max retries
3. **Celery task improvements**
- Task accepts lock_key parameter for cleanup
- Releases lock on successful completion
- Releases lock after max retries to prevent deadlock
## Frontend improvements:
1. **Optimistic UI updates**
- List immediately cleared from UI when user clicks "Clear All"
- For selective deletion, removed items are filtered out instantly
- Prevents user confusion and repeat submissions
2. **Improved user experience**
- No need to wait for backend processing to see results
- Clear visual feedback that operation started
- Backend revalidation happens after optimistic update
## How it works:
1. User clicks "Clear All" → List immediately disappears (optimistic update)
2. Frontend sends DELETE request → Backend checks Redis lock
3. If lock exists → Return 409 error (task already running)
4. If no lock → Acquire lock and queue Celery task
5. Celery task processes deletion in background
6. Task releases lock when done or after max retries
7. Frontend revalidates data after request completes
This prevents duplicate tasks even in edge cases like:
- Cross-tab submissions
- Slow network + multiple clicks
- Page refresh during operation
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
0 commit comments