Skip to content

Commit

Permalink
[FEAT] sql문 실행으로 스케쥴러 구현 (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
happyjamy authored Feb 23, 2024
1 parent dcde303 commit d612dac
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.oeid.mogakgo.core.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@Configuration
public class SchedulerConfig {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.oeid.mogakgo.scheduler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class FinishedProjectScheduler {

private final JdbcTemplate jdbcTemplate;
private final ResourceLoader resourceLoader;

private final List<String> sqlFilePaths;

public FinishedProjectScheduler(
JdbcTemplate jdbcTemplate, ResourceLoader resourceLoader,
@Value("${path.sql.finished-matched-project}") String finishedMatchedProjectSqlPath,
@Value("${path.sql.finished-pending-project}") String finishedPendingProjectSqlPath,
@Value("${path.sql.finished-project-request}") String finishedProjectRequestSqlPath
) {
this.jdbcTemplate = jdbcTemplate;
this.resourceLoader = resourceLoader;
this.sqlFilePaths = List.of(finishedMatchedProjectSqlPath, finishedPendingProjectSqlPath,
finishedProjectRequestSqlPath);
}

@Scheduled(cron = "0 0 0 * * ?") // 매일 자정에 실행
public void executeSqlFile() throws IOException {
for (String sqlFilePath : sqlFilePaths) {
String sql = loadSqlFromFile(sqlFilePath);
jdbcTemplate.execute(sql);
}
}

private String loadSqlFromFile(String path) throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(resourceLoader.getResource(path).getInputStream(),
StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining("\n"));
}
}

}
4 changes: 4 additions & 0 deletions src/main/resources/sql/finished_matched_project_after_12.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UPDATE project_tb
SET project_status = 'FINISHED'
WHERE DATE(created_at) != CURDATE()
AND project_status = 'MATCHED';
4 changes: 4 additions & 0 deletions src/main/resources/sql/finished_pending_project_after_12.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UPDATE project_tb
SET project_status = 'CANCELED'
WHERE DATE(created_at) != CURDATE()
AND project_status = 'PENDING';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
UPDATE project_join_request_tb pjr
JOIN project_tb p ON pjr.project_id = p.id
SET pjr.join_request_status = 'REJECTED'
WHERE p.project_status in ('FINISHED', 'CANCELED')
AND pjr.join_request_status = 'PENDING';

0 comments on commit d612dac

Please sign in to comment.