diff --git a/README.md b/README.md index 6959edb..ca06072 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,11 @@ The Worker responds by compressing and sending the requested files in each batch
To install and run the application: - Run ```git clone``` and then ```cd UrlsWorker```. -- [Optional] Create the file ```inputData.txt``` , which contains just one line with the ___workerId___, the __maxAssignmentsLimitPerBatch__, the __maxAssignmentsBatchesToHandleBeforeRestart__ and the ___controller's base api-url___, all seperated by a _comma_ ```,``` . For example: ```worker_1,1000,0,http://IP:PORT/api/```. -- Execute the ```installAndRun.sh``` script. In case the above file (_inputData.txt_) does not exist, it will request the required data from the user, and then it will create the _inputData.txt_ file.
+- [Optional] Create the file ```inputData.txt``` , which contains just one line with the ___workerId___, the ___maxAssignmentsLimitPerBatch___, the ___maxAssignmentsBatchesToHandleBeforeRestart___, the ___controller's base api-url___ and the ___shutdownOrCancelCode___, all seperated by a _comma_ "```,```" .
+ For example: ```worker_1,1000,0,http://IP:PORT/api/,stopOrCancelCode```.
+ The ___shutdownOrCancelCode___ is kind of an "auth-code", when receiving "__shutdown__" and "__cancel-shutdown__" requests. +- Execute the ```installAndRun.sh``` script.
+ In case the above file (_inputData.txt_) does not exist, the script will request the required data from the user, and then it will create the _inputData.txt_ file.

