- Increase the initialDelay for the "checkIfServiceIsReadyForShutdown" scheduled-task, in production, to 10 minutes.
- Code polishing.
This commit is contained in:
parent
5d99a4be5d
commit
6669dc61bf
|
@ -54,9 +54,8 @@ public class ScheduledTasks {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Immediately delete the selected tasks form the global list, so that if these tasks are not finished before the scheduler runs again, they will not be re-executed.
|
// Immediately delete the selected tasks form the global list, so that if these tasks are not finished before the scheduler runs again, they will not be re-executed.
|
||||||
for ( Callable<Boolean> selectedTask : tempList ) {
|
for ( Callable<Boolean> selectedTask : tempList )
|
||||||
UrlsController.backgroundCallableTasks.remove(selectedTask);
|
UrlsController.backgroundCallableTasks.remove(selectedTask);
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug(numOfTasks + " background tasks were found inside the \"backgroundCallableTasks\" list and are about to be executed.");
|
logger.debug(numOfTasks + " background tasks were found inside the \"backgroundCallableTasks\" list and are about to be executed.");
|
||||||
// Execute the tasks and wait for them to finish.
|
// Execute the tasks and wait for them to finish.
|
||||||
|
@ -82,7 +81,7 @@ public class ScheduledTasks {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Scheduled(initialDelay = 60_000, fixedDelay = 7_200_000) // Check every 2 hours.
|
@Scheduled(initialDelay = 600_000, fixedDelay = 7_200_000) // Check every 2 hours. The initial delay is 10 minutes, to allow to shut down quickly in case of problem when starting, but also account for the initial communication with the Workers, where a problem may appear.
|
||||||
//@Scheduled(initialDelay = 60_000, fixedDelay = 20_000) // Just for testing (every 20 secs).
|
//@Scheduled(initialDelay = 60_000, fixedDelay = 20_000) // Just for testing (every 20 secs).
|
||||||
public void checkIfServiceIsReadyForShutdown()
|
public void checkIfServiceIsReadyForShutdown()
|
||||||
{
|
{
|
||||||
|
@ -91,10 +90,9 @@ public class ScheduledTasks {
|
||||||
|
|
||||||
// If the workers have shutdown on their own, without been instructed to by the Controller, then the Controller will keep running.
|
// If the workers have shutdown on their own, without been instructed to by the Controller, then the Controller will keep running.
|
||||||
|
|
||||||
for ( String workerId : UrlsController.workersInfoMap.keySet() ) {
|
for ( String workerId : UrlsController.workersInfoMap.keySet() )
|
||||||
if ( ! UrlsController.workersInfoMap.get(workerId).getHasShutdown() ) // The workerId is certainly inside the map and has a workerInfo value.
|
if ( ! UrlsController.workersInfoMap.get(workerId).getHasShutdown() ) // The workerId is certainly inside the map and has a workerInfo value.
|
||||||
return; // If at least 1 worker is still active, then do not shut down the server.
|
return; // If at least 1 worker is still active, then do not shut down the server.
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("All workers have already shutdown. Shutting down the Controller..");
|
logger.info("All workers have already shutdown. Shutting down the Controller..");
|
||||||
Application.gentleAppShutdown();
|
Application.gentleAppShutdown();
|
||||||
|
|
|
@ -2,6 +2,7 @@ package eu.openaire.urls_controller.controllers;
|
||||||
|
|
||||||
import eu.openaire.urls_controller.models.WorkerInfo;
|
import eu.openaire.urls_controller.models.WorkerInfo;
|
||||||
import eu.openaire.urls_controller.services.ShutdownService;
|
import eu.openaire.urls_controller.services.ShutdownService;
|
||||||
|
import eu.openaire.urls_controller.util.GenericUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -97,10 +98,7 @@ public class ShutdownController {
|
||||||
return ResponseEntity.badRequest().body(errorMsg);
|
return ResponseEntity.badRequest().body(errorMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
String remoteAddr = request.getHeader("X-FORWARDED-FOR");
|
String remoteAddr = GenericUtils.getRequestorAddress(request);
|
||||||
if ( remoteAddr == null || remoteAddr.isEmpty() )
|
|
||||||
remoteAddr = request.getRemoteAddr();
|
|
||||||
|
|
||||||
if ( ! remoteAddr.equals(workerInfo.getWorkerIP()) ) {
|
if ( ! remoteAddr.equals(workerInfo.getWorkerIP()) ) {
|
||||||
logger.error(initMsg + " The request came from another IP: " + remoteAddr + " | while this worker was registered with the IP: " + workerInfo.getWorkerIP());
|
logger.error(initMsg + " The request came from another IP: " + remoteAddr + " | while this worker was registered with the IP: " + workerInfo.getWorkerIP());
|
||||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
|
||||||
|
|
|
@ -5,6 +5,7 @@ import eu.openaire.urls_controller.models.WorkerInfo;
|
||||||
import eu.openaire.urls_controller.payloads.requests.WorkerReport;
|
import eu.openaire.urls_controller.payloads.requests.WorkerReport;
|
||||||
import eu.openaire.urls_controller.services.UrlsService;
|
import eu.openaire.urls_controller.services.UrlsService;
|
||||||
import eu.openaire.urls_controller.util.FileUtils;
|
import eu.openaire.urls_controller.util.FileUtils;
|
||||||
|
import eu.openaire.urls_controller.util.GenericUtils;
|
||||||
import eu.openaire.urls_controller.util.ParquetFileUtils;
|
import eu.openaire.urls_controller.util.ParquetFileUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -103,9 +104,7 @@ public class UrlsController {
|
||||||
logger.error("The \"HttpServletRequest\" is null!");
|
logger.error("The \"HttpServletRequest\" is null!");
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||||
}
|
}
|
||||||
String remoteAddr = request.getHeader("X-FORWARDED-FOR");
|
String remoteAddr = GenericUtils.getRequestorAddress(request);
|
||||||
if ( (remoteAddr == null) || remoteAddr.isEmpty() )
|
|
||||||
remoteAddr = request.getRemoteAddr();
|
|
||||||
|
|
||||||
WorkerInfo workerInfo = workersInfoMap.get(workerId);
|
WorkerInfo workerInfo = workersInfoMap.get(workerId);
|
||||||
if ( workerInfo != null ) { // This worker has already been identified.
|
if ( workerInfo != null ) { // This worker has already been identified.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package eu.openaire.urls_controller.services;
|
package eu.openaire.urls_controller.services;
|
||||||
|
|
||||||
import eu.openaire.urls_controller.controllers.UrlsController;
|
import eu.openaire.urls_controller.controllers.UrlsController;
|
||||||
|
import eu.openaire.urls_controller.util.GenericUtils;
|
||||||
import eu.openaire.urls_controller.util.UriBuilder;
|
import eu.openaire.urls_controller.util.UriBuilder;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -27,9 +28,7 @@ public class ShutdownServiceImpl implements ShutdownService {
|
||||||
logger.error(initMsg + "The \"HttpServletRequest\" is null!");
|
logger.error(initMsg + "The \"HttpServletRequest\" is null!");
|
||||||
return ResponseEntity.internalServerError().build();
|
return ResponseEntity.internalServerError().build();
|
||||||
}
|
}
|
||||||
String remoteAddr = request.getHeader("X-FORWARDED-FOR");
|
String remoteAddr = GenericUtils.getRequestorAddress(request);
|
||||||
if ( remoteAddr == null || remoteAddr.isEmpty() )
|
|
||||||
remoteAddr = request.getRemoteAddr();
|
|
||||||
|
|
||||||
// In case the Controller is running inside a docker container, and we want to send the "shutdownServiceRequest" from the terminal (with curl), without entering inside the container,
|
// In case the Controller is running inside a docker container, and we want to send the "shutdownServiceRequest" from the terminal (with curl), without entering inside the container,
|
||||||
// then the request will appear coming from a local IP (192.168.X.Y), instead of localhost.
|
// then the request will appear coming from a local IP (192.168.X.Y), instead of localhost.
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package eu.openaire.urls_controller.util;
|
package eu.openaire.urls_controller.util;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
@ -30,4 +31,13 @@ public class GenericUtils {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String getRequestorAddress(HttpServletRequest request)
|
||||||
|
{
|
||||||
|
String remoteAddr = request.getHeader("X-FORWARDED-FOR"); // This retrieves the original IP address, if the request passes through a proxy server.
|
||||||
|
if ( remoteAddr == null )
|
||||||
|
remoteAddr = request.getRemoteAddr();
|
||||||
|
return remoteAddr;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue