Skip to content

Commit

Permalink
Merge pull request #68 from co-niverse/refactor/schedulerLock
Browse files Browse the repository at this point in the history
Scheduler shedlock
  • Loading branch information
GIVEN53 authored Oct 25, 2023
2 parents 3de3835 + 0447291 commit e0f00cf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +24,7 @@
*/
@RequiredArgsConstructor
@Service
@EnableSchedulerLock(defaultLockAtMostFor = "PT10S")
public class SchedulerService {
private final NotificationService notificationService;
private final NotificationSendService notificationSendService;
Expand All @@ -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> fcmMessage = notificationService.makeAccessFcmMessage();
fcmMessage.forEach(message -> notificationSendService.sendMessage(message));
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}
9 changes: 9 additions & 0 deletions src/main/resources/schema-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`
(
Expand Down Expand Up @@ -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)
)
8 changes: 8 additions & 0 deletions src/test/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`
(
Expand Down Expand Up @@ -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)
)

0 comments on commit e0f00cf

Please sign in to comment.