|
| 1 | +package com.loopers.interfaces.batch; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import org.springframework.batch.core.*; |
| 5 | +import org.springframework.batch.core.configuration.JobRegistry; |
| 6 | +import org.springframework.batch.core.launch.JobLauncher; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.web.bind.annotation.*; |
| 9 | + |
| 10 | +@RestController |
| 11 | +@RequiredArgsConstructor |
| 12 | +@RequestMapping("/api/v1/batch") |
| 13 | +public class BatchController { |
| 14 | + |
| 15 | + private final JobLauncher jobLauncher; |
| 16 | + private final JobRegistry jobRegistry; |
| 17 | + |
| 18 | + @PostMapping("/weekly-ranking") |
| 19 | + public ResponseEntity<String> runWeeklyRankingJob( |
| 20 | + @RequestParam String startDate, |
| 21 | + @RequestParam String endDate |
| 22 | + ) throws Exception { |
| 23 | + |
| 24 | + JobParameters params = new JobParametersBuilder() |
| 25 | + .addString("startDate", startDate) |
| 26 | + .addString("endDate", endDate) |
| 27 | + .addLong("timestamp", System.currentTimeMillis()) // 중복 실행 방지 |
| 28 | + .toJobParameters(); |
| 29 | + |
| 30 | + Job job = jobRegistry.getJob("weeklyRankingJob"); |
| 31 | + jobLauncher.run(job, params); |
| 32 | + |
| 33 | + return ResponseEntity.ok("WeeklyRankingJob started!"); |
| 34 | + } |
| 35 | + |
| 36 | + @PostMapping("/monthly-ranking") |
| 37 | + public ResponseEntity<String> runMonthlyRankingJob( |
| 38 | + @RequestParam String yearMonth |
| 39 | + ) throws Exception { |
| 40 | + |
| 41 | + JobParameters params = new JobParametersBuilder() |
| 42 | + .addString("yearMonth", yearMonth) |
| 43 | + .addLong("timestamp", System.currentTimeMillis()) // 중복 실행 방지 |
| 44 | + .toJobParameters(); |
| 45 | + |
| 46 | + Job job = jobRegistry.getJob("monthlyRankingJob"); |
| 47 | + JobExecution execution = jobLauncher.run(job, params); |
| 48 | + |
| 49 | + if (execution.getStatus() == BatchStatus.FAILED) { |
| 50 | + return ResponseEntity.status(500).body("monthlyRankingJob failed!"); |
| 51 | + } |
| 52 | + |
| 53 | + return ResponseEntity.ok("monthlyRankingJob started!"); |
| 54 | + } |
| 55 | +} |
0 commit comments