Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

중복 스케줄링 문제 예방 #85

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app-scheduler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// 프로메테우스 추가
implementation 'io.micrometer:micrometer-registry-prometheus'
// shed-lock
implementation 'net.javacrumbs.shedlock:shedlock-spring:5.1.0'
implementation 'net.javacrumbs.shedlock:shedlock-provider-jdbc-template:5.1.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import jakarta.annotation.PostConstruct;
import java.util.TimeZone;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "80s")
@SpringBootApplication
public class SchedulerApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.parkingcomestrue.external.config;

import javax.sql.DataSource;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class ShedLockConfig {

@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(
JdbcTemplateLockProvider.Configuration.builder()
.withJdbcTemplate(new JdbcTemplate(dataSource))
.usingDbTime()
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.parkingcomestrue.external.scheduler;

import com.parkingcomestrue.external.coordinate.CoordinateApiService;
import com.parkingcomestrue.external.parkingapi.ParkingApiService;
import com.parkingcomestrue.common.domain.parking.Location;
import com.parkingcomestrue.common.domain.parking.Parking;
import com.parkingcomestrue.common.domain.parking.repository.ParkingRepository;
import com.parkingcomestrue.external.coordinate.CoordinateApiService;
import com.parkingcomestrue.external.parkingapi.ParkingApiService;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -17,6 +17,7 @@
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -30,6 +31,7 @@ public class ParkingUpdateScheduler {
private final ParkingRepository parkingRepository;

@Scheduled(cron = "0 */30 * * * *")
@SchedulerLock(name = "autoUpdateOffer")
public void autoUpdateOfferCurrentParking() {
Map<String, Parking> parkingLots = readBy(ParkingApiService::offerCurrentParking);
Map<String, Parking> saved = findAllByName(parkingLots.keySet());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE shedlock
(
name VARCHAR(64) NOT NULL,
lock_until TIMESTAMP(3) NOT NULL,
locked_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
locked_by VARCHAR(255) NOT NULL,
PRIMARY KEY (name)
);