Notes: diff --git a/installAndRun.sh b/installAndRun.sh index bc9bdf9..d47e6cc 100755 --- a/installAndRun.sh +++ b/installAndRun.sh @@ -26,8 +26,11 @@ if [[ ! -f $inputDataFile ]]; then echo -e "\nGive the baseUrl of the controller (e.g.: http://IP:PORT/api/):" read -r controllerBaseUrl + echo -e "\nGive the shutdownOrCancelCode, as an \"auth-code\", when receiving \"shutdown\" and \"cancel-shutdown\" requests.:" + read -r shutdownOrCancelCode + touch $inputDataFile - echo "$workerId,$maxAssignmentsLimitPerBatch,$maxAssignmentsBatchesToHandleBeforeRestart,$controllerBaseUrl" >> $inputDataFile + echo "$workerId,$maxAssignmentsLimitPerBatch,$maxAssignmentsBatchesToHandleBeforeRestart,$controllerBaseUrl,$shutdownOrCancelCode" >> $inputDataFile echo -e "\n\n" fi diff --git a/src/main/java/eu/openaire/urls_worker/UrlsWorkerApplication.java b/src/main/java/eu/openaire/urls_worker/UrlsWorkerApplication.java index 41ceb1b..253e875 100644 --- a/src/main/java/eu/openaire/urls_worker/UrlsWorkerApplication.java +++ b/src/main/java/eu/openaire/urls_worker/UrlsWorkerApplication.java @@ -43,6 +43,7 @@ public class UrlsWorkerApplication { public static int maxAssignmentsLimitPerBatch = 0; public static int maxAssignmentsBatchesToHandleBeforeShutdown = -1; // Default value: -1 = argument-absent, 0 = infinite-batches public static String controllerBaseUrl = null; // BaseUrl template: "http://IP:PORT/api/" + public static String shutdownOrCancelCode = null; private static ConfigurableApplicationContext context; @@ -129,8 +130,10 @@ public class UrlsWorkerApplication { myReader = new Scanner(inputDataFile); if ( myReader.hasNextLine() ) { String[] data = myReader.nextLine().split(","); - if ( data.length < 4 ) { - String errorMsg = "Not all data were retrieved from file \"" + inputDataFilePath + "\"!"; + if ( data.length < 5 ) { + String errorMsg = "Not all data was retrieved from file \"" + inputDataFilePath + "\"!\n" + + "The file should contain the \"workerId\", the \"maxAssignmentsLimitPerBatch\", the \"maxAssignmentsBatchesToHandleBeforeShutdown\", " + + "the \"controllerBaseUrl\" and the \"shutdownOrCancelCode\", all seperated by a comma \",\"."; logger.error(errorMsg); System.err.println(errorMsg); System.exit(61); @@ -161,19 +164,24 @@ public class UrlsWorkerApplication { } if ( !controllerBaseUrl.endsWith("/") ) controllerBaseUrl += "/"; // Make sure the other urls will not break later. + + shutdownOrCancelCode = data[4].trim(); } - if ( (workerId == null) || (maxAssignmentsLimitPerBatch == 0) || (maxAssignmentsBatchesToHandleBeforeShutdown == -1) || (controllerBaseUrl == null) ) { - String errorMsg = "No \"workerId\" or/and \"maxAssignmentsLimitPerBatch\" or/and \"maxAssignmentsBatchesToHandleBeforeRestart\" or/and \"controllerBaseUrl\" could be retrieved from the file: " + inputDataFilePath; + if ( (workerId == null) || (maxAssignmentsLimitPerBatch == 0) || (maxAssignmentsBatchesToHandleBeforeShutdown == -1) || (controllerBaseUrl == null) + || (shutdownOrCancelCode == null) ) + { + String errorMsg = "No \"workerId\" or/and \"maxAssignmentsLimitPerBatch\" or/and \"maxAssignmentsBatchesToHandleBeforeRestart\" or/and \"controllerBaseUrl\" or/and \"shutdownOrCancelCode\" could be retrieved from the file: " + inputDataFilePath; logger.error(errorMsg); System.err.println(errorMsg); System.exit(63); } - logger.info("workerId: " + workerId + ", maxAssignmentsLimitPerBatch: " + maxAssignmentsLimitPerBatch + ", maxAssignmentsBatchesToHandleBeforeRestart: " + maxAssignmentsBatchesToHandleBeforeShutdown + ", controllerBaseUrl: " + controllerBaseUrl); // It's safe and helpful to show them in the logs. + logger.info("workerId: " + workerId + ", maxAssignmentsLimitPerBatch: " + maxAssignmentsLimitPerBatch + ", maxAssignmentsBatchesToHandleBeforeRestart: " + maxAssignmentsBatchesToHandleBeforeShutdown + ", controllerBaseUrl: " + controllerBaseUrl + ", shutdownOrCancelCode: "); + // It's safe and helpful to show all but the last variable, in the logs (keep in mind that the developer might not always be the end-user). } catch (Exception e) { - String errorMsg = "An error prevented the retrieval of the workerId and the controllerBaseUrl from the file: " + inputDataFilePath + "\n" + e.getMessage(); + String errorMsg = "An error prevented the retrieval of data from the file: " + inputDataFilePath + "\n" + e.getMessage(); logger.error(errorMsg, e); System.err.println(errorMsg); System.exit(64); diff --git a/src/main/java/eu/openaire/urls_worker/controllers/GeneralController.java b/src/main/java/eu/openaire/urls_worker/controllers/GeneralController.java index 25a993e..04a6bcc 100644 --- a/src/main/java/eu/openaire/urls_worker/controllers/GeneralController.java +++ b/src/main/java/eu/openaire/urls_worker/controllers/GeneralController.java @@ -1,8 +1,8 @@ package eu.openaire.urls_worker.controllers; +import eu.openaire.urls_worker.UrlsWorkerApplication; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -21,9 +21,6 @@ public class GeneralController { private static final Logger logger = LoggerFactory.getLogger(GeneralController.class); - @Value("${security.shutdownOrCancelCode}") - private String shutdownOrCancelCode; - public GeneralController() {} @@ -41,7 +38,7 @@ public class GeneralController { public ResponseEntity shutdownWorkerGracefully(@PathVariable String shutdownCode) { String initMsg = "Received a \"shutdownWorker\" request."; - if ( shutdownCode.equals(shutdownOrCancelCode) ) { + if ( shutdownCode.equals(UrlsWorkerApplication.shutdownOrCancelCode) ) { shouldShutdownWorker = true; logger.info(initMsg + " The worker will shutdown, after finishing current work."); return ResponseEntity.ok().build(); @@ -57,7 +54,7 @@ public class GeneralController { public ResponseEntity cancelShutdownWorkerGracefully(@PathVariable String cancelCode) { String initMsg = "Received a \"cancelShutdownWorker\" request."; - if ( cancelCode.equals(shutdownOrCancelCode) ) { + if ( cancelCode.equals(UrlsWorkerApplication.shutdownOrCancelCode) ) { 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(); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index cf37dbd..edb61fd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -15,9 +15,6 @@ server.port = 1881 # Server api path server.servlet.context-path=/api -# Here set the code to be checked, when receiving "shutdown" and "cancel-shutdown" requests. -security.shutdownOrCancelCode= - # LOGGING LEVELS logging.config=classpath:logback-spring.xml