Skip to content

Task 2-2: Alert khi Queue Lag Vượt Ngưỡng

Phase: 2 - Stability Priority: Medium Module: batch Depends on: task-2-1 Reference: docs/BountyHunter-Backend/details/feature-game-room-management/SPEC.md

Background

Room creation enqueue vào prize allocation queues. Nếu queue lag cao, room creation sẽ bị delay nhưng không có alert nào.

Tasks

DI Note (quyết định thiết kế): Có 2 lựa chọn: - Option A: Thêm method vào BatchQueueMonitorService hiện có (nếu đã có getQueueDepth() method) - Option B: Tạo RoomQueueMonitorService mới annotated với @Service (nếu BatchQueueMonitorService quá lớn hoặc không có sẵn getQueueDepth())

Kiểm tra BatchQueueMonitorService trước, chọn option phù hợp. Nếu tạo class mới, cần @EnableScheduling ở Application class (kiểm tra xem đã có chưa).

  • [ ] Kiểm tra BatchQueueMonitorService xem có method getQueueDepth(String queue) hay tương đương không
  • [ ] Thêm threshold-based alert cho các queues liên quan đến room:

    // Trong BatchQueueMonitorService hoặc RoomQueueMonitorService
    // Clarification: PRIZE_FOR_STREAMER dùng cùng threshold với PRIZE vì cả 2 đều xử lý paid prizes
    private static final long PRIZE_QUEUE_LAG_THRESHOLD = 50;
    private static final long UNLIMITED_QUEUE_LAG_THRESHOLD = 200;
    
    @Scheduled(fixedDelay = 30000)  // check mỗi 30 giây
    public void checkRoomQueues() {
        checkQueueLag("queue-prize-allocation-PRIZE", PRIZE_QUEUE_LAG_THRESHOLD);
        checkQueueLag("queue-prize-allocation-PRIZE_FOR_STREAMER", PRIZE_QUEUE_LAG_THRESHOLD);
        checkQueueLag("queue-prize-allocation-UNLIMITED", UNLIMITED_QUEUE_LAG_THRESHOLD);
    }
    
    private void checkQueueLag(String queue, long threshold) {
        Long depth = getQueueDepth(queue);  // reuse method hiện có hoặc implement mới
        if (depth != null && depth > threshold) {
            LOGGER.warn("[QUEUE_LAG] Queue {} depth={} exceeds threshold={}", queue, depth, threshold);
        }
    }
    

  • [ ] Nếu tạo RoomQueueMonitorService mới: đảm bảo inject JmsTemplate hoặc connection factory tương tự như BatchQueueMonitorService để lấy queue depth

Verification / Acceptance Criteria

  • [ ] @Scheduled(fixedDelay = 30000) được trigger mỗi 30 giây (verify qua log)
  • [ ] Khi queue queue-prize-allocation-PRIZE depth > 50, log [QUEUE_LAG] xuất hiện với đúng queue name và depth
  • [ ] Khi queue queue-prize-allocation-UNLIMITED depth > 200, log [QUEUE_LAG] xuất hiện
  • [ ] Khi queue depth dưới threshold, không có log warning
  • [ ] Nếu getQueueDepth() trả về null (queue không tồn tại hoặc lỗi kết nối), không throw exception — method bỏ qua silently
  • [ ] Service compile và start được mà không lỗi (scheduling bean được đăng ký)
  • [ ] Unit test: mock getQueueDepth trả về giá trị > threshold → verify log warning

Files to Modify

  • batch/src/main/java/com/figpop/batch/service/BatchQueueMonitorService.java