Task 3-1: Retry & Alert cho Async Secondary Wallet Creation
Phase: 3 - Resilience Priority: Medium Module:
authenticationDepends 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àoAuthService. 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 beanRetrySchedulervà inject vàoAuthService. 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: importjava.util.concurrent.CompletableFuture. -Duration: importjava.time.Duration. -LOGGER: xem task-2-1 về cách khai báo logger.
- [ ] Locate async wallet creation code (likely
CompletableFuturehoặ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.javacompile thành công —WalletServiceđược inject,retrySchedulerbean tồn tại và được inject, importjava.util.concurrent.CompletableFuturevàjava.time.Durationtồn tại. - [ ] Happy path:
sign-inthà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 sau5s. - [ ] 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-3pass sau khi thay đổi.
Files to Modify
authentication/src/main/java/com/figpop/authentication/controllers/auth/AuthService.javaapplication-core/service/wallet/WalletService.java(thêm retry support)