Skip to content

Commit

Permalink
Deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn authored Jul 5, 2024
2 parents d3cf915 + 99a3d0f commit eeeba23
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Region> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,25 @@ public MainWeatherResponse getMainWeather(double latitude, double longitude) {
List<Region> 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(),
Expand All @@ -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;
}
Expand Down

0 comments on commit eeeba23

Please sign in to comment.