Changeset 31813 for main

Show
Ignore:
Timestamp:
21.07.2017 18:37:30 (2 years ago)
Author:
ak19
Message:

Adding shutdownhook for GS2 to ensure the GS2 server is stopped on Ctrl-C. Tested on Linux.

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r31718 r31813  
    383383                if (!GS3) {      
    384384                    isLocalLibrary = LocalLibraryServer.start(gsdl_path, local_library_path); 
     385 
     386                    // Add a shutdown hook to take care of Ctrl-C situations 
     387                    // Sadly, neither the shutdownhook for GS3 nor for GS2 ever gets called on Windows 
     388                    // when a Ctrl-C is sent to the DOS prompt that launched GLI: 
     389                    // GLI on Windows currently waits to respond to a Ctrl-C until after GLI is already (properly) exited 
     390                    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 
     391                        public void run() { 
     392 
     393                            if(Gatherer.exit != true) { // unexpected termination, such as Ctrl-C won't set Gatherer.exit 
     394                            // so still need to at least issue the call to stop any running GS2 local library server 
     395                            System.err.println("ShutDownHook called..."); 
     396                            System.err.println("Attempting to forcibly terminate the GS2 server..."); 
     397                             
     398                            if (LocalLibraryServer.isRunning() == true) { 
     399                                LocalLibraryServer.forceStopServer(); 
     400                            } 
     401 
     402                             
     403                            } else { 
     404                            System.err.println("Gatherer.exit is false"); 
     405                            } 
     406                        } 
     407                        })); 
     408                     
    385409                } 
    386410                else if (!isGsdlRemote) { // local GS3, start the local tomcat 
     
    394418                    // https://coderanch.com/t/328888/java/Killing-process-spawned-Runtime-exec 
    395419                    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 
    396                         public void run() {                      
     420                        public void run() {                          
    397421 
    398422                            if(Gatherer.exit != true) { // unexpected termination, such as Ctrl-C won't set Gatherer.exit 
  • main/trunk/gli/src/org/greenstone/gatherer/greenstone/LocalLibraryServer.java

    r24906 r31813  
    3838import org.greenstone.gatherer.Gatherer; 
    3939import org.greenstone.gatherer.util.PortFinder; 
     40import org.greenstone.gatherer.util.SafeProcess; 
    4041import org.greenstone.gatherer.util.Utility; 
    4142 
     
    399400    if (isPersistentServer) { 
    400401        config(QUIT_COMMAND); 
    401     } else {         
     402    } else { 
    402403        boolean success = sendMessageToServer("QUIT\n"); 
    403404        try { 
     
    451452 
    452453    running = false; 
     454    } 
     455 
     456    // This method does the approximate equivalent of util.GSServerThread.stopServer() 
     457    // On unexpected, unnatural termination of GLI, call this to send the web-stop message the local library server to stop 
     458    static public void forceStopServer() { 
     459    SafeProcess p = null; 
     460    if (Utility.isWindows()) { 
     461        // cmd /C "cd "C:\path\to\greenstone3" && ant stop" 
     462        p = new SafeProcess("cmd /C \"cd \"" + Configuration.gsdl_path + File.separator + "\" && gsicontrol.bat web-stop\"");    
     463    } else { 
     464        p = new SafeProcess(new String[]{"/bin/bash", "-c", "cd \"" + Configuration.gsdl_path + "\" && ./gsicontrol.sh web-stop"}); 
     465    } 
     466    int result = p.runProcess(); 
     467    if(result == 0) { 
     468        System.err.println("Successfully stopped GS2 server."); 
     469        //DebugStream.println("********** SUCCESSFULLY stopped THE GS2 SERVER ON EXIT"); 
     470    } 
     471    else { 
     472        System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS2 SERVER ON EXIT"); 
     473    } 
     474    p = null; 
    453475    } 
    454476