-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#109 polling to sse #120
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3784b04
[Refactor] 결제 UNKNOWN 상태 도입 및 analysis 크레딧 메서드명 정리 (#62)
shinae1023 abb0a4b
Fix: 채용공고 async retry 상태머신 및 worker 메타데이터 보강 (#110)
shinae1023 4977edd
[Feat] async task SSE 상태 스트림 추가 (#109)
shinae1023 f387d0e
[Feat] SSE 하트비트 이벤트 추가 (#109)
shinae1023 0faa17a
[Fix] async stuck task sweep 및 timeout 복구 로직 추가 (#109)
shinae1023 fa683ba
[Fix] async SSE 발행 시점 및 sweep 안정성 보강 (#109)
shinae1023 06df346
[Fix] analysis async sweep 순환 의존성 제거 (#110)
shinae1023 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/jobdri/jobdri_api/domain/analysis/service/AnalysisAsyncSseService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.jobdri.jobdri_api.domain.analysis.service; | ||
|
|
||
| import com.jobdri.jobdri_api.domain.analysis.dto.response.AnalysisAsyncStatusResponse; | ||
| import com.jobdri.jobdri_api.domain.analysis.entity.AnalysisAsyncTask.TaskStatus; | ||
| import com.jobdri.jobdri_api.global.sse.SseSubscriptionRegistry; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class AnalysisAsyncSseService { | ||
|
|
||
| private static final String EVENT_NAME = "analysis-status"; | ||
|
|
||
| private final SseSubscriptionRegistry sseSubscriptionRegistry; | ||
|
|
||
| public SseEmitter subscribe(String taskId, Supplier<AnalysisAsyncStatusResponse> initialStatusSupplier) { | ||
| return sseSubscriptionRegistry.subscribe( | ||
| channelKey(taskId), | ||
| EVENT_NAME, | ||
| initialStatusSupplier, | ||
| this::isTerminal | ||
| ); | ||
| } | ||
|
|
||
| public void publish(AnalysisAsyncStatusResponse statusResponse) { | ||
| sseSubscriptionRegistry.publish( | ||
| channelKey(statusResponse.taskId()), | ||
| EVENT_NAME, | ||
| statusResponse, | ||
| isTerminal(statusResponse) | ||
| ); | ||
| } | ||
|
|
||
| private String channelKey(String taskId) { | ||
| return "analysis:" + taskId; | ||
| } | ||
|
|
||
| private boolean isTerminal(AnalysisAsyncStatusResponse statusResponse) { | ||
| TaskStatus status = TaskStatus.valueOf(statusResponse.status()); | ||
| return status == TaskStatus.SUCCEEDED || status == TaskStatus.FAILED; | ||
| } | ||
| } |
110 changes: 110 additions & 0 deletions
110
src/main/java/com/jobdri/jobdri_api/domain/analysis/service/AnalysisAsyncSweepService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package com.jobdri.jobdri_api.domain.analysis.service; | ||
|
|
||
| import com.jobdri.jobdri_api.domain.analysis.entity.AnalysisAsyncTask; | ||
| import com.jobdri.jobdri_api.domain.analysis.entity.AnalysisAsyncTask.CreditStatus; | ||
| import com.jobdri.jobdri_api.domain.analysis.entity.AnalysisAsyncTask.FailureReason; | ||
| import com.jobdri.jobdri_api.domain.analysis.entity.AnalysisAsyncTask.TaskStatus; | ||
| import com.jobdri.jobdri_api.domain.analysis.repository.AnalysisAsyncTaskRepository; | ||
| import com.jobdri.jobdri_api.domain.user.entity.User; | ||
| import com.jobdri.jobdri_api.domain.user.service.UserService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.transaction.support.TransactionTemplate; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.LocalDateTime; | ||
| import java.util.EnumSet; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class AnalysisAsyncSweepService { | ||
|
|
||
| private final AnalysisAsyncTaskRepository analysisAsyncTaskRepository; | ||
| private final AnalysisAsyncTaskService analysisAsyncTaskService; | ||
| private final AnalysisService analysisService; | ||
| private final UserService userService; | ||
| private final TransactionTemplate transactionTemplate; | ||
|
|
||
| @Value("${app.worker.analysis.queue-timeout-minutes:10}") | ||
| private long queueTimeoutMinutes; | ||
|
|
||
| @Value("${app.worker.analysis.processing-timeout-minutes:20}") | ||
| private long processingTimeoutMinutes; | ||
|
|
||
| public int sweepTimedOutTasks() { | ||
| int expiredCount = 0; | ||
| for (AnalysisAsyncTask task : analysisAsyncTaskRepository.findByStatusIn(EnumSet.of(TaskStatus.PENDING, TaskStatus.RUNNING))) { | ||
| try { | ||
| expiredCount += transactionTemplate.execute(status -> sweepTimedOutTask(task.getTaskId())); | ||
| } catch (RuntimeException e) { | ||
| log.error("Analysis async task sweep failed for taskId={}", task.getTaskId(), e); | ||
| } | ||
| } | ||
| return expiredCount; | ||
| } | ||
|
|
||
| private int sweepTimedOutTask(String taskId) { | ||
| AnalysisAsyncTask task = analysisAsyncTaskRepository.findById(taskId).orElse(null); | ||
| if (task == null || task.getStatus() == TaskStatus.SUCCEEDED || task.getStatus() == TaskStatus.FAILED) { | ||
| return 0; | ||
| } | ||
|
|
||
| ExpirationDecision expirationDecision = resolveExpiration(task); | ||
| if (expirationDecision == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| releaseCreditIfNeeded(task); | ||
| analysisAsyncTaskService.markFailed( | ||
| task.getTaskId(), | ||
| expirationDecision.failureReason(), | ||
| expirationDecision.errorMessage(), | ||
| task.getRetryCount() | ||
| ); | ||
| return 1; | ||
| } | ||
|
|
||
| private ExpirationDecision resolveExpiration(AnalysisAsyncTask task) { | ||
| LocalDateTime now = LocalDateTime.now(); | ||
| if (task.getStatus() == TaskStatus.PENDING && isExpired(task.getSubmittedAt(), now, queueTimeoutMinutes)) { | ||
| return new ExpirationDecision( | ||
| FailureReason.QUEUE_TIMEOUT, | ||
| "자소서 분석 작업이 대기열에서 시간 내 처리되지 않았습니다." | ||
| ); | ||
| } | ||
|
|
||
| LocalDateTime lastActivityAt = task.getLastAttemptAt() != null ? task.getLastAttemptAt() : task.getStartedAt(); | ||
| if (task.getStatus() == TaskStatus.RUNNING && isExpired(lastActivityAt, now, processingTimeoutMinutes)) { | ||
| return new ExpirationDecision( | ||
| FailureReason.INTERNAL_ERROR, | ||
| "자소서 분석 작업이 처리 제한 시간을 초과했습니다." | ||
| ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private boolean isExpired(LocalDateTime baseTime, LocalDateTime now, long timeoutMinutes) { | ||
| if (baseTime == null || timeoutMinutes <= 0) { | ||
| return false; | ||
| } | ||
| return Duration.between(baseTime, now).toMinutes() >= timeoutMinutes; | ||
| } | ||
|
|
||
| private void releaseCreditIfNeeded(AnalysisAsyncTask task) { | ||
| if (task.getCreditStatus() != CreditStatus.RESERVED || task.getCreditReferenceId() == null) { | ||
| return; | ||
| } | ||
|
|
||
| User user = userService.getUser(task.getUserId()); | ||
| analysisService.refundAnalysisCredit(user, task.getCreditReferenceId()); | ||
| analysisAsyncTaskService.markCreditReleased(task.getTaskId()); | ||
| } | ||
|
|
||
| private record ExpirationDecision(FailureReason failureReason, String errorMessage) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.