Skip to content

Commit 9d324ff

Browse files
author
jemin
committed
chore: config ์ˆ˜์ •
1 parent 7c64939 commit 9d324ff

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

โ€Ž.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ out/
3737
.vscode/
3838
src/main/resources/firebase/fcm_key.json
3939
src/main/resources/application.yml
40-
src/main/resources/application-dev.yml
40+
src/main/resources/application-dev.yml
41+
src/main/resources/application-prod.yml

โ€Žbuild.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ dependencies {
4646
testImplementation 'org.springframework.boot:spring-boot-starter-test'
4747
testImplementation 'org.springframework.security:spring-security-test'
4848

49+
implementation 'net.javacrumbs.shedlock:shedlock-spring:5.1.0'
50+
implementation 'net.javacrumbs.shedlock:shedlock-provider-redis-spring:5.1.0'
4951

5052
implementation 'org.springframework.boot:spring-boot-starter-webflux'
5153
implementation 'org.springframework.boot:spring-boot-starter'

โ€Žsrc/main/java/com/backend/global/config/RedisConfig.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.backend.auth.config;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.beans.factory.annotation.Value;
45
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.core.env.Environment;
68
import org.springframework.data.redis.connection.RedisConnectionFactory;
79
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
810
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
911
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
1012

13+
import net.javacrumbs.shedlock.core.LockProvider;
14+
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
15+
1116
@Configuration
1217
@EnableRedisRepositories
1318
public class RedisConfig {
@@ -18,11 +23,20 @@ public class RedisConfig {
1823
@Value("${spring.data.redis.port}")
1924
private int port;
2025

26+
@Autowired
27+
Environment env;
28+
2129
@Bean
2230
public RedisConnectionFactory connectionFactory(){
2331
RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration();
2432
redisConfiguration.setHostName(host);
2533
redisConfiguration.setPort(port);
2634
return new LettuceConnectionFactory(redisConfiguration);
2735
}
36+
37+
@Bean
38+
public LockProvider lockProvider(RedisConnectionFactory connectionFactory) {
39+
String lockEnv = env.getProperty("spring.profiles.active");
40+
return new RedisLockProvider(connectionFactory, lockEnv);
41+
}
2842
}

โ€Žsrc/main/java/com/backend/global/config/SchedulerConfig.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22

33
import org.springframework.context.annotation.Configuration;
44
import org.springframework.scheduling.annotation.EnableScheduling;
5+
import org.springframework.scheduling.annotation.SchedulingConfigurer;
6+
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
7+
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
8+
9+
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
510

6-
@EnableScheduling
711
@Configuration
8-
public class SchedulerConfig {
12+
@EnableScheduling
13+
@EnableSchedulerLock(defaultLockAtLeastFor = "10s", defaultLockAtMostFor = "10s")
14+
public class SchedulerConfig implements SchedulingConfigurer {
15+
16+
@Override
17+
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
18+
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
19+
20+
threadPoolTaskScheduler.setPoolSize(3);
21+
threadPoolTaskScheduler.setThreadGroupName("scheduler thread pool");
22+
threadPoolTaskScheduler.setThreadNamePrefix("scheduler-thread-");
23+
threadPoolTaskScheduler.initialize();
24+
25+
taskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
26+
}
927
}

โ€Žsrc/main/java/com/backend/global/scheduler/SchedulerService.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.time.LocalTime;
2121
import java.util.List;
2222

23+
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
2324

2425
@Slf4j
2526
@Service
@@ -34,13 +35,15 @@ public class SchedulerService {
3435
private final ApplicationEventPublisher applicationEventPublisher;
3536

3637

38+
@SchedulerLock(name = "outdate_goal_lock", lockAtMostFor = "10s", lockAtLeastFor = "10s")
3739
@Scheduled(cron = "0 0 * * * *", zone = "Asia/Seoul")
3840
public void storeOutDateGoal() {
3941

4042
List<Goal> goalList = goalQueryRepository.findGoalListEndDateExpired(LocalDate.now());
4143
goalList.forEach(Goal::store);
4244
}
4345

46+
@SchedulerLock(name = "send_alarm_lock", lockAtMostFor = "10s", lockAtLeastFor = "10s")
4447
@Scheduled(cron = "0 */30 * * * *", zone = "Asia/Seoul")
4548
public void sendAlarm()
4649
{
@@ -53,31 +56,4 @@ public void sendAlarm()
5356
detailGoalAlarmList.forEach(alarmDto ->
5457
applicationEventPublisher.publishEvent(new AlarmEvent(alarmDto.uid(), alarmDto.detailGoalTitle())));
5558
}
56-
57-
// @Scheduled(cron = "0 19 * * 0 *", zone = "Asia/Seoul")
58-
// public void sendReminder()
59-
// {
60-
// List<Goal> goalListReminderEnabled = goalQueryRepository.findGoalListReminderEnabled();
61-
//
62-
// Random random = new Random();
63-
//
64-
// // ๋žœ๋คํ•˜๊ฒŒ 2๊ฐœ ์„ ํƒ
65-
// for (int i = 0; i < RAND_COUNT; i++) {
66-
//
67-
// int randomIndex = random.nextInt(goalListReminderEnabled.size());
68-
// Goal goal = goalListReminderEnabled.get(randomIndex);
69-
//
70-
// if(Objects.nonNull(goal.getLastRemindDate()) && isIntervalDateExpired(goal))
71-
// {
72-
// goal.updateLastRemindDate(LocalDate.now());
73-
// applicationEventPublisher.publishEvent(new ReminderEvent(goal.getMemberId(), goal.getTitle()));
74-
// }
75-
// }
76-
// }
77-
//
78-
// private boolean isIntervalDateExpired(Goal goal) {
79-
// return goal.getLastRemindDate().isBefore(LocalDate.now().minusDays(REMIND_INTERVAL));
80-
// }
81-
82-
8359
}

โ€Žsrc/main/resources/config

0 commit comments

Comments
ย (0)