Skip to content

Commit

Permalink
Partial fix for run-app in interactive mode, there seems to be anothe…
Browse files Browse the repository at this point in the history
…r problem whereby the tomcat server is not stopping correctly that needs to be investigated
  • Loading branch information
graemerocher committed Jun 8, 2011
1 parent 8e797f2 commit 98733cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 57 deletions.
Expand Up @@ -21,6 +21,7 @@ import grails.util.GrailsNameUtils
import org.codehaus.groovy.grails.cli.GrailsScriptRunner
import org.codehaus.groovy.grails.cli.ScriptNotFoundException
import org.codehaus.groovy.grails.cli.logging.GrailsConsole
import grails.util.BuildSettingsHolder

/**
* Provides the implementation of interactive mode in Grails
Expand All @@ -30,27 +31,55 @@ import org.codehaus.groovy.grails.cli.logging.GrailsConsole
*/
class InteractiveMode {

static InteractiveMode current

@Delegate GrailsConsole console = GrailsConsole.getInstance()

GrailsScriptRunner scriptRunner
BuildSettings settings
boolean active = false
def grailsServer

InteractiveMode(BuildSettings settings, GrailsScriptRunner scriptRunner) {
this.scriptRunner = scriptRunner
this.settings = settings;
BuildSettingsHolder.settings = settings
}

void setGrailsServer(grailsServer) {
addStatus "Application loaded in interactive mode. Type 'exit' to shutdown."
this.grailsServer = grailsServer
}

void run() {
current = this

console.reader.addCompletor(new GrailsInteractiveCompletor(settings, scriptRunner.availableScripts))
active = true

while(active) {
def scriptName = userInput("Enter a script name to run. Use TAB for completion: ")
try {
if(scriptName.trim()) {
if(scriptName.startsWith("!")) {
def trimmed = scriptName.trim()
if(trimmed.trim()) {
if("exit".equals(trimmed)) {
if(grailsServer) {
try {
updateStatus "Stopping Grails server"
grailsServer.stop()
} catch (e) {
error "Error stopping server: ${e.message}", e
}
finally {
grailsServer = null
}

}
else {
error "Grails server is not running, cannot stop."
}
}
else if(scriptName.startsWith("!")) {
try {
def process=new ProcessBuilder(scriptName[1..-1].split(" ")).redirectErrorStream(true).start()
log process.inputStream.text
Expand All @@ -73,7 +102,10 @@ class InteractiveMode {
}
} catch (ScriptNotFoundException e) {
error "Script not found for name $scriptName"
} catch (Exception e) {
error "Error running script $scriptName: ${e.message}", e
}

}
}
}
Expand Up @@ -107,6 +107,7 @@ class InlineExplodedTomcatServer extends TomcatServer {

void stop() {
tomcat.stop()
tomcat.destroy()
}

private loadInstance(String name) {
Expand Down
67 changes: 12 additions & 55 deletions scripts/_GrailsRun.groovy
Expand Up @@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.codehaus.groovy.grails.plugins.PluginManagerHolder

import org.codehaus.groovy.grails.cli.interactive.InteractiveMode
import org.codehaus.groovy.grails.cli.GrailsScriptRunner
import grails.util.GrailsUtil

Expand Down Expand Up @@ -102,8 +103,7 @@ private EmbeddableServerFactory loadServerFactory() {
}
}
catch (Throwable e) {
GrailsUtil.deepSanitize(e)
e.printStackTrace()
console.error e
event("StatusFinal", ["Failed to load container [$containerClass]: ${e.message}"])
exit(1)
}
Expand Down Expand Up @@ -163,7 +163,7 @@ runServer = { Map args ->
catch (Throwable t) {
GrailsUtil.deepSanitize(t)
if (!(t instanceof SocketException) && !(t.cause instanceof SocketException)) {
t.printStackTrace()
console.error t
}
event("StatusFinal", ["Server failed to start: $t"])
exit(1)
Expand Down Expand Up @@ -191,51 +191,12 @@ target(stopPluginScanner: "Stops the plugin manager's scanner that detects chang
target(watchContext: "Watches the WEB-INF/classes directory for changes and restarts the server if necessary") {
depends(classpath)

long lastModified = classesDir.lastModified()
boolean keepRunning = true
boolean isInteractive = System.getProperty("grails.interactive.mode") == "true"

if (isInteractive) {
def daemonThread = new Thread({
println "--------------------------------------------------------"
println "Application loaded in interactive mode, type 'exit' to shutdown server or your command name in to continue (hit ENTER to run the last command):"

def reader = new BufferedReader(new InputStreamReader(System.in))
def cmd = reader.readLine()
def scriptName
while (cmd != null) {
if (cmd == 'exit' || cmd == 'quit') break
if (cmd != 'run-app') {
scriptName = cmd ? GrailsScriptRunner.processArgumentsAndReturnScriptName(cmd) : scriptName
if (scriptName) {
def now = System.currentTimeMillis()
GrailsScriptRunner.callPluginOrGrailsScript(scriptName)
def end = System.currentTimeMillis()
println "--------------------------------------------------------"
println "Command [$scriptName] completed in ${end - now}ms"
}
}
else {
println "Cannot run the 'run-app' command. Server already running!"
}
try {
println "--------------------------------------------------------"
println "Application loaded in interactive mode, type 'exit' to shutdown server or your command name in to continue (hit ENTER to run the last command):"

cmd = reader.readLine()
}
catch (IOException e) {
cmd = ""
}
}

println "Stopping Grails server..."
grailsServer.stop()
keepRunning = false

})
daemonThread.daemon = true
daemonThread.run()
if (InteractiveMode.current) {
Thread.start {
def im = InteractiveMode.current
im.grailsServer = grailsServer
im.run()
}
}

keepServerAlive()
Expand Down Expand Up @@ -265,18 +226,14 @@ target(stopServer: "Stops the Grails servlet container") {
grailsServer.stop()
}
catch (Throwable e) {
GrailsUtil.deepSanitize(e)
e.printStackTrace()
console.error "Error stopping server: ${e.message}"
console.error "Error stopping server: ${e.message}", e
}

try {
stopPluginScanner()
}
catch (Throwable e) {
GrailsUtil.deepSanitize(e)
e.printStackTrace()
console.error "Error stopping plugin change scanner: ${e.message}"
console.error "Error stopping plugin change scanner: ${e.message}", e
}
}
event("StatusFinal", ["Server stopped"])
Expand Down

0 comments on commit 98733cf

Please sign in to comment.