diff --git a/weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java b/weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java index 75eb756f..37b055a6 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java +++ b/weather-service/src/main/java/com/waither/weatherservice/batch/BatchScheduler.java @@ -21,6 +21,7 @@ public class BatchScheduler { private final JobLauncher jobLauncher; private final Job dailyWeatherJob; private final Job expectedWeatherJob; + private final Job weatherAdvisoryTasklet; @Scheduled(cron = "0 0 2,5,8,11,14,17,20,23 * * *") // 3시간마다 public void runDailyWeatherJob() { @@ -49,4 +50,18 @@ public void runExpectedWeatherJob() { log.error("Error executing expectedWeatherJob: ", e); } } + + @Scheduled(cron = "0 0 * * * *") // 1시간마다 + public void runWeatherAdvisoryJob() { + log.info("[ Scheduler ] WeatherAdvisory Api"); + try { + JobParameters jobParameters = new JobParametersBuilder() + .addLong("executedTime", System.currentTimeMillis()) + .toJobParameters(); + + jobLauncher.run(weatherAdvisoryTasklet, jobParameters); + } catch (JobExecutionException e) { + log.error("Error executing expectedWeatherJob: ", e); + } + } } diff --git a/weather-service/src/main/java/com/waither/weatherservice/batch/WeatherAdvisoryTasklet.java b/weather-service/src/main/java/com/waither/weatherservice/batch/WeatherAdvisoryTasklet.java new file mode 100644 index 00000000..5774cb9a --- /dev/null +++ b/weather-service/src/main/java/com/waither/weatherservice/batch/WeatherAdvisoryTasklet.java @@ -0,0 +1,39 @@ +package com.waither.weatherservice.batch; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.List; + +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; + +import com.waither.weatherservice.entity.Region; +import com.waither.weatherservice.exception.WeatherExceptionHandler; +import com.waither.weatherservice.response.WeatherErrorCode; +import com.waither.weatherservice.service.WeatherService; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class WeatherAdvisoryTasklet implements Tasklet { + + private final WeatherService weatherService; + + @Override + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) { + List regionList = weatherService.getRegionList(); + regionList.stream() + .forEach(region -> { + try { + weatherService.createWeatherAdvisory(region.getStartLat(), region.getStartLon()); + } catch (URISyntaxException e) { + throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_URI_ERROR); + } catch (IOException e) { + throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_OPENAPI_ERROR); + } + }); + return RepeatStatus.FINISHED; + } +} diff --git a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java index 95f4eeba..9a29d4dd 100644 --- a/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java +++ b/weather-service/src/main/java/com/waither/weatherservice/service/WeatherService.java @@ -165,25 +165,25 @@ public MainWeatherResponse getMainWeather(double latitude, double longitude) { List region = regionRepository.findRegionByLatAndLong(latitude, longitude); if (region.isEmpty()) - throw new WeatherExceptionHandler(WeatherErrorCode.WEATHER_MAIN_ERROR); + throw new WeatherExceptionHandler(WeatherErrorCode.REGION_NOT_FOUND); String regionName = region.get(0).getRegionName(); - log.info("region : {}", regionName); + log.info("[Main - api] region : {}", regionName); String expectedWeatherKey = regionName + "_" + convertLocalDateTimeToString(now); String dailyWeatherKey = regionName + "_" + convertLocalDateTimeToDailyWeatherTime(now); DailyWeather dailyWeather = dailyWeatherRepository.findById(dailyWeatherKey) - .orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.WEATHER_MAIN_ERROR)); + .orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.DAILY_NOT_FOUND)); - log.info(regionName + "DailyWeather : {}", dailyWeather); + log.info(regionName + "[Main - api] DailyWeather : {}", dailyWeather); ExpectedWeather expectedWeather = expectedWeatherRepository.findById(expectedWeatherKey) - .orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.WEATHER_MAIN_ERROR)); + .orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.EXPECTED_NOT_FOUND)); - log.info(regionName + "ExpectedWeather : {}", expectedWeather); + log.info(regionName + "[Main - api] ExpectedWeather : {}", expectedWeather); MainWeatherResponse weatherMainResponse = MainWeatherResponse.from( dailyWeather.getPop(), dailyWeather.getTmp(), dailyWeather.getTempMin(), @@ -193,7 +193,7 @@ public MainWeatherResponse getMainWeather(double latitude, double longitude) { expectedWeather.getExpectedRain(), expectedWeather.getExpectedPty(), expectedWeather.getExpectedSky() ); - log.info(regionName + "MainWeatherResponse : {}", weatherMainResponse); + log.info(regionName + "[Main - api] MainWeatherResponse : {}", weatherMainResponse); return weatherMainResponse; }