2021-03-16 17:38:53 +01:00
package eu.openaire.urls_worker.controllers ;
2022-06-28 15:00:11 +02:00
import eu.openaire.urls_worker.UrlsWorkerApplication ;
2021-03-16 17:38:53 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
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
2021-11-26 16:04:31 +01:00
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
2021-03-16 17:38:53 +01:00
@RestController
@RequestMapping ( " " )
public class GeneralController {
private static final Logger logger = LoggerFactory . getLogger ( GeneralController . class ) ;
2021-09-21 15:21:39 +02:00
public GeneralController ( ) { }
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} " )
public ResponseEntity < ? > shutdownWorkerGracefully ( @PathVariable String shutdownCode )
{
String initMsg = " Received a \" shutdownWorker \" request. " ;
2022-06-28 15:00:11 +02:00
if ( shutdownCode . equals ( UrlsWorkerApplication . shutdownOrCancelCode ) ) {
2022-06-22 17:53:27 +02:00
shouldShutdownWorker = true ;
logger . info ( initMsg + " The worker will shutdown, after finishing current work. " ) ;
return ResponseEntity . ok ( ) . build ( ) ;
} else {
String errorMsg = initMsg + " But, it has an invalid \" shutdownCode \" : " + shutdownCode ;
logger . error ( errorMsg ) ;
return ResponseEntity . status ( HttpStatus . FORBIDDEN ) . body ( errorMsg ) ;
}
}
@GetMapping ( " cancelShutdownWorker/{cancelCode} " )
public ResponseEntity < ? > cancelShutdownWorkerGracefully ( @PathVariable String cancelCode )
{
String initMsg = " Received a \" cancelShutdownWorker \" request. " ;
2022-06-28 15:00:11 +02:00
if ( cancelCode . equals ( UrlsWorkerApplication . shutdownOrCancelCode ) ) {
2022-06-22 17:53:27 +02:00
shouldShutdownWorker = false ;
logger . info ( initMsg + " Any previous \" shutdownWorker \" -request is canceled. The \" maxAssignmentsBatchesToHandleBeforeShutdown \" will still be honored (if it's set). " ) ;
return ResponseEntity . ok ( ) . build ( ) ;
} else {
String errorMsg = initMsg + " But, it has an invalid \" cancelCode \" : " + cancelCode ;
logger . error ( errorMsg ) ;
return ResponseEntity . status ( HttpStatus . FORBIDDEN ) . body ( errorMsg ) ;
}
}
2021-11-26 16:04:31 +01:00
@GetMapping ( " getHandledAssignmentsCounts " )
public ResponseEntity < ? > getHandledAssignmentsCounts ( )
{
List < Long > handledAssignmentsCounts = new ArrayList < > ( FullTextsController . assignmentsNumsHandledAndLocallyDeleted . size ( ) / 2 ) ;
for ( Map . Entry < Long , Boolean > entry : FullTextsController . assignmentsNumsHandledAndLocallyDeleted . entrySet ( ) )
{
if ( entry . getValue ( ) . equals ( true ) )
handledAssignmentsCounts . add ( entry . getKey ( ) ) ;
}
return ResponseEntity . ok ( handledAssignmentsCounts ) ;
}
2021-03-16 17:38:53 +01:00
}