diff --git a/build.gradle b/build.gradle index 603e38c6..6b9568b0 100644 --- a/build.gradle +++ b/build.gradle @@ -76,6 +76,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-batch' testImplementation 'org.springframework.batch:spring-batch-test' + //scheduler + implementation 'net.javacrumbs.shedlock:shedlock-spring:4.14.0' + implementation 'net.javacrumbs.shedlock:shedlock-provider-jdbc-template:4.14.0' + } tasks.named('test') { diff --git a/src/main/java/com/coniverse/dangjang/domain/intro/dto/Version.java b/src/main/java/com/coniverse/dangjang/domain/intro/dto/Version.java index 54ff1413..e814ada9 100644 --- a/src/main/java/com/coniverse/dangjang/domain/intro/dto/Version.java +++ b/src/main/java/com/coniverse/dangjang/domain/intro/dto/Version.java @@ -5,8 +5,8 @@ @AllArgsConstructor public enum Version { - MINIMUM("1.0.3"), - LATEST("1.0.3"); + MINIMUM("1.0.4"), + LATEST("1.0.4"); @Getter private final String version; diff --git a/src/main/java/com/coniverse/dangjang/domain/scheduler/service/SchedulerService.java b/src/main/java/com/coniverse/dangjang/domain/scheduler/service/SchedulerService.java index d480b5cd..17bda0b3 100644 --- a/src/main/java/com/coniverse/dangjang/domain/scheduler/service/SchedulerService.java +++ b/src/main/java/com/coniverse/dangjang/domain/scheduler/service/SchedulerService.java @@ -7,6 +7,9 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock; +import net.javacrumbs.shedlock.spring.annotation.SchedulerLock; + import com.coniverse.dangjang.domain.notification.dto.fluentd.FcmMessage; import com.coniverse.dangjang.domain.notification.service.NotificationSendService; import com.coniverse.dangjang.domain.notification.service.NotificationService; @@ -21,6 +24,7 @@ */ @RequiredArgsConstructor @Service +@EnableSchedulerLock(defaultLockAtMostFor = "PT10S") public class SchedulerService { private final NotificationService notificationService; private final NotificationSendService notificationSendService; @@ -34,6 +38,7 @@ public class SchedulerService { * @since 1.1.0 */ @Scheduled(cron = "0 0 18 * * *", zone = "Asia/Seoul") + @SchedulerLock(name = "SchedulerService_makeNotification", lockAtLeastFor = "PT60S", lockAtMostFor = "PT70S") public void makeNotification() { List fcmMessage = notificationService.makeAccessFcmMessage(); fcmMessage.forEach(message -> notificationSendService.sendMessage(message)); diff --git a/src/main/java/com/coniverse/dangjang/global/config/SchedulerConfiguration.java b/src/main/java/com/coniverse/dangjang/global/config/SchedulerConfiguration.java new file mode 100644 index 00000000..88e65b71 --- /dev/null +++ b/src/main/java/com/coniverse/dangjang/global/config/SchedulerConfiguration.java @@ -0,0 +1,25 @@ +package com.coniverse.dangjang.global.config; + +import javax.sql.DataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import net.javacrumbs.shedlock.core.LockProvider; +import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider; + +/** + * 분산 서버에서 스케줄러를 사용하기 위함 + * + * @author EVE + * @since 1.1.0 + */ + +@Configuration +public class SchedulerConfiguration { + @Bean + public LockProvider lockProvider(DataSource dataSource) { + return new JdbcTemplateLockProvider((dataSource)); + } + +} diff --git a/src/main/resources/schema-dev.sql b/src/main/resources/schema-dev.sql index 4ef2f0e9..50f2d073 100644 --- a/src/main/resources/schema-dev.sql +++ b/src/main/resources/schema-dev.sql @@ -11,6 +11,7 @@ DROP TABLE IF EXISTS PURCHASE_HISTORY; DROP TABLE IF EXISTS POINT_PRODUCT; DROP TABLE IF EXISTS USER_POINT; DROP TABLE IF EXISTS USERS; +DROP TABLE IF EXISTS SHEDLOCK; CREATE TABLE `USERS` ( @@ -154,3 +155,11 @@ CREATE TABLE `NOTIFICATION` FOREIGN KEY (`OAUTH_ID`) REFERENCES USERS (`OAUTH_ID`) ON DELETE CASCADE, FOREIGN KEY (`TYPE`) REFERENCES NOTIFICATION_TYPE (`TYPE`) ); + +CREATE TABLE `SHEDLOCK` ( + `NAME` VARCHAR(64), + `LOCK_UNTIL` TIMESTAMP(3) NULL, + `LOCKED_AT` TIMESTAMP(3) NULL, + `LOCKED_BY` VARCHAR(255), + PRIMARY KEY (NAME) +) diff --git a/src/test/resources/schema.sql b/src/test/resources/schema.sql index 987c60f7..31ad0ca8 100644 --- a/src/test/resources/schema.sql +++ b/src/test/resources/schema.sql @@ -12,6 +12,7 @@ DROP TABLE IF EXISTS PURCHASE_HISTORY; DROP TABLE IF EXISTS POINT_PRODUCT; DROP TABLE IF EXISTS USER_POINT; DROP TABLE IF EXISTS USERS; +DROP TABLE IF EXISTS SHEDLOCK; CREATE TABLE `USERS` ( @@ -156,3 +157,10 @@ CREATE TABLE `NOTIFICATION` FOREIGN KEY (`OAUTH_ID`) REFERENCES USERS (`OAUTH_ID`), FOREIGN KEY (`TYPE`) REFERENCES NOTIFICATION_TYPE (`TYPE`) ); +CREATE TABLE `SHEDLOCK` ( + `NAME` VARCHAR(64), + `LOCK_UNTIL` TIMESTAMP(3) NULL, + `LOCKED_AT` TIMESTAMP(3) NULL, + `LOCKED_BY` VARCHAR(255), + PRIMARY KEY (NAME) +) \ No newline at end of file