Skip to content

Commit

Permalink
YEL-214 [deploy] spring batch setting & lunch event 스케줄링 구현
Browse files Browse the repository at this point in the history
YEL-214 [deploy] spring batch setting & lunch event 스케줄링 구현
  • Loading branch information
hyeonjeongs authored Mar 1, 2024
2 parents 5ff1315 + 7bae149 commit d275132
Show file tree
Hide file tree
Showing 21 changed files with 544 additions and 153 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ dependencies {
// Repositories
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.amqp:spring-rabbit:3.1.1'
implementation 'org.hibernate:hibernate-core:6.4.4.Final'
implementation 'mysql:mysql-connector-java:8.0.33'

runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j:8.0.31'
runtimeOnly 'com.mysql:mysql-connector-j:8.2.0'

// Validations
implementation 'org.springframework.boot:spring-boot-starter-validation'
Expand Down Expand Up @@ -88,6 +90,7 @@ dependencies {
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.mockito:mockito-inline:5.2.0'
testImplementation 'org.springframework.batch:spring-batch-test'

// jwt decode
implementation 'org.bouncycastle:bcprov-jdk15on:1.69'
Expand All @@ -109,6 +112,10 @@ dependencies {
// tink
implementation 'com.google.crypto.tink:tink-android:1.4.0-rc1'
implementation 'com.google.crypto.tink:apps-rewardedads:1.10.0'

// spring batch
implementation 'org.springframework.boot:spring-boot-starter-batch'

}

asciidoctor {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/yello/server/ServerApplication.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.yello.server;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;


@SpringBootApplication
@ConditionalOnMissingBean(value = DefaultBatchConfiguration.class, annotation = EnableBatchProcessing.class)
public class ServerApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public void recommendUser(String recommendYelloId, String userYelloId) {
ZonedDateTime.now(GlobalZoneId).format(ISO_OFFSET_DATE_TIME),
recommendedUser
));
notificationService.sendRecommendSignupAndGetTicketNotification(recommendedUser);
}

notificationService.sendRecommendNotification(user, recommendedUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,5 @@ List<User> findAllByOtherGroupContainingYelloId(@Param("groupName") String group
@Query("select u from User u "
+ "where LOWER(u.name) like LOWER(CONCAT('%', :name, '%'))")
Page<User> findAllByNameContaining(Pageable pageable, @Param("name") String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ List<User> findAllByOtherGroupContainingYelloId(String groupName, String keyword
Page<User> findAllByNameContaining(Pageable pageable, String name);

void delete(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,6 @@ public Page<User> findAllByNameContaining(Pageable pageable, String name) {
public void delete(User user) {
userJpaRepository.delete(user);
}


}
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
package com.yello.server.domain.vote.service;

import static com.yello.server.global.common.ErrorCode.DUPLICATE_VOTE_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.INVALID_VOTE_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.LACK_POINT_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.LACK_USER_EXCEPTION;
import static com.yello.server.global.common.factory.WeightedRandomFactory.randomPoint;
import static com.yello.server.global.common.util.ConstantUtil.KEYWORD_HINT_POINT;
import static com.yello.server.global.common.util.ConstantUtil.NAME_HINT_DEFAULT;
import static com.yello.server.global.common.util.ConstantUtil.NAME_HINT_POINT;
import static com.yello.server.global.common.util.ConstantUtil.NO_FRIEND_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.RANDOM_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.VOTE_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_FEMALE;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_MALE;

import com.yello.server.domain.friend.dto.response.FriendShuffleResponse;
import com.yello.server.domain.friend.entity.Friend;
import com.yello.server.domain.friend.exception.FriendException;
Expand All @@ -33,17 +19,19 @@
import com.yello.server.domain.vote.exception.VoteForbiddenException;
import com.yello.server.domain.vote.exception.VoteNotFoundException;
import com.yello.server.domain.vote.repository.VoteRepository;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

import static com.yello.server.global.common.ErrorCode.*;
import static com.yello.server.global.common.factory.WeightedRandomFactory.randomPoint;
import static com.yello.server.global.common.util.ConstantUtil.*;

@Builder
@Component
@RequiredArgsConstructor
Expand All @@ -66,27 +54,32 @@ public List<Vote> createVotes(Long senderId, List<VoteAnswer> voteAnswers) {
final User sender = userRepository.getById(senderId);

IntStream.range(0, voteAnswers.size())
.forEach(index -> {
VoteAnswer currentVote = voteAnswers.get(index);
.filter(index -> {
User receiver = userRepository.getById(voteAnswers.get(index).friendId());
return Objects.isNull(receiver.getDeletedAt());
})
.forEach(index -> {
VoteAnswer currentVote = voteAnswers.get(index);

if (isDuplicatedVote(index, voteAnswers)) {
throw new VoteForbiddenException(DUPLICATE_VOTE_EXCEPTION);
}

if (isDuplicatedVote(index, voteAnswers)) {
throw new VoteForbiddenException(DUPLICATE_VOTE_EXCEPTION);
}
User receiver = userRepository.getById(currentVote.friendId());
Question question = questionRepository.getById(currentVote.questionId());

User receiver = userRepository.getById(currentVote.friendId());
Question question = questionRepository.getById(currentVote.questionId());

Vote newVote = Vote.createVote(
currentVote.keywordName(),
sender,
receiver,
question,
currentVote.colorIndex()
);
Vote newVote = Vote.createVote(
currentVote.keywordName(),
sender,
receiver,
question,
currentVote.colorIndex()
);

Vote savedVote = voteRepository.save(newVote);
votes.add(savedVote);
});
Vote savedVote = voteRepository.save(newVote);
votes.add(savedVote);
});

return votes;
}
Expand All @@ -97,15 +90,15 @@ public List<QuestionForVoteResponse> generateVoteQuestion(User user, List<Questi
Collections.shuffle(questionList);

return questionList.stream()
.map(question -> QuestionForVoteResponse.builder()
.friendList(getShuffledFriends(user))
.keywordList(getShuffledKeywords(question))
.question(QuestionVO.of(question))
.questionPoint(randomPoint())
.subscribe(user.getSubscribe().toString())
.build())
.limit(VOTE_COUNT)
.toList();
.map(question -> QuestionForVoteResponse.builder()
.friendList(getShuffledFriends(user))
.keywordList(getShuffledKeywords(question))
.question(QuestionVO.of(question))
.questionPoint(randomPoint())
.subscribe(user.getSubscribe().toString())
.build())
.limit(VOTE_COUNT)
.toList();
}

@Override
Expand Down Expand Up @@ -146,18 +139,18 @@ public KeywordCheckResponse useKeywordHint(User user, Vote vote) {
public void makeGreetingVote(User user) {
final User sender = userManager.getOfficialUser(user.getGender());
final Question greetingQuestion = questionRepository.findByQuestionContent(
null,
GREETING_NAME_FOOT,
null,
GREETING_KEYWORD_FOOT
null,
GREETING_NAME_FOOT,
null,
GREETING_KEYWORD_FOOT
).orElseGet(() ->
questionRepository.save(
Question.of(
null,
GREETING_NAME_FOOT,
null,
GREETING_KEYWORD_FOOT)
)
questionRepository.save(
Question.of(
null,
GREETING_NAME_FOOT,
null,
GREETING_KEYWORD_FOOT)
)
);

voteRepository.save(createFirstVote(sender, user, greetingQuestion));
Expand All @@ -177,19 +170,19 @@ public List<FriendShuffleResponse> getShuffledFriends(User user) {

if (friends.size() > NO_FRIEND_COUNT && friends.size() < RANDOM_COUNT) {
return friendList.stream()
.map(FriendShuffleResponse::of)
.toList();
.map(FriendShuffleResponse::of)
.toList();
}

return friendList.stream()
.map(FriendShuffleResponse::of)
.limit(RANDOM_COUNT)
.toList();
.map(FriendShuffleResponse::of)
.limit(RANDOM_COUNT)
.toList();
}

private boolean isDuplicatedVote(int index, List<VoteAnswer> voteAnswers) {
return index > 0 && voteAnswers.get(index - 1).questionId()
.equals(voteAnswers.get(index).questionId());
.equals(voteAnswers.get(index).questionId());
}

private List<String> getShuffledKeywords(Question question) {
Expand All @@ -198,24 +191,24 @@ private List<String> getShuffledKeywords(Question question) {
Collections.shuffle(keywordList);

return keywordList.stream()
.map(Keyword::getKeywordName)
.limit(RANDOM_COUNT)
.toList();
.map(Keyword::getKeywordName)
.limit(RANDOM_COUNT)
.toList();
}

private Vote createFirstVote(User sender, User receiver, Question question) {
final ThreadLocalRandom random = ThreadLocalRandom.current();
final String answer = "널 기다렸어";

return Vote.builder()
.answer(answer)
.nameHint(-3)
.isAnswerRevealed(true)
.isRead(false)
.sender(sender)
.receiver(receiver)
.question(question)
.colorIndex(random.nextInt(12) + 1)
.build();
.answer(answer)
.nameHint(-3)
.isAnswerRevealed(true)
.isRead(false)
.sender(sender)
.receiver(receiver)
.question(question)
.colorIndex(random.nextInt(12) + 1)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.yello.server.domain.vote.exception.VoteForbiddenException;
import com.yello.server.domain.vote.exception.VoteNotFoundException;
import com.yello.server.domain.vote.repository.VoteRepository;
import com.yello.server.infrastructure.firebase.service.NotificationService;
import com.yello.server.infrastructure.rabbitmq.service.ProducerService;
import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class VoteService {

private final VoteManager voteManager;
private final ProducerService producerService;
private final NotificationService notificationService;

public VoteListResponse findAllVotes(Long userId, Pageable pageable) {
Integer totalCount = voteRepository.countAllByReceiverUserId(userId);
Expand Down Expand Up @@ -104,6 +106,10 @@ public VoteDetailResponse findVoteById(Long voteId, Long userId) {
final Vote vote = voteRepository.getById(voteId);
final User user = userRepository.getById(userId);

if(!vote.getIsRead()) {
notificationService.sendOpenVoteNotification(vote.getSender());
}

vote.read();
return VoteDetailResponse.of(vote, user);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.yello.server.infrastructure.batch;


import com.yello.server.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.context.annotation.Configuration;

@RequiredArgsConstructor
@Configuration
public class ChunkProcessor {
public ItemProcessor<User, User> lunchEventProcessor() {
ItemProcessor<User, User> item = user -> {
System.out.println(user.getId() + ", " + user.getName() + " dds121212");
return user;
};
return item;
}

}
Loading

0 comments on commit d275132

Please sign in to comment.