Skip to content

Commit

Permalink
[chore] : 스케줄러에 shedlock 설정 추가 및 적용 (#52)
Browse files Browse the repository at this point in the history
* chore : 모듈별 프로퍼티파일 설정 추가

* chore : shedlock, redis 의존성 추가

* chore : shedlock 설정 추가 및 적용
  • Loading branch information
EunChanNam authored Jan 2, 2024
1 parent 32a694f commit 59c89c3
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ build/
.DS_Store
core/src/main/resources/application.yml
scheduler/src/main/resources/application.yml
api/src/main/resources/application.yml
application-core.yml
application-scheduler.yml

### STS ###
.apt_generated
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/dev/hooon/ApiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class ApiApplication {

public static void main(String[] args) {
System.setProperty("spring.config.name", "application-core,application,application-scheduler");
SpringApplication.run(ApiApplication.class, args);
}
}
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.fasterxml.jackson.core:jackson-databind'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
//테스트 픽쳐에서 사용할 의존성
testFixturesImplementation 'org.springframework.boot:spring-boot-starter-test'
testFixturesImplementation "org.testcontainers:testcontainers:1.19.3"
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/dev/hooon/common/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.hooon.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

@Configuration
public class RedisConfig {

@Value("${spring.data.redis.host}")
public String host;
@Value("${spring.data.redis.port}")
public int port;

@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}
}
3 changes: 3 additions & 0 deletions scheduler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework:spring-tx:6.1.1'
// shedlock
implementation 'net.javacrumbs.shedlock:shedlock-spring:5.1.0'
implementation 'net.javacrumbs.shedlock:shedlock-provider-redis-spring:5.1.0'

implementation project(':core')
testImplementation(testFixtures(project(':core')))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class SchedulerApplication {

public static void main(String[] args) {
System.setProperty("spring.config.name", "application-core,application,application-scheduler");
SpringApplication.run(SchedulerApplication.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;

@Configuration
@EnableScheduling
@EnableAsync
@EnableSchedulerLock(defaultLockAtLeastFor = "10s", defaultLockAtMostFor = "30s")
public class SchedulerConfig {

@Bean
public LockProvider lockProvider(RedisConnectionFactory redisConnectionFactory) {
String lockEnv = System.getProperty("spring.profiles.active");
return new RedisLockProvider(redisConnectionFactory, lockEnv);
}

@Bean("scheduler")
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

import dev.hooon.waitingbooking.application.facade.WaitingBookingFacade;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class WaitingBookingScheduler {

private final WaitingBookingFacade waitingBookingFacade;

@Scheduled(cron = "0 0/10 * * * *")
@SchedulerLock(name = "wb_1", lockAtLeastFor = "9m", lockAtMostFor = "15m")
public void scheduleWaitingBookingProcess() {
log.info("wb_1 : run");
waitingBookingFacade.processWaitingBooking();
}

@Scheduled(cron = "0/5 * * * * *")
@SchedulerLock(name = "wb_2", lockAtLeastFor = "4s", lockAtMostFor = "8s")
public void scheduleExpiredWaitingBookingProcess() {
log.info("wb_2 : run : run");
waitingBookingFacade.processExpiredWaitingBooking();
}
}

0 comments on commit 59c89c3

Please sign in to comment.