2021-03-16 17:38:53 +01:00
|
|
|
package eu.openaire.urls_worker;
|
|
|
|
|
2021-06-22 04:58:07 +02:00
|
|
|
import eu.openaire.publications_retriever.PublicationsRetriever;
|
2021-09-21 15:21:39 +02:00
|
|
|
import eu.openaire.publications_retriever.util.file.FileUtils;
|
2021-06-22 04:58:07 +02:00
|
|
|
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;
|
2021-05-20 02:28:48 +02:00
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
|
2021-06-22 04:58:07 +02:00
|
|
|
import javax.annotation.PreDestroy;
|
2021-09-21 15:21:39 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.Scanner;
|
2021-06-22 04:58:07 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2021-03-16 17:38:53 +01:00
|
|
|
|
|
|
|
@SpringBootApplication
|
2021-05-20 02:28:48 +02:00
|
|
|
@EnableScheduling
|
2021-03-16 17:38:53 +01:00
|
|
|
public class UrlsWorkerApplication {
|
|
|
|
|
2021-06-22 04:58:07 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UrlsWorkerApplication.class);
|
|
|
|
|
2021-09-21 15:21:39 +02:00
|
|
|
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) {
|
2021-09-21 15:21:39 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-06-22 04:58:07 +02:00
|
|
|
@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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 15:21:39 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|