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

비동기 처리 시 메서드를 직접 호출하는 코드를 이벤트 발행으로 변경 #871

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,4 +1,4 @@
package balancetalk.talkpick.domain;
package balancetalk.talkpick.application;

import static balancetalk.file.domain.FileType.TALK_PICK;

Expand All @@ -15,7 +15,7 @@

@Component
@RequiredArgsConstructor
public class TalkPickFileHandler {
public class TalkPickFileService {

private final FileRepository fileRepository;
private final FileHandler fileHandler;
Expand All @@ -24,6 +24,9 @@ public class TalkPickFileHandler {
@Retryable(backoff = @Backoff(delay = 1000))
@Transactional
public void handleFilesOnTalkPickCreate(List<Long> fileIds, Long talkPickId) {
if (fileIds == null || fileIds.isEmpty()) {
return;
}
relocateFiles(fileIds, talkPickId);
}

Expand All @@ -32,14 +35,6 @@ private void relocateFiles(List<Long> fileIds, Long talkPickId) {
fileHandler.relocateFiles(files, talkPickId, TALK_PICK);
}

public List<String> findImgUrlsBy(Long talkPickId) {
return fileRepository.findImgUrlsByResourceIdAndFileType(talkPickId, TALK_PICK);
}

public List<Long> findFileIdsBy(Long talkPickId) {
return fileRepository.findIdsByResourceIdAndFileType(talkPickId, TALK_PICK);
}

@Async
@Retryable(backoff = @Backoff(delay = 1000))
@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package balancetalk.talkpick.application;

import static balancetalk.talkpick.domain.SummaryStatus.FAIL;

import balancetalk.talkpick.domain.TalkPickSummarizer;
import balancetalk.talkpick.domain.repository.TalkPickRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand All @@ -13,16 +8,12 @@
@RequiredArgsConstructor
public class TalkPickScheduleService {

private final TalkPickRepository talkPickRepository;
private final TalkPickSummarizer talkPickSummarizer;
private final TalkPickSummaryService talkPickSummaryService;
private final TodayTalkPickService todayTalkPickService;

// @Scheduled(cron = "0 30 00 * * ?")
public void retryFailedSummaries() {
List<Long> summaryFailedTalkPickIds = talkPickRepository.findIdsBySummaryStatus(FAIL);
for (Long summaryFailedTalkPickId : summaryFailedTalkPickIds) {
talkPickSummarizer.summarizeTalkPick(summaryFailedTalkPickId);
}
talkPickSummaryService.summarizeFailedTalkPick();
}

@Scheduled(cron = "0 00 00 * * ?")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package balancetalk.talkpick.application;

import static balancetalk.file.domain.FileType.TALK_PICK;
import static balancetalk.global.exception.ErrorCode.NOT_FOUND_TALK_PICK;
import static balancetalk.talkpick.dto.TalkPickDto.CreateTalkPickRequest;
import static balancetalk.talkpick.dto.TalkPickDto.TalkPickDetailResponse;
import static balancetalk.talkpick.dto.TalkPickDto.TalkPickResponse;
import static balancetalk.talkpick.dto.TalkPickDto.UpdateTalkPickRequest;

import balancetalk.file.domain.repository.FileRepository;
import balancetalk.global.exception.BalanceTalkException;
import balancetalk.member.domain.Member;
import balancetalk.member.domain.MemberRepository;
import balancetalk.member.dto.ApiMember;
import balancetalk.member.dto.GuestOrApiMember;
import balancetalk.talkpick.domain.TalkPick;
import balancetalk.talkpick.domain.TalkPickFileHandler;
import balancetalk.talkpick.domain.TalkPickSummarizer;
import balancetalk.talkpick.domain.event.TalkPickCreatedEvent;
import balancetalk.talkpick.domain.event.TalkPickDeletedEvent;
import balancetalk.talkpick.domain.event.TalkPickUpdatedEvent;
import balancetalk.talkpick.domain.repository.TalkPickRepository;
import balancetalk.vote.domain.TalkPickVote;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -30,20 +34,16 @@ public class TalkPickService {

private final MemberRepository memberRepository;
private final TalkPickRepository talkPickRepository;
private final TalkPickFileHandler talkPickFileHandler;
private final TalkPickSummarizer talkPickSummarizer;
private final ApplicationEventPublisher eventPublisher;
private final FileRepository fileRepository;

@Transactional
public Long createTalkPick(CreateTalkPickRequest request, ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
TalkPick savedTalkPick = talkPickRepository.save(request.toEntity(member));
Long savedTalkPickId = savedTalkPick.getId();

if (request.containsFileIds()) {
talkPickFileHandler.handleFilesOnTalkPickCreate(request.getFileIds(), savedTalkPickId);
}

talkPickSummarizer.summarizeTalkPick(savedTalkPickId);
eventPublisher.publishEvent(new TalkPickCreatedEvent(savedTalkPickId, request.getFileIds()));

return savedTalkPickId;
}
Expand All @@ -54,8 +54,8 @@ public TalkPickDetailResponse findById(Long talkPickId, GuestOrApiMember guestOr
.orElseThrow(() -> new BalanceTalkException(NOT_FOUND_TALK_PICK));
talkPick.increaseViews();

List<String> imgUrls = talkPickFileHandler.findImgUrlsBy(talkPickId);
List<Long> fileIds = talkPickFileHandler.findFileIdsBy(talkPickId);
List<String> imgUrls = fileRepository.findImgUrlsByResourceIdAndFileType(talkPickId, TALK_PICK);
List<Long> fileIds = fileRepository.findIdsByResourceIdAndFileType(talkPickId, TALK_PICK);

if (guestOrApiMember.isGuest()) {
return TalkPickDetailResponse.from(talkPick, imgUrls, fileIds, false, null);
Expand Down Expand Up @@ -85,16 +85,17 @@ public void updateTalkPick(Long talkPickId, UpdateTalkPickRequest request, ApiMe
Member member = apiMember.toMember(memberRepository);
TalkPick talkPick = member.getTalkPickById(talkPickId);
talkPick.update(request.toEntity(member));
talkPickFileHandler
.handleFilesOnTalkPickUpdate(request.getNewFileIds(), request.getDeleteFileIds(), talkPickId);
talkPickSummarizer.summarizeTalkPick(talkPickId);

eventPublisher.publishEvent(
new TalkPickUpdatedEvent(talkPickId, request.getNewFileIds(), request.getDeleteFileIds()));
}

@Transactional
public void deleteTalkPick(Long talkPickId, ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
TalkPick talkPick = member.getTalkPickById(talkPickId);
talkPickRepository.delete(talkPick);
talkPickFileHandler.handleFilesOnTalkPickDelete(talkPickId);

eventPublisher.publishEvent(new TalkPickDeletedEvent(talkPickId));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package balancetalk.talkpick.domain;
package balancetalk.talkpick.application;

import static balancetalk.global.exception.ErrorCode.NOT_FOUND_TALK_PICK;
import static balancetalk.global.exception.ErrorCode.TALK_PICK_SUMMARY_FAILED;
Expand All @@ -8,24 +8,27 @@
import static balancetalk.talkpick.domain.SummaryStatus.SUCCESS;

import balancetalk.global.exception.BalanceTalkException;
import balancetalk.talkpick.domain.Summary;
import balancetalk.talkpick.domain.TalkPick;
import balancetalk.talkpick.domain.repository.TalkPickRepository;
import balancetalk.talkpick.dto.fields.BaseTalkPickFields;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
@Service
@RequiredArgsConstructor
public class TalkPickSummarizer {
public class TalkPickSummaryService {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String SYSTEM_MESSAGE = """
Expand Down Expand Up @@ -58,7 +61,6 @@ public void summarizeTalkPick(Long talkPickId) {
TalkPick talkPick = talkPickRepository.findById(talkPickId)
.orElseThrow(() -> new BalanceTalkException(NOT_FOUND_TALK_PICK));


// 본문 글자수가 너무 짧으면 요약 제공 안함
if (talkPick.hasShortContent()) {
talkPick.updateSummaryStatus(NOT_REQUIRED);
Expand Down Expand Up @@ -114,4 +116,18 @@ private BaseTalkPickFields getBaseTalkPickFields(TalkPick talkPick) {
talkPick.getOptionA(),
talkPick.getOptionB());
}

@Transactional
public void summarizeFailedTalkPick() {
List<TalkPick> summaryFailedTalkPicks = talkPickRepository.findAllBySummaryStatus(FAIL);
for (TalkPick summaryFailedTalkPick : summaryFailedTalkPicks) {
try {
summarize(summaryFailedTalkPick);
} catch (Exception e) {
log.error("Fail to summary TalkPick ID = {}", summaryFailedTalkPick.getId());
log.error("exception message = {} {}", e.getMessage(), e.getStackTrace());
summaryFailedTalkPick.updateSummaryStatus(FAIL);
Hanjaemo marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package balancetalk.talkpick.domain.event;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class TalkPickCreatedEvent {

private Long talkPickId;
private List<Long> fileIds;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package balancetalk.talkpick.domain.event;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class TalkPickDeletedEvent {

private Long talkPickId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package balancetalk.talkpick.domain.event;

import balancetalk.talkpick.application.TalkPickFileService;
import balancetalk.talkpick.application.TalkPickSummaryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@RequiredArgsConstructor
public class TalkPickEventHandler {

private final TalkPickSummaryService talkPickSummaryService;
private final TalkPickFileService talkPickFileService;

@TransactionalEventListener
public void handleTalkPickCreatedEvent(TalkPickCreatedEvent event) {
talkPickFileService.handleFilesOnTalkPickCreate(event.getFileIds(), event.getTalkPickId());
talkPickSummaryService.summarizeTalkPick(event.getTalkPickId());
}
Hanjaemo marked this conversation as resolved.
Show resolved Hide resolved

@TransactionalEventListener
public void handleTalkPickUpdatedEvent(TalkPickUpdatedEvent event) {
talkPickFileService.handleFilesOnTalkPickUpdate(
event.getNewFileIds(), event.getDeleteFileIds(), event.getTalkPickId());
talkPickSummaryService.summarizeTalkPick(event.getTalkPickId());
}

@TransactionalEventListener
public void handleTalkPickDeletedEvent(TalkPickDeletedEvent event) {
talkPickFileService.handleFilesOnTalkPickDelete(event.getTalkPickId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package balancetalk.talkpick.domain.event;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class TalkPickUpdatedEvent {

private Long talkPickId;
private List<Long> newFileIds;
private List<Long> deleteFileIds;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package balancetalk.talkpick.domain.repository;

import balancetalk.talkpick.domain.SummaryStatus;
import balancetalk.talkpick.domain.TalkPick;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TalkPickRepository extends JpaRepository<TalkPick, Long>, TalkPickRepositoryCustom {

Page<TalkPick> findAllByMemberIdOrderByEditedAtDesc(Long memberId, Pageable pageable);

List<TalkPick> findAllBySummaryStatus(SummaryStatus summaryStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static balancetalk.talkpick.dto.TalkPickDto.TalkPickResponse;

import balancetalk.talkpick.domain.SummaryStatus;
import balancetalk.talkpick.domain.TalkPick;
import java.util.List;
import org.springframework.data.domain.Page;
Expand All @@ -15,6 +14,4 @@ public interface TalkPickRepositoryCustom {
Page<TalkPickResponse> findPagedTalkPicks(Pageable pageable);

List<TalkPickResponse> findBestTalkPicks();

List<Long> findIdsBySummaryStatus(SummaryStatus summaryStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static balancetalk.talkpick.dto.TalkPickDto.TalkPickResponse;
import static balancetalk.vote.domain.QTalkPickVote.talkPickVote;

import balancetalk.talkpick.domain.SummaryStatus;
import balancetalk.talkpick.domain.TalkPick;
import balancetalk.talkpick.dto.QTalkPickDto_TalkPickResponse;
import com.querydsl.jpa.impl.JPAQuery;
Expand Down Expand Up @@ -65,12 +64,4 @@ public List<TalkPickResponse> findBestTalkPicks() {
.limit(3)
.fetch();
}

@Override
public List<Long> findIdsBySummaryStatus(SummaryStatus summaryStatus) {
return queryFactory.select(talkPick.id)
.from(talkPick)
.where(talkPick.summaryStatus.eq(summaryStatus))
.fetch();
}
}
Loading
Loading