From 26fecaa42f79176b8ebd84d8877eb632971d81df Mon Sep 17 00:00:00 2001 From: Michael Yan Date: Fri, 29 Nov 2024 17:15:55 +0800 Subject: [PATCH] Give help infomation to enable '/shutdown' endpoint when executing `stop-app` failed Closes gh-18 --- commands/stop-app.groovy | 47 ++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/commands/stop-app.groovy b/commands/stop-app.groovy index 1b0320b..098f500 100644 --- a/commands/stop-app.groovy +++ b/commands/stop-app.groovy @@ -1,6 +1,3 @@ -import javax.management.remote.JMXServiceURL -import javax.management.remote.JMXConnectorFactory -import javax.management.ObjectName import org.grails.io.support.* description("Stops the running Grace application") { @@ -9,6 +6,7 @@ description("Stops the running Grace application") { flag name:'port', description:"Specifies the port which the Grace application is running on (defaults to 8080 or 8443 for HTTPS)" flag name:'host', description:"Specifies the host the Grace application is bound to" } + System.setProperty("run-app.running", "false") Integer port = flag('port')?.toInteger() ?: config.getProperty('server.port', Integer) ?: 8080 @@ -16,24 +14,35 @@ String host = flag('host') ?: config.getProperty('server.address', String) ?: "l String contextPath = config.getProperty('server.context-path') ?: config.getProperty('server.contextPath') ?: "" String managementPath = config.getProperty('management.endpoints.web.base-path') ?: config.getProperty('management.endpoints.web.basePath') ?: "/actuator" console.updateStatus "Shutting down application..." + def url = new URL("http://$host:${port}${contextPath}${managementPath}/shutdown") try { - def connection = url.openConnection() - connection.setRequestMethod("POST") - connection.doOutput = true - connection.connect() - console.updateStatus connection.content.text - while(isServerAvailable(host, port)) { - sleep 100 - } - console.updateStatus "Application shutdown." - return true - + def connection = url.openConnection() + connection.setRequestMethod("POST") + connection.doOutput = true + connection.connect() + console.updateStatus connection.content.text + while(isServerAvailable(host, port)) { + sleep 100 + } + console.updateStatus "Application shutdown." + return true } -catch (e) { - console.error "Application not running.", e - return false +catch (java.net.ConnectException e) { + console.error "Application not running.", e + return true } - - +catch (java.io.FileNotFoundException ignored) { + console.info '''# Please check that '/shutdown' endpoint enabled in 'app/conf/application.yml' +management: + endpoint: + shutdown: + enabled: true +''' + return true +} +catch (Exception e) { + console.error "Application shutdown error: ", e + return true +}