Skip to content

Commit

Permalink
Merge pull request #163 from Team-Going/feature/160
Browse files Browse the repository at this point in the history
[refactor] 디스코드 웹훅 알림 비동기 이벤트로 리팩터링
  • Loading branch information
SunwoongH authored Apr 19, 2024
2 parents aa9f45d + 9ec0496 commit 65fa1aa
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 29 deletions.
2 changes: 2 additions & 0 deletions doorip-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ dependencies {
implementation project(path: ':doorip-domain')
implementation project(path: ':doorip-external')
implementation project(path: ':doorip-common')
implementation project(path: ':doorip-event-publisher')
implementation project(path: ':doorip-event-subscriber')
}

jar {
Expand Down
4 changes: 1 addition & 3 deletions doorip-api/src/main/java/org/doorip/DooripApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(
scanBasePackageClasses = {DomainRoot.class, CommonRoot.class, ExternalRoot.class}
)
@SpringBootApplication
public class DooripApplication {
public static void main(String[] args) {
SpringApplication.run(DooripApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
import org.doorip.auth.jwt.JwtProvider;
import org.doorip.auth.jwt.JwtValidator;
import org.doorip.auth.jwt.Token;
import org.doorip.event.SignUpEvent;
import org.doorip.exception.ConflictException;
import org.doorip.exception.EntityNotFoundException;
import org.doorip.exception.InvalidValueException;
import org.doorip.exception.UnauthorizedException;
import org.doorip.message.ErrorMessage;
import org.doorip.message.EventMessage;
import org.doorip.openfeign.apple.AppleOAuthProvider;
import org.doorip.openfeign.discord.DiscordMessageProvider;
import org.doorip.openfeign.kakao.KakaoOAuthProvider;
import org.doorip.publisher.EventPublisher;
import org.doorip.user.domain.*;
import org.doorip.user.dto.request.ResultUpdateRequest;
import org.doorip.user.dto.request.UserReissueRequest;
import org.doorip.user.dto.request.UserSignInRequest;
import org.doorip.user.dto.request.UserSignUpRequest;
import org.doorip.user.dto.request.ProfileUpdateRequest;
import org.doorip.user.dto.request.*;
import org.doorip.user.dto.response.ProfileGetResponse;
import org.doorip.user.dto.response.UserSignInResponse;
import org.doorip.user.dto.response.UserSignUpResponse;
Expand Down Expand Up @@ -46,7 +43,7 @@ public class UserService {
private final JwtValidator jwtValidator;
private final AppleOAuthProvider appleOAuthProvider;
private final KakaoOAuthProvider kakaoOAuthProvider;
private final DiscordMessageProvider discordMessageProvider;
private final EventPublisher eventPublisher;

@Transactional(readOnly = true)
public void splash(Long userId) {
Expand All @@ -71,7 +68,8 @@ public UserSignUpResponse signUp(String token, UserSignUpRequest request) {
User savedUser = saveUser(request, platformId, enumPlatform);
Token issueToken = jwtProvider.issueToken(savedUser.getId());
updateRefreshToken(issueToken.refreshToken(), savedUser);
discordMessageProvider.sendMessage(EventMessage.SIGN_UP_EVENT);
int userCount = userRepository.countUser();
eventPublisher.publishSignUpEvent(SignUpEvent.of(EventMessage.SIGN_UP_EVENT, savedUser.getName(), userCount));
return UserSignUpResponse.of(issueToken, savedUser.getId());
}

Expand Down
4 changes: 0 additions & 4 deletions doorip-common/src/main/java/org/doorip/CommonRoot.java

This file was deleted.

13 changes: 13 additions & 0 deletions doorip-common/src/main/java/org/doorip/event/SignUpEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.doorip.event;

import org.doorip.message.EventMessage;

public record SignUpEvent(
EventMessage eventMessage,
String name,
int count
) {
public static SignUpEvent of(EventMessage eventMessage, String name, int count) {
return new SignUpEvent(eventMessage, name, count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum EventMessage {
SIGN_UP_EVENT("doorip 서비스에 회원가입 이벤트가 발생했습니다. 🎉");
SIGN_UP_EVENT("[🎉 doorip 회원가입 🎉]\n>> 닉네임: [%s]\n>> 현재 가입자 수: [%d명]");

private final String message;
}
4 changes: 0 additions & 4 deletions doorip-domain/src/main/java/org/doorip/DomainRoot.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import org.doorip.user.domain.Platform;
import org.doorip.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findUserByPlatformAndPlatformId(Platform platform, String platformId);

boolean existsUserByPlatformAndPlatformId(Platform platform, String platformId);

@Query("select count(*) from User u")
int countUser();
}
12 changes: 12 additions & 0 deletions doorip-event-publisher/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation project(path: ':doorip-common')
}

bootJar {
enabled = false
}

jar {
enabled = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.doorip.publisher;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.doorip.event.SignUpEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
public class EventPublisher {
private final ApplicationEventPublisher eventPublisher;

public void publishSignUpEvent(SignUpEvent event) {
log.info("[publish] sign up event {}", event);
eventPublisher.publishEvent(event);
}
}
13 changes: 13 additions & 0 deletions doorip-event-subscriber/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation project(path: ':doorip-common')
implementation project(path: ':doorip-external')
}

bootJar {
enabled = false
}

jar {
enabled = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.doorip.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@EnableAsync
@Configuration
public class AsyncConfig {
@Bean
public Executor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);
executor.setMaxPoolSize(30);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("async-executor-");
executor.initialize();
return executor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.doorip.subscriber;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.doorip.event.SignUpEvent;
import org.doorip.message.EventMessage;
import org.doorip.openfeign.discord.DiscordMessageProvider;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
public class EventSubscriber {
private final DiscordMessageProvider discordMessageProvider;

@Async
@EventListener
public void subscribeSignUpEvent(SignUpEvent event) {
log.info("[subscribe] sign up event {}", event);
EventMessage eventMessage = event.eventMessage();
String message = String.format(eventMessage.getMessage(), event.name(), event.count());
discordMessageProvider.sendMessage(message);
}
}
4 changes: 0 additions & 4 deletions doorip-external/src/main/java/org/doorip/ExternalRoot.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.doorip.config;

import org.doorip.ExternalRoot;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableFeignClients(basePackageClasses = ExternalRoot.class)
@EnableFeignClients(basePackages = "org.doorip")
public class FeignClientConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.RequiredArgsConstructor;
import org.doorip.exception.InvalidValueException;
import org.doorip.message.ErrorMessage;
import org.doorip.message.EventMessage;
import org.springframework.stereotype.Component;

import static org.doorip.openfeign.discord.DiscordMessage.createDiscordMessage;
Expand All @@ -14,8 +13,8 @@
public class DiscordMessageProvider {
private final DiscordFeignClient discordFeignClient;

public void sendMessage(EventMessage eventMessage) {
DiscordMessage discordMessage = createDiscordMessage(eventMessage.getMessage());
public void sendMessage(String message) {
DiscordMessage discordMessage = createDiscordMessage(message);
sendMessageToDiscord(discordMessage);
}

Expand Down
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ include 'doorip-api'
include 'doorip-domain'
include 'doorip-common'
include 'doorip-external'
include 'doorip-event-publisher'
include 'doorip-event-subscriber'

0 comments on commit 65fa1aa

Please sign in to comment.