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; 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; @SpringBootApplication @EnableScheduling public class UrlsWorkerApplication { private static final Logger logger = LoggerFactory.getLogger(UrlsWorkerApplication.class); private static final String inputDataFilePath = FileUtils.workingDir + "inputData.txt"; public static String workerId = null; public static String controllerBaseUrl = null; // BaseUrl template: "http://IP:PORT/api/" public static void main(String[] args) { setInputData(); // This may cause the Server to terminate early, in case the workerId or the controllerBaseUrl cannot be found. 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 setInputData() { // Take the workerId and the controllerBaseUrl from the file. Scanner myReader = null; try { File inputDataFile = new File(inputDataFilePath); if ( !inputDataFile.exists() ) { String errorMsg = "controllerBaseUrlFile \"" + inputDataFilePath + "\" does not exists!"; logger.error(errorMsg); System.err.println(errorMsg); System.exit(60); } myReader = new Scanner(inputDataFile); if ( myReader.hasNextLine() ) { String[] data = myReader.nextLine().split(","); if ( data.length < 2 ) { String errorMsg = "Not all data were retrieved from file \"" + inputDataFilePath + "\"!"; logger.error(errorMsg); System.err.println(errorMsg); System.exit(61); } workerId = data[0].trim(); controllerBaseUrl = data[1].trim(); if ( !controllerBaseUrl.endsWith("/") ) controllerBaseUrl += "/"; // Make sure the whole urls will not break later. } if ( (workerId == null) || (controllerBaseUrl == null) ) { String errorMsg = "No \"workerId\" or/and \"controllerBaseUrl\" could be retrieved from the file: " + inputDataFilePath; logger.error(errorMsg); System.err.println(errorMsg); System.exit(62); } logger.info("workerId: " + workerId + ", controllerBaseUrl: " + controllerBaseUrl); // It's safe and helpful to show them in the logs. } catch (Exception e) { String errorMsg = "An error prevented the retrieval of the workerId and the controllerBaseUrl from the file: " + inputDataFilePath + "\n" + e.getMessage(); logger.error(errorMsg); System.err.println(errorMsg); e.printStackTrace(); System.exit(63); } finally { if ( myReader != null ) myReader.close(); } } }