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

주차장 스케줄러 트랜잭션 분리 #74

Merged
merged 2 commits into from
Jun 4, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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 com.parkingcomestrue.external.service.ParkingService;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -28,13 +29,14 @@ public class ParkingUpdateScheduler {
private final List<ParkingApiService> parkingApiServices;
private final CoordinateApiService coordinateApiService;
private final ParkingRepository parkingRepository;
private final ParkingService parkingService;

@Scheduled(cron = "0 */30 * * * *")
public void autoUpdateOfferCurrentParking() {
Map<String, Parking> parkingLots = readBy(ParkingApiService::offerCurrentParking);
Map<String, Parking> saved = findAllByName(parkingLots.keySet());
updateSavedParkingLots(parkingLots, saved);
saveNewParkingLots(parkingLots, saved);
List<Parking> newParkingLots = findNewParkingLots(parkingLots, saved);
parkingService.updateParkingLots(parkingLots, saved, newParkingLots);
}

private Map<String, Parking> readBy(Predicate<ParkingApiService> currentParkingAvailable) {
Expand Down Expand Up @@ -68,24 +70,17 @@ private Map<String, Parking> findAllByName(Set<String> names) {
.collect(toParkingMap());
}

private void updateSavedParkingLots(Map<String, Parking> parkingLots, Map<String, Parking> saved) {
for (String parkingName : saved.keySet()) {
Parking origin = saved.get(parkingName);
Parking updated = parkingLots.get(parkingName);
origin.update(updated);
}
}

private void saveNewParkingLots(Map<String, Parking> parkingLots, Map<String, Parking> saved) {
private List<Parking> findNewParkingLots(Map<String, Parking> parkingLots, Map<String, Parking> saved) {
List<Parking> newParkingLots = parkingLots.keySet()
.stream()
.filter(parkingName -> !saved.containsKey(parkingName))
.map(parkingLots::get)
.toList();
updateLocation(newParkingLots);
parkingRepository.saveAll(newParkingLots);
return newParkingLots;
}


private void updateLocation(List<Parking> newParkingLots) {
for (Parking parking : newParkingLots) {
Location locationByAddress = coordinateApiService.extractLocationByAddress(
Expand All @@ -99,7 +94,7 @@ private void updateLocation(List<Parking> newParkingLots) {
public void autoUpdateNotOfferCurrentParking() {
Map<String, Parking> parkingLots = readBy(parkingApiService -> !parkingApiService.offerCurrentParking());
Map<String, Parking> saved = findAllByName(parkingLots.keySet());
updateSavedParkingLots(parkingLots, saved);
saveNewParkingLots(parkingLots, saved);
List<Parking> newParkingLots = findNewParkingLots(parkingLots, saved);
parkingService.updateParkingLots(parkingLots, saved, newParkingLots);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.parkingcomestrue.external.service;

import com.parkingcomestrue.common.domain.parking.Parking;
import com.parkingcomestrue.common.domain.parking.repository.ParkingRepository;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ParkingService {

private final ParkingRepository parkingRepository;

@Transactional
public void updateParkingLots(Map<String, Parking> parkingLots, Map<String, Parking> saved,
List<Parking> newParkingLots) {
updateSavedParkingLots(parkingLots, saved);
saveNewParkingLots(newParkingLots);
}

private void updateSavedParkingLots(Map<String, Parking> parkingLots, Map<String, Parking> saved) {
for (String parkingName : saved.keySet()) {
Parking origin = saved.get(parkingName);
Parking updated = parkingLots.get(parkingName);
origin.update(updated);
}
}

private void saveNewParkingLots(List<Parking> newParkingLots) {
parkingRepository.saveAll(newParkingLots);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.parkingcomestrue.external.coordinate.CoordinateApiService;
import com.parkingcomestrue.external.service.ParkingService;
import com.parkingcomestrue.fake.ExceptionParkingApiService;
import com.parkingcomestrue.fake.FakeCoordinateApiService;
import com.parkingcomestrue.fake.NotOfferCurrentParkingApiService;
Expand Down Expand Up @@ -29,7 +30,8 @@ void autoUpdateOfferCurrentParking() {
ParkingUpdateScheduler scheduler = new ParkingUpdateScheduler(
List.of(offerCurrentParkingApiService),
coordinateService,
parkingRepository
parkingRepository,
new ParkingService(parkingRepository)
);

//when
Expand All @@ -52,7 +54,8 @@ void autoUpdateNotOfferCurrentParking() {
ParkingUpdateScheduler scheduler = new ParkingUpdateScheduler(
List.of(notOfferCurrentParkingApiService),
coordinateService,
parkingRepository
parkingRepository,
new ParkingService(parkingRepository)
);

//when
Expand All @@ -76,7 +79,8 @@ void notAffectBetweenOfferAndNotOfferCurrentParking() {
ParkingUpdateScheduler scheduler = new ParkingUpdateScheduler(
List.of(offerCurrentParkingApiService, notOfferCurrentParkingApiService),
coordinateService,
parkingRepository
parkingRepository,
new ParkingService(parkingRepository)
);

//when
Expand All @@ -93,7 +97,8 @@ void autoUpdateWithExceptionApi() {
ParkingUpdateScheduler scheduler = new ParkingUpdateScheduler(
List.of(new OfferCurrentParkingApiService(5), new ExceptionParkingApiService()),
coordinateService,
parkingRepository
parkingRepository,
new ParkingService(parkingRepository)
);

//when
Expand Down
Loading