jamesfredley commented on code in PR #15698:
URL: https://github.com/apache/grails-core/pull/15698#discussion_r3353373728


##########
grails-profiles/base/commands/stop-app.groovy:
##########
@@ -1,101 +1,22 @@
-import javax.management.remote.JMXServiceURL
-import javax.management.remote.JMXConnectorFactory
-import javax.management.ObjectName
-import org.grails.io.support.*
-import groovy.jmx.GroovyMBean
-
 description("Stops the running Grails application") {
     usage "grails stop-app"
     synonyms 'stop'
-    flag name:'port', description:"Specifies the port which the Grails 
application is running on (defaults to 8080 or 8443 for HTTPS)"
-    flag name:'host', description:"Specifies the host the Grails application 
is bound to"
-}
-System.setProperty("run-app.running", "false")
-def getJMXLocalConnectorAddresses = {->
-       final applicationMainClassName = MainClassFinder.findMainClass()
-
-       if(applicationMainClassName) {
-               try {
-                   final String CONNECTOR_ADDRESS = 
"com.sun.management.jmxremote.localConnectorAddress"
-                   def VirtualMachine = 
getClass().classLoader.loadClass('com.sun.tools.attach.VirtualMachine')
-                   return VirtualMachine.list()
-                       .findAll { 
-                               it.displayName() == applicationMainClassName 
-                       }
-                       .collect { desc ->
-                           def vm = VirtualMachine.attach(desc.id())
-                           try {
-                               def connectorAddress = 
vm.agentProperties.getProperty(CONNECTOR_ADDRESS)
-                               if (connectorAddress == null) {
-                                   // Trying to load agent
-                                   def agent = 
[vm.systemProperties.getProperty("java.home"), "lib", 
"management-agent.jar"].join(File.separator)
-                                   vm.loadAgent(agent)
-
-                                   connectorAddress = 
vm.agentProperties.getProperty(CONNECTOR_ADDRESS)
-                               }
-                               if (connectorAddress) {
-                                   return connectorAddress
-                               }
-                           } finally {
-                               vm.detach()
-                           }
-                       }.findAll { it }
-                               
-               }
-               catch(Throwable e) {
-                       // fallback to REST request if JMX not available
-               }
-       }
 }
 
+System.setProperty("run-app.running", "false")
 
-def addresses = getJMXLocalConnectorAddresses() 
-if(addresses) {
-       JMXServiceURL url = new JMXServiceURL(addresses[0])
-       def connector = JMXConnectorFactory.connect(url)
-
-       try {
-           def server = connector.MBeanServerConnection
+console.updateStatus "Stopping application..."
 
-           def objectName = server.queryNames(null,null).find {  
it.canonicalName.contains('name=shutdownEndpoint,type=Endpoint') }
-           def mbean = new GroovyMBean(server, objectName)
-       console.addStatus "Shutting down application..."
-        mbean.shutdown()
-        console.addStatus "Application shutdown."
-        return true
+if (org.grails.cli.gradle.RunningApplicationRegistry.stopAll()) {

Review Comment:
   Correct - that was the core limitation. The previous version cancelled the 
in-memory Gradle build token, which only existed in the one CLI JVM, so it 
couldn't reach a forked app or an independent `grails stop-app`.
   
   This is now fixed in a53817cc4a: the forked `bootRun` app writes its own PID 
to `build/run-app.pid` (Spring Boot `ApplicationPidFileWriter`), and `stop-app` 
reads that file and stops the process with `ProcessHandle.destroy()` (graceful 
`SIGTERM` on Unix, best effort on Windows). Because the handoff is a file on 
disk, it works for forked processes and when `stop-app` is run from a different 
invocation/terminal than `run-app`. Details in the PR comment above.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to