UrlsWorker/src/main/java/eu/openaire/urls_worker/UrlsWorkerApplication.java

91 lines
3.4 KiB
Java
Raw Normal View History

2021-03-16 17:38:53 +01:00
package eu.openaire.urls_worker;
import eu.openaire.publications_retriever.PublicationsRetriever;
import eu.openaire.publications_retriever.util.file.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-03-16 17:38:53 +01:00
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.annotation.PreDestroy;
import java.io.File;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
2021-03-16 17:38:53 +01:00
@SpringBootApplication
@EnableScheduling
2021-03-16 17:38:53 +01:00
public class UrlsWorkerApplication {
private static final Logger logger = LoggerFactory.getLogger(UrlsWorkerApplication.class);
private static final String controllerBaseUrlFilePath = FileUtils.workingDir + "controllerBaseUrl.txt";
public static String controllerBaseUrl = null; // BaseUrl template: "http://IP:PORT/api/"
2021-03-16 17:38:53 +01:00
public static void main(String[] args) {
setControllerBaseUrl(); // This may cause the Server to terminate early, in case the controllerBaseUrl cannot be found.
2021-03-16 17:38:53 +01:00
SpringApplication.run(UrlsWorkerApplication.class, args);
}
@PreDestroy
public static void preDestroy()
{
if ( PublicationsRetriever.executor != null )
{
PublicationsRetriever.executor.shutdown(); // Define that no new tasks will be scheduled.
try {
if ( !PublicationsRetriever.executor.awaitTermination(1, TimeUnit.MINUTES) ) {
logger.warn("The working threads did not finish on time! Stopping them immediately..");
PublicationsRetriever.executor.shutdownNow();
}
} catch (InterruptedException e) {
PublicationsRetriever.executor.shutdownNow();
}
}
}
private static void setControllerBaseUrl()
{
// Take the controllerBaseUrl from the file.
Scanner myReader = null;
try {
File controllerBaseUrlFile = new File(controllerBaseUrlFilePath);
if ( !controllerBaseUrlFile.exists() ) {
String errorMsg = "controllerBaseUrlFile \"" + controllerBaseUrlFilePath + "\" does not exists!";
logger.error(errorMsg);
System.err.println(errorMsg);
System.exit(60);
}
myReader = new Scanner(controllerBaseUrlFile);
if ( !myReader.hasNextLine() ) {
String errorMsg = "The controllerBaseUrlFile is empty! No WorkerReports can be sent from this worker! Exiting..";
logger.error(errorMsg);
System.err.println(errorMsg);
System.exit(61);
}
controllerBaseUrl = myReader.nextLine().trim();
if ( !controllerBaseUrl.endsWith("/") )
controllerBaseUrl += "/"; // Make sure the whole urls will not break later.
logger.info("The controllerBaseUrl is: " + controllerBaseUrl);
} catch (Exception e) {
String errorMsg = "An error prevented the retrieval of the controllerBaseUrl from the file: " + controllerBaseUrlFilePath + "\n" + e.getMessage();
logger.error(errorMsg);
System.err.println(errorMsg);
e.printStackTrace();
System.exit(62);
} finally {
if ( myReader != null )
myReader.close();
}
}
2021-03-16 17:38:53 +01:00
}