Skip to content

Task 3-1: Retry & Alert cho Async Secondary Wallet Creation

Phase: 3 - Resilience Priority: Medium Module: authentication Depends on: Không có Reference: docs/BountyHunter-Backend/details/feature-authentication/SPEC.md

Background

Secondary wallet (Immutable) được tạo bất đồng bộ trong signIn(). Nếu quá trình này thất bại, user không có wallet nhưng không có alert nào được gửi.

Tasks

File: authentication/controllers/auth/AuthService.java

DI / Import notes: - walletService: phải được inject vào AuthService. Nếu chưa có, thêm: @Autowired private WalletService walletService;. Import: com.figpop.application_core.service.wallet.WalletService (hoặc package tương ứng trong project). - retryScheduler: đây là một service/bean tùy chỉnh cần được tạo mới hoặc đã tồn tại. Nếu chưa có, cần khai báo bean RetryScheduler và inject vào AuthService. Xác nhận xem project đã có retry abstraction nào (e.g. Spring Retry, Resilience4j) chưa trước khi implement mới. - CompletableFuture: import java.util.concurrent.CompletableFuture. - Duration: import java.time.Duration. - LOGGER: xem task-2-1 về cách khai báo logger.

  • [ ] Locate async wallet creation code (likely CompletableFuture hoặc @Async)
  • [ ] Thêm error handling và retry với exponential backoff:

    CompletableFuture.runAsync(() -> {
        try {
            walletService.createSecondaryWallet(userId);
        } catch (Exception e) {
            LOGGER.error("[AUTH] Async wallet creation failed for userId={}", userId, e);
            // Schedule retry
            retryScheduler.scheduleRetry("create-wallet-" + userId, () ->
                walletService.createSecondaryWallet(userId), 3, Duration.ofSeconds(5));
        }
    });
    

  • [ ] Nếu retry vẫn fail → ghi vào dead letter table / JMS queue để xử lý thủ công

  • [ ] Alert khi wallet creation fail rate > threshold

Verification / Acceptance Criteria

  • [ ] Compile check: AuthService.java compile thành công — WalletService được inject, retryScheduler bean tồn tại và được inject, import java.util.concurrent.CompletableFuturejava.time.Duration tồn tại.
  • [ ] Happy path: sign-in thành công → wallet creation được gọi async; không block response trả về cho client.
  • [ ] Error handling: Simulate wallet creation failure (mock exception) → error được log ở level ERROR với userId; retry được schedule sau 5s.
  • [ ] Retry exhausted: Sau 3 lần retry thất bại → record được ghi vào dead letter table / JMS queue (kiểm tra DB hoặc queue).
  • [ ] User not blocked: User vẫn có thể login thành công dù wallet creation fail (graceful degradation).
  • [ ] Test reference: Scenario "missing wallet" trong task-4-3 pass sau khi thay đổi.

Files to Modify

  • authentication/src/main/java/com/figpop/authentication/controllers/auth/AuthService.java
  • application-core/service/wallet/WalletService.java (thêm retry support)