From 5a57ea64be3e7ab88a1a6893ee82c795d4d3ee1b Mon Sep 17 00:00:00 2001 From: ipatini Date: Wed, 24 Nov 2021 11:19:51 +0200 Subject: [PATCH] EMS: Control Service: Added EMS server exit endpoint. Modified run.* to support restart --- event-management/bin/run.bat | 9 +++++++ event-management/bin/run.sh | 16 +++++++++---- .../control/ControlServiceApplication.java | 24 ++++++++++++------- .../control/ControlServiceController.java | 22 +++++++++++++---- .../control/ControlServiceCoordinator.java | 7 +++++- 5 files changed, 59 insertions(+), 19 deletions(-) diff --git a/event-management/bin/run.bat b/event-management/bin/run.bat index 705597fad..d156520fa 100644 --- a/event-management/bin/run.bat +++ b/event-management/bin/run.bat @@ -62,12 +62,21 @@ rem set JAVA_OPTS=-Djavax.net.debug=all echo MELODIC_CONFIG_DIR=%MELODIC_CONFIG_DIR% echo Starting EMS server... +IF NOT DEFINED RESTART_EXIT_CODE set RESTART_EXIT_CODE=99 +:_restart_ems + rem Use when Esper is packaged in control-service.jar rem java %JAVA_OPTS% -Djasypt.encryptor.password=%JASYPT_PASSWORD% -Duser.timezone=Europe/Warsaw -Djava.security.egd=file:/dev/urandom -jar %JARS_DIR%\control-service.jar --logging.config=file:%LOG_CONFIG_FILE% rem Use when Esper is NOT packaged in control-service.jar java %JAVA_OPTS% -Djasypt.encryptor.password=%JASYPT_PASSWORD% -Duser.timezone=Europe/Warsaw -Djava.security.egd=file:/dev/urandom -cp %JARS_DIR%\control-service.jar -Dloader.path=%JARS_DIR%\esper-7.1.0.jar org.springframework.boot.loader.PropertiesLauncher -nolog --logging.config=file:%LOG_CONFIG_FILE% %* +if errorlevel %RESTART_EXIT_CODE% ( + echo Restarting EMS server... + goto :_restart_ems +) +echo EMS server exited + rem e.g. --spring.config.location=%MELODIC_CONFIG_DIR%\ rem e.g. --spring.config.name=application.properties diff --git a/event-management/bin/run.sh b/event-management/bin/run.sh index bf17867e3..7aef86f03 100755 --- a/event-management/bin/run.sh +++ b/event-management/bin/run.sh @@ -66,11 +66,19 @@ fi echo "MELODIC_CONFIG_DIR=${MELODIC_CONFIG_DIR}" echo "Starting EMS server..." -# Use when Esper is packaged in control-service.jar -# java $JAVA_OPTS -Djasypt.encryptor.password=$JASYPT_PASSWORD -Duser.timezone=Europe/Warsaw -Djava.security.egd=file:/dev/urandom -jar $JARS_DIR/control-service/target/control-service.jar --logging.config=file:$LOG_CONFIG_FILE +if [[ -z $RESTART_EXIT_CODE ]]; then RESTART_EXIT_CODE=99; export RESTART_EXIT_CODE; fi +retCode=$RESTART_EXIT_CODE +while :; do + # Use when Esper is packaged in control-service.jar + # java $JAVA_OPTS -Djasypt.encryptor.password=$JASYPT_PASSWORD -Duser.timezone=Europe/Warsaw -Djava.security.egd=file:/dev/urandom -jar $JARS_DIR/control-service/target/control-service.jar --logging.config=file:$LOG_CONFIG_FILE -# Use when Esper is NOT packaged in control-service.jar -java $JAVA_OPTS -Djasypt.encryptor.password=$JASYPT_PASSWORD -Duser.timezone=Europe/Warsaw -Djava.security.egd=file:/dev/urandom -cp ${JARS_DIR}/control-service.jar -Dloader.path=${JARS_DIR}/esper-7.1.0.jar org.springframework.boot.loader.PropertiesLauncher --logging.config=file:$LOG_CONFIG_FILE $* + # Use when Esper is NOT packaged in control-service.jar + java $JAVA_OPTS -Djasypt.encryptor.password=$JASYPT_PASSWORD -Duser.timezone=Europe/Warsaw -Djava.security.egd=file:/dev/urandom -cp ${JARS_DIR}/control-service.jar -Dloader.path=${JARS_DIR}/esper-7.1.0.jar org.springframework.boot.loader.PropertiesLauncher --logging.config=file:$LOG_CONFIG_FILE $* + + retCode=$? + if [[ $retCode -eq $RESTART_EXIT_CODE ]]; then echo "Restarting EMS server..."; else break; fi +done +echo "EMS server exited" # Extra parameters # e.g. --spring.config.location=$MELODIC_CONFIG_DIR diff --git a/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceApplication.java b/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceApplication.java index 3913e1cde..923581db5 100644 --- a/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceApplication.java +++ b/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceApplication.java @@ -91,22 +91,28 @@ public class ControlServiceApplication implements ApplicationContextAware { synchronized static void exitApp(int exitCode, long gracePeriod) { if (exitTimer==null) { - // Close SpringBoot application - log.info("ControlServiceApplication.exitApp(): Closing application context..."); - ExitCodeGenerator exitCodeGenerator = () -> exitCode; - SpringApplication.exit(applicationContext, exitCodeGenerator); - - // Wait for 'gracePeriod' seconds before force JVM to exit + // Wait for 'gracePeriod' seconds before forcing JVM to exit log.info("ControlServiceApplication.exitApp(): Wait for {}sec before exit", gracePeriod); - exitTimer = new Timer("exit-app-grace-period-timer", true); + exitTimer = new Timer("exit-timer", true); exitTimer.schedule(new TimerTask() { @Override public void run() { - log.info("ControlServiceApplication.exitApp(): Exiting..."); + log.info("ControlServiceApplication.exitApp(): exit-timer: Exiting with code: {}", exitCode); System.exit(exitCode); - log.info("ControlServiceApplication.exitApp(): Bye"); + log.info("ControlServiceApplication.exitApp(): exit-timer: Bye"); } }, gracePeriod * 1000); + + // Close SpringBoot application + log.info("ControlServiceApplication.exitApp(): Closing application context..."); + ExitCodeGenerator exitCodeGenerator = () -> { + log.info("ControlServiceApplication.exitApp(): exitCodeGenerator: Exit code: {}", exitCode); + return exitCode; + }; + SpringApplication.exit(applicationContext, exitCodeGenerator); + log.info("ControlServiceApplication.exitApp(): Exiting with code: {}", exitCode); + System.exit(exitCode); + } else { log.warn("ControlServiceApplication.exitApp(): Exit timer has already started: {}", exitTimer); } diff --git a/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceController.java b/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceController.java index 87d88092a..9d0b932d7 100644 --- a/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceController.java +++ b/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceController.java @@ -433,15 +433,27 @@ public class ControlServiceController { // EMS status and information query methods // ------------------------------------------------------------------------------------------------------------ - @RequestMapping(value = { "/ems/shutdown", "/ems/shutdown/{exitApp}" }, method = {GET, POST}) - public String emsShutdown(@PathVariable Optional exitApp) { - boolean _exitApp = exitApp.orElse(false); - log.info("ControlServiceController.emsShutdown(): exitApp={}", _exitApp); + @RequestMapping(value = "/ems/shutdown", method = {GET, POST}) + public String emsShutdown() { + log.info("ControlServiceController.emsShutdown(): "); coordinator.emsShutdown(); - if (_exitApp) coordinator.emsExit(); return "OK"; } + @RequestMapping(value = { "/ems/exit", "/ems/exit/{exitCode}" }, method = {GET, POST}) + public String emsExit(@PathVariable Optional exitCode) { + if (properties.isExitAllowed()) { + int _exitCode = exitCode.orElse(properties.getExitCode()); + log.info("ControlServiceController.emsExit(): exitCode={}", _exitCode); + coordinator.emsShutdown(); + coordinator.emsExit(_exitCode); + return "OK"; + } else { + log.info("ControlServiceController.emsExit(): Exiting EMS is not allowed"); + return "NOT ALLOWED"; + } + } + @RequestMapping(value = "/ems/status", method = {GET, POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public String emsStatus() { diff --git a/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceCoordinator.java b/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceCoordinator.java index 3fa702d22..9f528cdda 100644 --- a/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceCoordinator.java +++ b/event-management/control-service/src/main/java/eu/melodic/event/control/ControlServiceCoordinator.java @@ -685,10 +685,15 @@ public class ControlServiceCoordinator { @Async synchronized void emsExit() { + emsExit(properties.getExitCode()); + } + + @Async + synchronized void emsExit(int exitCode) { if (properties.isExitAllowed()) { // Signal SpringBootApp to exit log.info("ControlServiceCoordinator.emsExit(): Signaling exit..."); - ControlServiceApplication.exitApp(properties.getExitCode(), properties.getExitGracePeriod()); + ControlServiceApplication.exitApp(exitCode, properties.getExitGracePeriod()); log.info("ControlServiceCoordinator.emsExit(): Signaling exit... done"); } else { log.warn("ControlServiceCoordinator.emsExit(): Exit is not allowed"); -- GitLab