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 ;
2022-12-07 11:29:05 +01:00
import eu.openaire.urls_worker.controllers.FullTextsController ;
2021-11-26 16:04:31 +01:00
import eu.openaire.urls_worker.util.UriBuilder ;
2021-06-22 04:58:07 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2023-05-15 12:41:53 +02:00
import org.springframework.beans.factory.annotation.Value ;
2021-11-26 16:04:31 +01:00
import org.springframework.boot.CommandLineRunner ;
2021-03-16 17:38:53 +01:00
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.autoconfigure.SpringBootApplication ;
2021-11-26 16:04:31 +01:00
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
2022-09-12 15:38:44 +02:00
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext ;
2021-12-23 23:12:34 +01:00
import org.springframework.context.ConfigurableApplicationContext ;
2021-11-26 16:04:31 +01:00
import org.springframework.context.annotation.Bean ;
import org.springframework.core.env.Environment ;
2021-05-20 02:28:48 +02:00
import org.springframework.scheduling.annotation.EnableScheduling ;
2021-11-26 16:04:31 +01:00
import org.springframework.web.cors.CorsConfiguration ;
import org.springframework.web.cors.CorsConfigurationSource ;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource ;
2021-05-20 02:28:48 +02:00
2021-06-22 04:58:07 +02:00
import javax.annotation.PreDestroy ;
2021-11-26 16:04:31 +01:00
import java.util.Arrays ;
import java.util.Collections ;
2021-06-22 04:58:07 +02:00
import java.util.concurrent.TimeUnit ;
2021-03-16 17:38:53 +01:00
@SpringBootApplication
2021-11-26 16:04:31 +01:00
@EnableConfigurationProperties
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 ) ;
2023-05-15 12:22:55 +02:00
private static String workerId ;
2021-09-21 15:21:39 +02:00
2021-12-23 23:12:34 +01:00
private static ConfigurableApplicationContext context ;
2021-09-22 15:36:48 +02:00
2023-05-15 12:22:55 +02:00
public UrlsWorkerApplication ( @Value ( " ${info.workerId} " ) String workerId )
{
UrlsWorkerApplication . workerId = workerId ;
}
2021-03-16 17:38:53 +01:00
public static void main ( String [ ] args ) {
2021-12-23 23:12:34 +01:00
context = SpringApplication . run ( UrlsWorkerApplication . class , args ) ;
2021-12-03 03:09:40 +01:00
Runtime javaRuntime = Runtime . getRuntime ( ) ;
2022-12-05 20:58:16 +01:00
int mb = 1048576 ;
logger . debug ( " HeapSize: " + ( javaRuntime . totalMemory ( ) / mb ) + " mb " ) ;
logger . debug ( " HeapMaxSize: " + ( javaRuntime . maxMemory ( ) / mb ) + " mb " ) ;
logger . debug ( " HeapFreeSize: " + ( javaRuntime . freeMemory ( ) / mb ) + " mb " ) ;
2023-05-15 12:22:55 +02:00
logger . info ( " The Worker has started running. Its id is: \" " + workerId + " \" . " ) ;
2021-03-16 17:38:53 +01:00
}
2021-09-22 15:36:48 +02:00
2022-01-03 23:23:45 +01:00
public static void gentleAppShutdown ( )
2021-12-23 23:12:34 +01:00
{
2023-02-21 14:22:49 +01:00
int exitCode = 0 ;
try {
exitCode = SpringApplication . exit ( context , ( ) - > 0 ) ; // The "PreDestroy" method will be called. (the "context" will be closed automatically (I checked it))
} catch ( IllegalArgumentException iae ) {
logger . error ( iae . getMessage ( ) ) ; // This will say "Context must not be null", in case the "gentleAppShutdown()" was called too early in the app's lifetime. But it's ok.
}
2021-12-23 23:12:34 +01:00
System . exit ( exitCode ) ;
}
2021-06-22 04:58:07 +02:00
@PreDestroy
public static void preDestroy ( )
{
if ( PublicationsRetriever . executor ! = null )
{
2021-12-23 23:12:34 +01:00
logger . info ( " Shutting down the threads used by \" PublicationsRetriever \" -plugin.. " ) ;
2021-06-22 04:58:07 +02:00
PublicationsRetriever . executor . shutdown ( ) ; // Define that no new tasks will be scheduled.
try {
2023-05-23 19:17:58 +02:00
if ( ! PublicationsRetriever . executor . awaitTermination ( 2 , TimeUnit . MINUTES ) ) {
2021-06-22 04:58:07 +02:00
logger . warn ( " The working threads did not finish on time! Stopping them immediately.. " ) ;
PublicationsRetriever . executor . shutdownNow ( ) ;
}
2021-11-26 16:04:31 +01:00
} catch ( SecurityException se ) {
logger . error ( " Could not shutdown the threads in any way..! " , se ) ;
} catch ( InterruptedException ie ) {
try {
PublicationsRetriever . executor . shutdownNow ( ) ;
} catch ( SecurityException se ) {
logger . error ( " Could not shutdown the threads in any way..! " , se ) ;
}
2021-06-22 04:58:07 +02:00
}
}
2021-12-31 02:51:58 +01:00
2022-12-07 11:29:05 +01:00
FullTextsController . deleteDirectory ( - 1 ) ;
2022-12-15 13:15:24 +01:00
logger . info ( " Exiting.. " ) ;
2021-06-22 04:58:07 +02:00
}
2021-11-26 16:04:31 +01:00
@Bean
public CorsConfigurationSource corsConfigurationSource ( ) {
CorsConfiguration configuration = new CorsConfiguration ( ) ;
configuration . setAllowedOrigins ( Collections . singletonList ( " * " ) ) ;
configuration . setAllowedMethods ( Arrays . asList ( " GET " , " POST " , " PUT " , " PATCH " , " DELETE " , " OPTIONS " ) ) ;
configuration . setAllowedHeaders ( Arrays . asList ( " authorization " , " content-type " , " x-auth-token " ) ) ;
configuration . setExposedHeaders ( Collections . singletonList ( " x-auth-token " ) ) ;
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ( ) ;
source . registerCorsConfiguration ( " /** " , configuration ) ;
return source ;
}
2022-12-15 13:15:24 +01:00
2021-11-26 16:04:31 +01:00
@Bean
2022-09-12 15:38:44 +02:00
public CommandLineRunner setServerBaseUrl ( Environment environment , ServletWebServerApplicationContext webServerAppCtxt )
2021-11-26 16:04:31 +01:00
{
2022-09-12 15:38:44 +02:00
return args - > new UriBuilder ( environment , webServerAppCtxt ) ;
2021-11-26 16:04:31 +01:00
}
2021-03-16 17:38:53 +01:00
}