Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

전송 실패한 알림에 대한 로깅 구체화 #763

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.google.firebase.messaging.BatchResponse;
Expand Down Expand Up @@ -53,14 +54,10 @@ public List<FcmToken> getFailedWith5xxTokens() {
return getTokens(this::isFailedWith5xx);
}

public List<FcmToken> getNonRetryableFailedTokens() {
return getTokens(errorCode -> !isFailedWith429(errorCode) && !isFailedWith5xx(errorCode));
}

public List<FcmToken> getFinallyFailedTokens() {
return failedTokens.values().stream()
.flatMap(List::stream)
.toList();
public Map<MessagingErrorCode, List<FcmToken>> getNonRetryableFailedTokens() {
return failedTokens.keySet().stream()
.filter(errorCode -> !isFailedWith429(errorCode) && !isFailedWith5xx(errorCode))
.collect(Collectors.toMap(errorCode -> errorCode, failedTokens::get));
}

public int getRetryAfterSeconds() {
Expand Down Expand Up @@ -101,4 +98,8 @@ private boolean isTokenAbsent(MessagingErrorCode... errorCodes) {
public boolean hasNoFailedTokens() {
return failedTokens.isEmpty();
}

public void removeFailedWith404Tokens() {
failedTokens.remove(MessagingErrorCode.UNREGISTERED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,40 @@ public class FcmRetryableChecker {

@Transactional
public boolean check(CommonNotification notification, FcmFailedResponse failedResponse, int attempt) {
handleNonRetryableTokens(notification, failedResponse);
if (failedResponse.hasNoFailedTokens()) {
log.info("No failed tokens for title: {}, body: {}.", notification.getTitle(), notification.getBody());
return false;
}
return checkWhenFailedTokensExist(notification, failedResponse, attempt);
}

private boolean checkWhenFailedTokensExist(
CommonNotification notification, FcmFailedResponse failedResponse, int attempt
) {
removeAllUnregisteredTokens(failedResponse);
if (attempt > MAX_ATTEMPT) {
log.info("Max attempt reached for title: {}, body: {}, failed: {}", notification.getTitle(),
notification.getBody(), failedResponse.getFinallyFailedTokens());
log.info("Max attempt reached for title: {}, body: {}, tokens: {}", notification.getTitle(),
notification.getBody(), failedResponse.getFailedTokens());
return false;
}
if (failedResponse.hasNoRetryableTokens()) {
log.info("No retryable tokens for title: {}, body: {}.", notification.getTitle(), notification.getBody());
log.info("No retryable tokens for title: {}, body: {}, tokens: {}", notification.getTitle(), notification.getBody(),
failedResponse.getNonRetryableFailedTokens());
return false;
}
return true;
}

private void handleNonRetryableTokens(CommonNotification notification, FcmFailedResponse failedResponse) {
List<FcmToken> nonRetryableFailedTokens = failedResponse.getNonRetryableFailedTokens();
if (nonRetryableFailedTokens.isEmpty()) {
return;
}

log.info("Cannot Retry for title: {}, body: {}, failed: {}.", notification.getTitle(),
notification.getBody(), nonRetryableFailedTokens);
removeAllUnregisteredTokens(failedResponse.getFailedWith404Tokens());
}

private void removeAllUnregisteredTokens(List<FcmToken> failedWith404Tokens) {
private void removeAllUnregisteredTokens(FcmFailedResponse failedResponse) {
List<FcmToken> failedWith404Tokens = failedResponse.getFailedWith404Tokens();
if (failedWith404Tokens.isEmpty()) {
return;
}

log.info("Removing all unregistered tokens: {}", failedWith404Tokens);
List<String> tokens = failedWith404Tokens.stream().map(FcmToken::getToken).toList();

fcmTokenWriter.deleteAll(tokens);
failedResponse.removeFailedWith404Tokens();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지우는 이유가 궁금해요

}
}