2021-03-16 17:38:53 +01:00
package eu.openaire.urls_worker.controllers ;
2023-01-25 17:33:49 +01:00
import eu.openaire.urls_worker.components.AssignmentsHandler ;
2023-05-23 21:19:41 +02:00
import eu.openaire.urls_worker.components.plugins.PublicationsRetrieverPlugin ;
2022-09-12 15:38:44 +02:00
import eu.openaire.urls_worker.util.UriBuilder ;
2021-03-16 17:38:53 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2023-01-25 17:33:49 +01:00
import org.springframework.beans.factory.annotation.Value ;
2022-06-22 17:53:27 +02:00
import org.springframework.http.HttpStatus ;
2021-03-16 17:38:53 +01:00
import org.springframework.http.ResponseEntity ;
2023-05-23 21:19:41 +02:00
import org.springframework.web.bind.annotation.* ;
2021-03-16 17:38:53 +01:00
2022-09-12 15:38:44 +02:00
import javax.servlet.http.HttpServletRequest ;
2023-05-22 20:25:22 +02:00
import java.io.File ;
import java.net.UnknownHostException ;
import java.util.regex.Pattern ;
2021-11-26 16:04:31 +01:00
2021-03-16 17:38:53 +01:00
@RestController
@RequestMapping ( " " )
public class GeneralController {
private static final Logger logger = LoggerFactory . getLogger ( GeneralController . class ) ;
2023-04-28 22:46:39 +02:00
private final String controllerIp ;
2023-05-23 21:19:41 +02:00
private final String workerReportsDirPath ;
2023-05-22 20:25:22 +02:00
private final String workerId ;
2021-03-16 17:38:53 +01:00
2023-01-25 17:33:49 +01:00
2023-05-22 20:25:22 +02:00
private static final Pattern DOMAIN_DETECTOR = Pattern . compile ( " ^.*[a-zA-Z].*$ " ) ;
2023-05-23 21:19:41 +02:00
public GeneralController ( @Value ( " ${info.controllerIp} " ) String controllerIp , @Value ( " ${workerReportsDirPath} " ) String workerReportsDirPath , @Value ( " ${info.workerId} " ) String workerId )
2023-05-22 20:25:22 +02:00
{
if ( DOMAIN_DETECTOR . matcher ( controllerIp ) . matches ( ) ) {
try {
this . controllerIp = java . net . InetAddress . getByName ( controllerIp ) . getHostAddress ( ) ;
} catch ( UnknownHostException uhe ) {
String errorMsg = " The domain given for the Controller ( " + controllerIp + " ) is unknown to the world! So its IP cannot be retrieved! " ;
logger . error ( errorMsg ) ;
throw new RuntimeException ( errorMsg ) ;
}
} else
this . controllerIp = controllerIp ;
2023-05-23 21:19:41 +02:00
this . workerReportsDirPath = workerReportsDirPath ;
2023-05-22 20:25:22 +02:00
this . workerId = workerId ;
2023-01-25 17:33:49 +01:00
}
2021-03-16 17:38:53 +01:00
2022-06-22 17:53:27 +02:00
2021-03-16 17:38:53 +01:00
@GetMapping ( " isAlive " )
public ResponseEntity < ? > isWorkerAlive ( ) {
logger . info ( " Received an \" isAlive \" request. " ) ;
return ResponseEntity . ok ( ) . build ( ) ;
}
2021-05-20 02:28:48 +02:00
2022-06-22 17:53:27 +02:00
public static boolean shouldShutdownWorker = false ;
2023-05-23 21:24:49 +02:00
@PostMapping ( " shutdownWorker " )
2023-05-03 14:15:46 +02:00
public ResponseEntity < ? > shutdownWorkerGracefully ( HttpServletRequest request )
2022-06-22 17:53:27 +02:00
{
2023-05-22 20:25:22 +02:00
String initMsg = " Received a \" shutdownWorker \" request. " ;
2023-05-03 14:15:46 +02:00
ResponseEntity < ? > responseEntity = passSecurityChecks ( request , initMsg ) ;
2022-09-12 15:38:44 +02:00
if ( responseEntity ! = null )
return responseEntity ;
2023-02-01 15:41:40 +01:00
String finalMsg = " " ;
if ( shouldShutdownWorker )
2023-05-22 20:25:22 +02:00
finalMsg = " The worker has already received a \" shutdownWorker \" (which was not canceled afterwards). " ;
2023-02-01 15:41:40 +01:00
else
shouldShutdownWorker = true ;
2023-05-22 20:25:22 +02:00
finalMsg + = " The worker will shutdown, after finishing current work. " ;
2022-12-05 20:58:16 +01:00
logger . info ( initMsg + finalMsg ) ;
2023-02-24 22:27:02 +01:00
return ResponseEntity . ok ( ) . body ( finalMsg + " \ n " ) ;
2022-06-22 17:53:27 +02:00
}
2023-05-23 21:24:49 +02:00
@PostMapping ( " cancelShutdownWorker " )
2023-05-03 14:15:46 +02:00
public ResponseEntity < ? > cancelShutdownWorkerGracefully ( HttpServletRequest request )
2022-06-22 17:53:27 +02:00
{
2023-05-23 21:24:49 +02:00
String initMsg = " Received a \" cancelShutdownWorker \" request. " ;
2023-05-03 14:15:46 +02:00
ResponseEntity < ? > responseEntity = passSecurityChecks ( request , initMsg ) ;
2022-09-12 15:38:44 +02:00
if ( responseEntity ! = null )
return responseEntity ;
shouldShutdownWorker = false ;
2023-05-23 21:24:49 +02:00
String finalMsg = " Any previous \" shutdownWorker \" -request is canceled. The \" maxAssignmentsBatchesToHandleBeforeShutdown \" will still be honored (if it's set). " ;
2022-12-05 20:58:16 +01:00
logger . info ( initMsg + finalMsg ) ;
2023-02-24 22:27:02 +01:00
return ResponseEntity . ok ( ) . body ( finalMsg + " \ n " ) ;
2022-06-22 17:53:27 +02:00
}
2021-11-26 16:04:31 +01:00
@GetMapping ( " getHandledAssignmentsCounts " )
public ResponseEntity < ? > getHandledAssignmentsCounts ( )
{
2022-12-07 11:29:05 +01:00
return ResponseEntity . ok ( AssignmentsHandler . assignmentsNumsHandled ) ;
2021-11-26 16:04:31 +01:00
}
2022-09-12 15:38:44 +02:00
2023-05-23 21:19:41 +02:00
@PostMapping ( " addReportResultToWorker/{assignmentsCounter} " )
public ResponseEntity < ? > addReportResultToWorker ( @PathVariable long assignmentsCounter , @RequestBody ( required = false ) String errorMsg )
{
String directoryPath = PublicationsRetrieverPlugin . assignmentsBasePath + " assignments_ " + assignmentsCounter + " _fullTexts " ;
File dir = new File ( directoryPath ) ;
if ( ! dir . isDirectory ( ) ) {
2023-05-31 14:25:36 +02:00
logger . error ( " The \" addReportResultToWorker \" -endpoint was called for an unknown \" assignmentsCounter \" : " + assignmentsCounter ) ;
2023-05-23 21:19:41 +02:00
return ResponseEntity . notFound ( ) . build ( ) ;
}
if ( errorMsg = = null ) {
logger . info ( " The Controller successfully handled the WorkerReport, for assignments_ " + assignmentsCounter + " . The worker-report and all full-text files associated with it, will be deleted. " ) ;
FullTextsController . deleteAssignmentsDirectory ( assignmentsCounter , dir ) ;
FullTextsController . deleteFile ( this . workerReportsDirPath + this . workerId + " _assignments_ " + assignmentsCounter + " _report.json " ) ;
} else
logger . error ( " The Controller failed to handle the WorkerReport, for assignments_ " + assignmentsCounter + " . The error is: \ n " + errorMsg ) ;
return ResponseEntity . ok ( ) . build ( ) ;
}
2023-05-03 14:15:46 +02:00
public ResponseEntity < ? > passSecurityChecks ( HttpServletRequest request , String initMsg )
2022-09-12 15:38:44 +02:00
{
if ( request = = null ) {
2023-05-23 21:24:49 +02:00
logger . error ( initMsg + " The \" HttpServletRequest \" is null! " ) ;
2022-09-12 15:38:44 +02:00
return ResponseEntity . internalServerError ( ) . build ( ) ;
}
2023-06-10 01:40:45 +02:00
String remoteAddr = request . getHeader ( " X-FORWARDED-FOR " ) ; // This retrieves the original IP address, if the request passes through a proxy server.
if ( remoteAddr = = null )
2022-09-12 15:38:44 +02:00
remoteAddr = request . getRemoteAddr ( ) ;
2023-04-28 22:46:39 +02:00
if ( ! ( remoteAddr . equals ( " 127.0.0.1 " ) | | remoteAddr . equals ( UriBuilder . ip ) | | remoteAddr . equals ( controllerIp ) ) ) {
2023-05-23 21:24:49 +02:00
logger . error ( initMsg + " The request came from another IP: " + remoteAddr + " | while this worker has the IP: " + UriBuilder . ip ) ;
2022-09-12 15:38:44 +02:00
return ResponseEntity . status ( HttpStatus . FORBIDDEN ) . build ( ) ;
}
return null ; // The checks are passing.
}
2021-03-16 17:38:53 +01:00
}