Skip to content

Commit

Permalink
chore: config ์ˆ˜์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
jemin committed Jan 15, 2024
1 parent 7c64939 commit 9d324ff
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ out/
.vscode/
src/main/resources/firebase/fcm_key.json
src/main/resources/application.yml
src/main/resources/application-dev.yml
src/main/resources/application-dev.yml
src/main/resources/application-prod.yml
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/backend/global/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -18,11 +23,20 @@ public class RedisConfig {
@Value("${spring.data.redis.port}")
private int port;

@Autowired
Environment env;

@Bean
public RedisConnectionFactory connectionFactory(){
RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration();
redisConfiguration.setHostName(host);
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);
}
}
22 changes: 20 additions & 2 deletions src/main/java/com/backend/global/config/SchedulerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
30 changes: 3 additions & 27 deletions src/main/java/com/backend/global/scheduler/SchedulerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.time.LocalTime;
import java.util.List;

import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;

@Slf4j
@Service
Expand All @@ -34,13 +35,15 @@ 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() {

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

@SchedulerLock(name = "send_alarm_lock", lockAtMostFor = "10s", lockAtLeastFor = "10s")
@Scheduled(cron = "0 */30 * * * *", zone = "Asia/Seoul")
public void sendAlarm()
{
Expand All @@ -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<Goal> 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));
// }


}
2 changes: 1 addition & 1 deletion src/main/resources/config

0 comments on commit 9d324ff

Please sign in to comment.