From 9d324ff4b152998a608890c21a5a75934669c291 Mon Sep 17 00:00:00 2001 From: jemin Date: Mon, 15 Jan 2024 16:45:44 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20config=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- build.gradle | 2 ++ .../backend/global/config/RedisConfig.java | 14 +++++++++ .../global/config/SchedulerConfig.java | 22 ++++++++++++-- .../global/scheduler/SchedulerService.java | 30 ++----------------- src/main/resources/config | 2 +- 6 files changed, 42 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 8402a98..7db382f 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,5 @@ out/ .vscode/ src/main/resources/firebase/fcm_key.json src/main/resources/application.yml -src/main/resources/application-dev.yml \ No newline at end of file +src/main/resources/application-dev.yml +src/main/resources/application-prod.yml \ No newline at end of file diff --git a/build.gradle b/build.gradle index 21a2e44..0170941 100644 --- a/build.gradle +++ b/build.gradle @@ -46,6 +46,8 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' + implementation 'net.javacrumbs.shedlock:shedlock-spring:5.1.0' + implementation 'net.javacrumbs.shedlock:shedlock-provider-redis-spring:5.1.0' implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter' diff --git a/src/main/java/com/backend/global/config/RedisConfig.java b/src/main/java/com/backend/global/config/RedisConfig.java index a92866a..3b93ee1 100644 --- a/src/main/java/com/backend/global/config/RedisConfig.java +++ b/src/main/java/com/backend/global/config/RedisConfig.java @@ -1,13 +1,18 @@ package com.backend.auth.config; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import net.javacrumbs.shedlock.core.LockProvider; +import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider; + @Configuration @EnableRedisRepositories public class RedisConfig { @@ -18,6 +23,9 @@ public class RedisConfig { @Value("${spring.data.redis.port}") private int port; + @Autowired + Environment env; + @Bean public RedisConnectionFactory connectionFactory(){ RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration(); @@ -25,4 +33,10 @@ public RedisConnectionFactory connectionFactory(){ redisConfiguration.setPort(port); return new LettuceConnectionFactory(redisConfiguration); } + + @Bean + public LockProvider lockProvider(RedisConnectionFactory connectionFactory) { + String lockEnv = env.getProperty("spring.profiles.active"); + return new RedisLockProvider(connectionFactory, lockEnv); + } } \ No newline at end of file diff --git a/src/main/java/com/backend/global/config/SchedulerConfig.java b/src/main/java/com/backend/global/config/SchedulerConfig.java index 7a2bc00..38d600f 100644 --- a/src/main/java/com/backend/global/config/SchedulerConfig.java +++ b/src/main/java/com/backend/global/config/SchedulerConfig.java @@ -2,8 +2,26 @@ import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; + +import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock; -@EnableScheduling @Configuration -public class SchedulerConfig { +@EnableScheduling +@EnableSchedulerLock(defaultLockAtLeastFor = "10s", defaultLockAtMostFor = "10s") +public class SchedulerConfig implements SchedulingConfigurer { + + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); + + threadPoolTaskScheduler.setPoolSize(3); + threadPoolTaskScheduler.setThreadGroupName("scheduler thread pool"); + threadPoolTaskScheduler.setThreadNamePrefix("scheduler-thread-"); + threadPoolTaskScheduler.initialize(); + + taskRegistrar.setTaskScheduler(threadPoolTaskScheduler); + } } diff --git a/src/main/java/com/backend/global/scheduler/SchedulerService.java b/src/main/java/com/backend/global/scheduler/SchedulerService.java index fc2e261..c2fb703 100644 --- a/src/main/java/com/backend/global/scheduler/SchedulerService.java +++ b/src/main/java/com/backend/global/scheduler/SchedulerService.java @@ -20,6 +20,7 @@ import java.time.LocalTime; import java.util.List; +import net.javacrumbs.shedlock.spring.annotation.SchedulerLock; @Slf4j @Service @@ -34,6 +35,7 @@ public class SchedulerService { private final ApplicationEventPublisher applicationEventPublisher; + @SchedulerLock(name = "outdate_goal_lock", lockAtMostFor = "10s", lockAtLeastFor = "10s") @Scheduled(cron = "0 0 * * * *", zone = "Asia/Seoul") public void storeOutDateGoal() { @@ -41,6 +43,7 @@ public void storeOutDateGoal() { goalList.forEach(Goal::store); } + @SchedulerLock(name = "send_alarm_lock", lockAtMostFor = "10s", lockAtLeastFor = "10s") @Scheduled(cron = "0 */30 * * * *", zone = "Asia/Seoul") public void sendAlarm() { @@ -53,31 +56,4 @@ public void sendAlarm() detailGoalAlarmList.forEach(alarmDto -> applicationEventPublisher.publishEvent(new AlarmEvent(alarmDto.uid(), alarmDto.detailGoalTitle()))); } - -// @Scheduled(cron = "0 19 * * 0 *", zone = "Asia/Seoul") -// public void sendReminder() -// { -// List goalListReminderEnabled = goalQueryRepository.findGoalListReminderEnabled(); -// -// Random random = new Random(); -// -// // 랜덤하게 2개 선택 -// for (int i = 0; i < RAND_COUNT; i++) { -// -// int randomIndex = random.nextInt(goalListReminderEnabled.size()); -// Goal goal = goalListReminderEnabled.get(randomIndex); -// -// if(Objects.nonNull(goal.getLastRemindDate()) && isIntervalDateExpired(goal)) -// { -// goal.updateLastRemindDate(LocalDate.now()); -// applicationEventPublisher.publishEvent(new ReminderEvent(goal.getMemberId(), goal.getTitle())); -// } -// } -// } -// -// private boolean isIntervalDateExpired(Goal goal) { -// return goal.getLastRemindDate().isBefore(LocalDate.now().minusDays(REMIND_INTERVAL)); -// } - - } diff --git a/src/main/resources/config b/src/main/resources/config index 6d05ce2..b1a29c5 160000 --- a/src/main/resources/config +++ b/src/main/resources/config @@ -1 +1 @@ -Subproject commit 6d05ce2fbad77b670745f92ae9ef28cf81cca5ad +Subproject commit b1a29c5787a527551db083e6bdffae69ebcee8a9