Skip to content

Commit

Permalink
๐Ÿ”ง chore: log level ๋ณ€๊ฒฝ ๋ฐ ํŒŒ์ผ ์ถ”๊ฐ€ #160
Browse files Browse the repository at this point in the history
  • Loading branch information
jun02160 committed May 1, 2024
1 parent ad774e8 commit 5a72ee8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.time.LocalDateTime;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.PessimisticLockingFailureException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
Expand All @@ -16,7 +19,6 @@
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PessimisticLockException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import sopt.org.motivoo.domain.mission.entity.CompletedStatus;
import sopt.org.motivoo.domain.mission.entity.Mission;
import sopt.org.motivoo.domain.mission.entity.MissionQuest;
Expand All @@ -28,11 +30,12 @@
import sopt.org.motivoo.domain.user.repository.UserRetriever;
import sopt.org.motivoo.external.s3.S3Service;

@Slf4j
@Component
@RequiredArgsConstructor
public class UserMissionScheduler {

private static final Logger logger = LoggerFactory.getLogger(ScheduledTask.class);

private final UserMissionRetriever userMissionRetriever;
private final UserRetriever userRetriever;
private final MissionRetriever missionRetriever;
Expand All @@ -46,27 +49,25 @@ public class UserMissionScheduler {


// 1. ๋ฏธ์…˜ ๋‹ฌ์„ฑ ์ƒํƒœ ๋ฐ˜์˜
@Scheduled(cron = "@daily", zone = "Asia/Seoul")
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
public void setCompletedStatus() {
log.info("๋ฏธ์…˜ ๋‹ฌ์„ฑ์ƒํƒœ ์—…๋ฐ์ดํŠธ ์Šค์ผ€์ค„๋Ÿฌ ์ง„์ž…");
logger.debug("๋ฏธ์…˜ ๋‹ฌ์„ฑ์ƒํƒœ ์—…๋ฐ์ดํŠธ ์Šค์ผ€์ค„๋Ÿฌ ์ง„์ž…");

List<UserMission> missionsByCreatedAt = userMissionRetriever.getUserMissionsByCreatedDt(LocalDate.now().minusDays(1));
log.info("์–ด์ œ์˜ UserMission ๊ฐœ์ˆ˜: {}๊ฐœ", missionsByCreatedAt.size());
logger.trace("์–ด์ œ์˜ UserMission ๊ฐœ์ˆ˜: {}๊ฐœ", missionsByCreatedAt.size());
for (UserMission userMission : missionsByCreatedAt) {

// ์ˆ˜๋™ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ
TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);

log.info(userMission.getMission().getContent());
log.info(userMission.getCompletedStatus().getValue());
if (userMission.getImgUrl() != null) {
try {
log.info("์ด๋ฏธ์ง€๊ฐ€ ์˜ฌ๋ผ์™”์œผ๋‹ˆ ์„ฑ๊ณต์ด์–ด๋ผ..");
logger.trace("์ด๋ฏธ์ง€๊ฐ€ ์˜ฌ๋ผ์™”์œผ๋‹ˆ ์„ฑ๊ณต์ด์–ด๋ผ..");
userMission.updateCompletedStatus(CompletedStatus.SUCCESS);

UserMission um = em.merge(userMission);
log.info("์„ฑ๊ณต ์ƒํƒœ๋กœ ์—…๋ฐ์ดํŠธ ์™„๋ฃŒ: {}", um.getCompletedStatus());
logger.trace("์„ฑ๊ณต ์ƒํƒœ๋กœ ์—…๋ฐ์ดํŠธ ์™„๋ฃŒ: {}", um.getCompletedStatus());
transactionManager.commit(transactionStatus);
} catch (PessimisticLockingFailureException | PessimisticLockException e) {
transactionManager.rollback(transactionStatus);
Expand All @@ -78,11 +79,11 @@ public void setCompletedStatus() {
if (!userMission.isEmptyUserMission() &&
(userMission.getCompletedStatus() != CompletedStatus.SUCCESS || userMission.getImgUrl() == null)) {
try {
log.info("์„ฑ๊ณต์ด ์•„๋‹ˆ๋‹ˆ ์‹คํŒจ์—ฌ๋ผ..");
logger.trace("์„ฑ๊ณต์ด ์•„๋‹ˆ๋‹ˆ ์‹คํŒจ์—ฌ๋ผ..");
userMission.updateCompletedStatus(CompletedStatus.FAIL);

UserMission um = em.merge(userMission);
log.info("์‹คํŒจ ์ƒํƒœ๋กœ ์—…๋ฐ์ดํŠธ ์™„๋ฃŒ: {}", um.getCompletedStatus());
logger.trace("์‹คํŒจ ์ƒํƒœ๋กœ ์—…๋ฐ์ดํŠธ ์™„๋ฃŒ: {}", um.getCompletedStatus());
transactionManager.commit(transactionStatus);
} catch (PessimisticLockingFailureException | PessimisticLockException e) {
transactionManager.rollback(transactionStatus);
Expand All @@ -96,24 +97,25 @@ public void setCompletedStatus() {


// ์ž์ •๋งˆ๋‹ค ์˜ค๋Š˜์˜ ๋ฏธ์…˜ ์ดˆ๊ธฐ๊ฐ’ bulk insert
@Scheduled(cron = "@daily", zone = "Asia/Seoul")
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
public void insertEmptyUserMission() {
log.info("EmptyMission Bulk Insert");
logger.debug("EmptyMission Bulk Insert ๐ŸStart - {}", System.currentTimeMillis());
List<User> users = userRetriever.findAll().stream()
.filter(user -> !user.isDeleted())
.filter(user -> user.getParentchild().isMatched()).toList();
Mission emptyMission = missionRetriever.getEmptyMission();
MissionQuest missionQuest = missionQuestRetriever.getRandomMissionQuest();

userMissionRetriever.bulkSaveInitUserMission(users, LocalDate.now(), emptyMission, missionQuest);
log.info("User {} ๋ช…์˜ ์˜ค๋Š˜์˜ ๋ฏธ์…˜ insert ์„ฑ๊ณต", users.size());
logger.debug("User {} ๋ช…์˜ ์˜ค๋Š˜์˜ ๋ฏธ์…˜ insert ์„ฑ๊ณต", users.size());
logger.debug("EmptyMission Bulk Insert ๐Ÿ”šEnd - {}", System.currentTimeMillis());
}


// ๋งค์ผ ์ƒˆ๋ฒฝ 4์‹œ๋งˆ๋‹ค 30์ผ ์ด์ „์˜ ์‚ฌ์ง„์€ ๋ฒ„ํ‚ท์—์„œ ์‚ญ์ œํ•œ๋‹ค
@Scheduled(cron = "0 0 4 * * *", zone = "Asia/Seoul")
public void deleteImgBefore30Days() {
log.info("30์ผ ๋™์•ˆ ๋ณด๊ด€ํ•œ ์ด๋ฏธ์ง€๋Š” ์‚ญ์ œ");
logger.debug("30์ผ ๋™์•ˆ ๋ณด๊ด€ํ•œ ์ด๋ฏธ์ง€๋Š” ์‚ญ์ œ");
LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30);
userMissionRetriever.getUserMissionsByCreatedAtBefore(thirtyDaysAgo)
.forEach(um -> s3Service.deleteImage(um.getImgUrl()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.PessimisticLockingFailureException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
Expand All @@ -18,18 +21,18 @@
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PessimisticLockException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import sopt.org.motivoo.batch.service.firebase.UserStepManager;
import sopt.org.motivoo.common.advice.BusinessException;
import sopt.org.motivoo.domain.mission.entity.UserMission;
import sopt.org.motivoo.domain.mission.service.UserMissionService;
import sopt.org.motivoo.domain.user.entity.User;

@Slf4j
@Component
@RequiredArgsConstructor
public class UserStepScheduler {

private static final Logger logger = LoggerFactory.getLogger(ScheduledTask.class);

private final UserMissionService userMissionService;
private final UserStepManager userStepManager;

Expand All @@ -39,23 +42,21 @@ public class UserStepScheduler {
private EntityManager em;


// @Scheduled(cron = "* */2 * * * *", zone = "Asia/Seoul")
@Scheduled(cron = "@daily", zone = "Asia/Seoul")
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
public void flagUserStepInitialize() {
log.info("์œ ์ € ๊ฑธ์Œ ์ˆ˜ ์ดˆ๊ธฐํ™” ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰");
logger.trace("์œ ์ € ๊ฑธ์Œ ์ˆ˜ ์ดˆ๊ธฐํ™” ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰");
try {
userStepManager.insertUserStep();
log.info("์Šค์ผ€์ค„๋Ÿฌ์—์„œ ์œ ์ € insert ์„ฑ๊ณต");
logger.trace("์Šค์ผ€์ค„๋Ÿฌ์—์„œ ์œ ์ € insert ์„ฑ๊ณต");
// firebaseService.selectUser();
} catch (Exception e) {
log.error("ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ: {}", e.getMessage());
logger.error("ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ: {}", e.getMessage());
throw new BusinessException(FIREBASE_DB_INSERT_ERROR);
}
}

@Scheduled(fixedRate = 5000, zone = "Asia/Seoul")
public void readUserStep() {
log.info("์œ ์ € ๊ฑธ์Œ ์ˆ˜ ์ฝ๊ธฐ ์—ฐ์‚ฐ&์ƒํƒœ ์—…๋ฐ์ดํŠธ ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰");
try {
Map<User, Integer> userGoalSteps = userMissionService.getUsersGoalStep();
List<Long> ids = userGoalSteps.keySet().stream()
Expand All @@ -72,11 +73,11 @@ public void readUserStep() {
TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);

log.info("๋ชฉํ‘œ ๊ฑธ์Œ ์ˆ˜ {}๋ฅผ ๋„˜์€ ํ˜„์žฌ ๊ฑธ์Œ ์ˆ˜ {}", userGoalSteps.get(user), result.get(String.valueOf(id)));
logger.trace("๋ชฉํ‘œ ๊ฑธ์Œ ์ˆ˜ {}๋ฅผ ๋„˜์€ ํ˜„์žฌ ๊ฑธ์Œ ์ˆ˜ {}", userGoalSteps.get(user), result.get(String.valueOf(id)));
try {
user.getCurrentUserMission().updateCompletedStatus(STEP_COMPLETED);
UserMission u = em.merge(user.getCurrentUserMission());
log.info("User ๋ฏธ์…˜ ์ƒํƒœ ๋ฐ˜์˜ ์™„๋ฃŒ: {}", u.getCompletedStatus());
logger.trace("User ๋ฏธ์…˜ ์ƒํƒœ ๋ฐ˜์˜ ์™„๋ฃŒ: {}", u.getCompletedStatus());
transactionManager.commit(transactionStatus);
} catch (PessimisticLockingFailureException | PessimisticLockException e) {
transactionManager.rollback(transactionStatus);
Expand All @@ -86,9 +87,9 @@ public void readUserStep() {
}
}

log.info("์Šค์ผ€์ค„๋Ÿฌ์—์„œ ์œ ์ € select ์„ฑ๊ณต");
logger.trace("์Šค์ผ€์ค„๋Ÿฌ์—์„œ ์œ ์ € select ์„ฑ๊ณต");
} catch (Exception e) {
log.error("ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ: {}", e.getMessage());
logger.error("ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ: {}", e.getMessage());
throw new BusinessException(FIREBASE_DB_READ_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void checkMissionChoice(UserMission todayMission) {
// ์˜ค๋Š˜์˜ ๋ฏธ์…˜์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
public static boolean validateTodayDateMission(UserMission todayMission) {
if (!todayMission.isNowDate() || todayMission.isEmptyUserMission()) {
log.info("์œ ํšจํ•˜์ง€ ์•Š์€ ์˜ค๋Š˜์˜ ๋ฏธ์…˜! (์•„์ง ์œ ์ €๊ฐ€ ์„ ํƒ X)");
log.debug("์œ ํšจํ•˜์ง€ ์•Š์€ ์˜ค๋Š˜์˜ ๋ฏธ์…˜! (์•„์ง ์œ ์ €๊ฐ€ ์„ ํƒ X)");
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public class FirebaseService {
public void insertFBData(Map<String, Integer> values) {

ref.setValueAsync(values);
log.info("๋ชจ๋“  ํ™œ์„ฑ ์œ ์ €์˜ ๋ฐ์ดํ„ฐ insert ์„ฑ๊ณต!");
log.trace("๋ชจ๋“  ํ™œ์„ฑ ์œ ์ €์˜ ๋ฐ์ดํ„ฐ insert ์„ฑ๊ณต!");
}

public void updateFBData(Long id) {
Map<String, Object> values = new HashMap<>();
values.put(id.toString(), 0);
ref.updateChildrenAsync(values);

log.info("์ƒˆ๋กœ ๊ฐ€์ž…ํ•œ ์œ ์ €์˜ ๋ฐ์ดํ„ฐ insert ์„ฑ๊ณต!");
log.trace("์ƒˆ๋กœ ๊ฐ€์ž…ํ•œ ์œ ์ €์˜ ๋ฐ์ดํ„ฐ insert ์„ฑ๊ณต!");
}

public void selectAllUserStep() {
Expand Down Expand Up @@ -83,7 +83,7 @@ public void onCancelled(DatabaseError databaseError) {
}

latch.await(); // ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.
log.info("FB์—์„œ ๊ฐ€์ ธ์˜จ Map ์‚ฌ์ด์ฆˆ: {}", result.size());
log.trace("FB์—์„œ ๊ฐ€์ ธ์˜จ Map ์‚ฌ์ด์ฆˆ: {}", result.size());
return result;
}

Expand All @@ -94,20 +94,20 @@ public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String key = childSnapshot.getKey();
Object value = childSnapshot.getValue();
System.out.println(key + " : " + value.toString());
log.debug(key + " : " + value.toString());
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("Failed to read value." + databaseError.toException());
log.error("Failed to read value." + databaseError.toException());
}
});
}

public void deleteUserStep(Map<String, Object> values) {
ref.updateChildrenAsync(values);

log.info("ํƒˆํ‡ดํ•œ ์œ ์ €์˜ ๋ฐ์ดํ„ฐ delete ์„ฑ๊ณต!");
log.trace("ํƒˆํ‡ดํ•œ ์œ ์ €์˜ ๋ฐ์ดํ„ฐ delete ์„ฑ๊ณต!");
}
}

0 comments on commit 5a72ee8

Please sign in to comment.