Skip to content

Commit

Permalink
fix: added checks to avoid too early restart or multiple parallel ext…
Browse files Browse the repository at this point in the history
…ractions of the same ghz
  • Loading branch information
jhaeu committed Oct 19, 2023
1 parent 19e4491 commit 56c0008
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@Service
public class GraphService {
private static final Logger LOGGER = Logger.getLogger(AppConfigMigration.class.getName());
private static final Logger LOGGER = Logger.getLogger(GraphService.class.getName());

public List<ORSGraphManager> graphManagers = new ArrayList<>();

Expand All @@ -24,36 +24,45 @@ public void addGraphhopperLocation(ORSGraphManager orsGraphManager) {
@Async
@Scheduled(cron = "${ors.engine.graphservice.schedule.download.cron:0 0 0 31 2 *}")//Default is "never"
public void checkForUpdatesInRepo() {

LOGGER.debug("Scheduled check for updates in graph repository...");

for (ORSGraphManager orsGraphManager : graphManagers) {
if (orsGraphManager.isActive()) {
LOGGER.info("[%s] Scheduled check for updates in graph repository: Download or extraction in progress".formatted(orsGraphManager.getRouteProfileName()));
LOGGER.info("Scheduled check for updates in graph repository: [%s] Download or extraction in progress".formatted(orsGraphManager.getProfileWithHash()));
} else if (orsGraphManager.hasDownloadedExtractedGraph()) {
LOGGER.info("[%s] A newer graph was already downloaded and extracted".formatted(orsGraphManager.getRouteProfileName()));
LOGGER.info("Scheduled check for updates in graph repository: [%s] A newer graph was already downloaded and extracted".formatted(orsGraphManager.getProfileWithHash()));
} else {
LOGGER.info("Scheduled check for updates in graph repository: [%s] Checking for update.".formatted(orsGraphManager.getProfileWithHash()));
orsGraphManager.downloadAndExtractLatestGraphIfNecessary();
}
}

LOGGER.debug("Scheduled check for updates in graph repository done");
}

@Async
@Scheduled(cron = "${ors.engine.graphservice.schedule.activate.cron:0 0 0 31 2 *}")//Default is "never"
public void checkForDownloadedGraphsToActivate() {

LOGGER.debug("Scheduled check for downloaded graphs...");

boolean restartNeeded = false;
boolean restartAllowed = true;

for (ORSGraphManager orsGraphManager : graphManagers) {
if (orsGraphManager.isActive()) {
LOGGER.info("[%s] Scheduled check for downloaded graphs: Download in progress".formatted(orsGraphManager.getRouteProfileName()));
if (orsGraphManager.isActive() || orsGraphManager.hasGraphDownloadFile()) {
LOGGER.info("Scheduled check for downloaded graphs: [%s] Download or extraction in progress".formatted(orsGraphManager.getProfileWithHash()));
restartAllowed = false;
}
if (orsGraphManager.hasDownloadedExtractedGraph()) {
LOGGER.info("[%s] Scheduled check for downloaded graphs: Downloaded extracted graph available".formatted(orsGraphManager.getRouteProfileName()));
LOGGER.info("Scheduled check for downloaded graphs: [%s] Downloaded extracted graph available".formatted(orsGraphManager.getProfileWithHash()));
restartNeeded = true;
}
}

if (restartNeeded && restartAllowed) {
LOGGER.info("Scheduled check for downloaded graphs done -> restarting openrouteservice");
restartApplication();
} else {
LOGGER.info("Scheduled check for downloaded graphs done -> restarting openrouteservice is %s".formatted(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,44 @@ public void extractDownloadedGraph() {
LOGGER.debug("[%s] No downloaded graph to extract".formatted(getProfileWithHash()));
return;
}

File graphDownloadFile = getGraphDownloadFile();
String graphDownloadFileAbsPath = graphDownloadFile.getAbsolutePath();
File targetDirectory = getGraphExractionDirectory();
String targetDirectoryAbsPath = targetDirectory.getAbsolutePath();
File extractionDirectory = asIncompleteDirectory(targetDirectory);
String extractionDirectoryAbsPath = extractionDirectory.getAbsolutePath();

if (extractionDirectory.exists()){
LOGGER.debug("[%s] Extraction already started".formatted(getProfileWithHash()));
return;
}

try {
File incompleteDirectory = asIncompleteDirectory(getGraphExractionDirectory());
String incompleteDirPath = incompleteDirectory.getAbsolutePath();

LOGGER.debug("[%s] Extracting downloaded graph file to %s".formatted(getProfileWithHash(), incompleteDirPath));
(new Unzipper()).unzip(getGraphDownloadFile().getAbsolutePath(), incompleteDirPath, true);
LOGGER.debug("[%s] Extracting downloaded graph file to %s".formatted(getProfileWithHash(), extractionDirectoryAbsPath));
long start = System.currentTimeMillis();
(new Unzipper()).unzip(graphDownloadFileAbsPath, extractionDirectoryAbsPath, true);
long end = System.currentTimeMillis();

LOGGER.debug("[%s] Extraction of downloaded graph file finished after %d ms, deleting downloaded graph file %s".formatted(
getProfileWithHash(),
end-start,
graphDownloadFileAbsPath));
graphDownloadFile.delete();

LOGGER.debug("[%s] Extraction of downloaded graph file done, renaming directory to %s".formatted(
LOGGER.debug("[%s] Renaming extraction directory to %s".formatted(
getProfileWithHash(),
getGraphExractionDirectory().getAbsolutePath()));
incompleteDirectory.renameTo(getGraphExractionDirectory());
targetDirectoryAbsPath));
extractionDirectory.renameTo(targetDirectory);

} catch (IOException ioException) {
LOGGER.error("[%s] Couldn't extract file %s to %s".formatted(getProfileWithHash(), getGraphDownloadFile().getAbsolutePath(), getGraphExractionDirectory().getAbsolutePath()));
throw new RuntimeException("Couldn't extract file " + getGraphDownloadFile().getAbsolutePath() + " to " + getGraphExractionDirectory().getAbsolutePath(), ioException);
LOGGER.error("[%s] Error during extraction of %s to %s -> %s".formatted(
getProfileWithHash(),
graphDownloadFileAbsPath,
extractionDirectoryAbsPath,
targetDirectoryAbsPath));
throw new RuntimeException("Caught ", ioException);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ void initialize(EngineConfig engineConfig) {
repoManager = new ORSGraphRepoManager(engineConfig, fileManager, routeProfileName, graphsRepoGraphVersion);
}

String getProfileWithHash() {
public String getProfileWithHash() {
return fileManager.getProfileWithHash();
}

public boolean isActive() {
return fileManager.isActive();
}

boolean hasLocalGraph() {
public boolean hasLocalGraph() {
return fileManager.hasLocalGraph();
}

boolean hasGraphDownloadFile() {
public boolean hasGraphDownloadFile() {
return fileManager.hasGraphDownloadFile();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public void downloadGraphIfNecessary() {
return;
}
if (fileManager.isActive()) {
LOGGER.info("[%s] ORSGraphManager is active - skipping download".formatted(getProfileWithHash()));
LOGGER.debug("[%s] ORSGraphManager is active - skipping download".formatted(getProfileWithHash()));
return;
}

LOGGER.info("[%s] Checking for possible graph update from remote repository...".formatted(getProfileWithHash()));
LOGGER.debug("[%s] Checking for possible graph update from remote repository...".formatted(getProfileWithHash()));
try {
ORSGraphInfoV1 persistedRemoteGraphInfo = fileManager.getPreviouslyDownloadedRemoteGraphInfo();
File graphDownloadFile = fileManager.getGraphDownloadFile();
Expand All @@ -91,7 +91,11 @@ public void downloadGraphIfNecessary() {

String downloadUrl = fileManager.createGraphUrlFromGraphInfoUrl(remoteGraphInfo);
LOGGER.info("[%s] Downloading %s to file %s".formatted(getProfileWithHash(), downloadUrl, graphDownloadFile.getAbsolutePath()));

long start = System.currentTimeMillis();
downloadAsset(downloadUrl, graphDownloadFile);
long end = System.currentTimeMillis();
LOGGER.info("[%s] Download finished after %d ms".formatted(getProfileWithHash(), end-start));
} catch (Exception e) {
LOGGER.error("[%s] Caught an exception during graph download check or graph download:".formatted(getProfileWithHash()), e);
}
Expand Down

0 comments on commit 56c0008

Please sign in to comment.