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 ;
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 ;
2021-12-17 07:24:09 +01:00
import org.springframework.web.bind.annotation.GetMapping ;
2022-06-22 17:53:27 +02:00
import org.springframework.web.bind.annotation.PathVariable ;
2021-12-17 07:24:09 +01:00
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RestController ;
2021-03-16 17:38:53 +01:00
2022-09-12 15:38:44 +02:00
import javax.servlet.http.HttpServletRequest ;
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 ;
2021-03-16 17:38:53 +01:00
2023-01-25 17:33:49 +01:00
private final String shutdownOrCancelCode ;
2023-04-28 22:46:39 +02:00
public GeneralController ( @Value ( " ${info.controllerIp} " ) String controllerIp , @Value ( " ${info.shutdownOrCancelCode} " ) String shutdownOrCancelCode ) {
this . controllerIp = controllerIp ;
2023-01-25 17:33:49 +01:00
this . shutdownOrCancelCode = shutdownOrCancelCode ;
}
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 ;
@GetMapping ( " shutdownWorker/{shutdownCode} " )
2022-09-12 15:38:44 +02:00
public ResponseEntity < ? > shutdownWorkerGracefully ( @PathVariable String shutdownCode , HttpServletRequest request )
2022-06-22 17:53:27 +02:00
{
String initMsg = " Received a \" shutdownWorker \" request. " ;
2022-09-12 15:38:44 +02:00
ResponseEntity < ? > responseEntity = passSecurityChecks ( request , shutdownCode , initMsg ) ;
if ( responseEntity ! = null )
return responseEntity ;
2023-02-01 15:41:40 +01:00
String finalMsg = " " ;
if ( shouldShutdownWorker )
finalMsg = " The worker has already received a \" shutdownWorker \" (which was not canceled afterwards). " ;
else
shouldShutdownWorker = true ;
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
}
@GetMapping ( " cancelShutdownWorker/{cancelCode} " )
2022-09-12 15:38:44 +02:00
public ResponseEntity < ? > cancelShutdownWorkerGracefully ( @PathVariable String cancelCode , HttpServletRequest request )
2022-06-22 17:53:27 +02:00
{
String initMsg = " Received a \" cancelShutdownWorker \" request. " ;
2022-09-12 15:38:44 +02:00
ResponseEntity < ? > responseEntity = passSecurityChecks ( request , cancelCode , initMsg ) ;
if ( responseEntity ! = null )
return responseEntity ;
shouldShutdownWorker = false ;
2022-12-05 20:58:16 +01:00
String finalMsg = " Any previous \" shutdownWorker \" -request is canceled. The \" maxAssignmentsBatchesToHandleBeforeShutdown \" will still be honored (if it's set). " ;
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-01-25 17:33:49 +01:00
public ResponseEntity < ? > passSecurityChecks ( HttpServletRequest request , String code , String initMsg )
2022-09-12 15:38:44 +02:00
{
if ( request = = null ) {
logger . error ( initMsg + " The \" HttpServletRequest \" is null! " ) ;
return ResponseEntity . internalServerError ( ) . build ( ) ;
}
String remoteAddr = request . getHeader ( " X-FORWARDED-FOR " ) ;
if ( remoteAddr = = null | | " " . equals ( remoteAddr ) )
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 ) ) ) {
2022-09-12 15:38:44 +02:00
logger . error ( initMsg + " The request came from another IP: " + remoteAddr + " | while this worker has the IP: " + UriBuilder . ip ) ;
return ResponseEntity . status ( HttpStatus . FORBIDDEN ) . build ( ) ;
}
2023-01-25 17:33:49 +01:00
if ( ! code . equals ( this . shutdownOrCancelCode ) ) {
2022-09-12 15:38:44 +02:00
String errorMsg = initMsg + " But, it contains an invalid code: " + code ;
logger . error ( errorMsg ) ;
return ResponseEntity . status ( HttpStatus . UNAUTHORIZED ) . body ( errorMsg ) ;
}
return null ; // The checks are passing.
}
2021-03-16 17:38:53 +01:00
}