You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by James Wang <Ja...@infor.com> on 2012/05/31 14:37:09 UTC

Commons daemon - stop the service

I need some insights on how procrun shuts down an application. Procrun has a parameter called "StopTimeout" which Defines the timeout in seconds that procrun waits for service to exit gracefully. What happens after the timeout period? Doe procrun shut down the JVM?
A more general question is: how do the involved parties interact with one another - JVM, procrun, and Windows service manager? I have been perplexed by two errors: one says the process ended unexpectedly. The other says service manager could not get a response from procrun (after a long wait). In both cases, the event viewer showed errors related to prunsrv.exe. These errors are more associated with the 32 bit version. AMD64 seems to work a better.
Any suggestions?
Thanks.

James Wang | Software Engineer, Development | [Description: Description: infor-logo-esig] | direct: 734.623.4056 | fax: 734.623.4190 | James.Wang@infor.com<ma...@infor.com>


RE: Commons daemon - stop the service

Posted by Martin Gainty <mg...@hotmail.com>.
a quick scan of the source gives us some cluesif  PID is not there procrun exits with a -1fd = open(args->pidf, O_RDONLY, 0);
    if (!quiet)
        log_debug("get_pidf: %d in %s", fd, args->pidf);
    if (fd < 0) {
        /* something has gone wrong the JVM has stopped */
        return -1;
    } later on with wait_child we check again if PID is not alive in which case a 1 is returned  /* check if the pid file process exists */
        fd = open(args->pidf, O_RDONLY);
        if (fd < 0 && havejvm) {
            /* something has gone wrong the JVM has stopped */
            return 1;
        }else{ ...return 0; /* ready JVM started ..0 is returned to internal functions*/} /*if procrun returns 0 internally you're JVM is started*/  /*.. the chronology for procrun denoument..*/ stop_child() while (count > 0) {
            sleep(1);
            pid = get_pidf(args, true);
            if (pid <= 0) {
                /* JVM has stopped... the pid is non-existent*/
                return 0;
            }
            count--;
        }
        signal(SIGTERM, controller);
        signal(SIGINT, controller);        while (waitpid(pid, &status, 0) != pid) {
            /* Waith for process */
        }        /* The child must have exited cleanly */
        if (WIFEXITED(status)) {
            status = WEXITSTATUS(status);            /* Delete the pid file */
            if (args->vers != true && args->chck != true && status != 122)
                unlink(args->pidf);            /* If the child got out with 123 he wants to be restarted */
            /* See java_abort123 (we use this return code to restart when the JVM aborts) */
            if (status == 123) {
                log_debug("Reloading service");
                /* prevent looping */
                if (laststart + 60 > time(NULL)) {
                    log_debug("Waiting 60 s to prevent looping");
                    sleep(60);
                }
                continue;
            }
            /* If the child got out with 0 he is shutting down */
            if (status == 0) {
                log_debug("Service shut down");
                return 0;
            }    remove_tmp_file(args);
    log_debug("Shutdown or reload requested: exiting");    /* Stop the service */
    if (java_stop() != true)
        return 6;  /*cannot stop java*/
    if (doreload == true)
        ret = 123;
    else
        ret = 0;    /* Destroy the service */
    java_destroy();    /* Destroy the Java VM */
    if (JVM_destroy(ret) != true)
        return 7;

 yes if all goes well the JVM should be destroyed
Martin Gainty 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 Ez az
üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és
semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek
könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
ezen üzenet tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.

 > From: James.Wang@infor.com
> To: user@commons.apache.org
> Subject: RE: Commons daemon - stop the service
> Date: Thu, 7 Jun 2012 18:06:03 +0000
> 
> I have played with procrun a bit more and realized that SCM waits for at most two minutes. After that, it shows a message saying it got no response from the process and displays "stopping" as status on the service. But when the process finally ends, it shows "stopped" status (you have to manually refresh the SCM). 
> Procrun has a parameter "StopTimeout", which "Defines the timeout in seconds that procrun waits for service to exit gracefully."  What happens after the timeout period? Does procrun shut down the JVM?
> Thanks.
> 
> James
> 
> -----Original Message-----
> From: Mladen Turk [mailto:mturk@apache.org] 
> Sent: Thursday, May 31, 2012 2:16 PM
> To: user@commons.apache.org
> Subject: Re: Commons daemon - stop the service
> 
> On 05/31/2012 07:53 PM, James Wang wrote:
> > Thanks for the comments.
> > I set both start mode and stop mode to JVM.
> 
> OK.
> 
> > What will happen when a thread needs 20 more minutes to complete its job?
> 
> Well for something like that procrun is not the toolkit to use.
> First of all, if you need to frequently start/stop service why using the service at the first place?
> The sole purpose of service concept is to start the application at boot mode or on demand and to start them in correct order.
> 
> Then there is absolute limit which OS imposes on the service shutdown timeout, and this is for all services on the system, see http://support.microsoft.com/kb/Q146092
> 
> So the only clean solution is that your shutdown code does the actual cleanup causing the clean exit.
> 
> By spec, JVM won't exit if you have non-daemon threads running, so if you cant make them exit, then the only solution is System.exit, which calls OS process _exit() which causes the SCM to think service failed.
> 
> Also you should design your application differently for service and command line mode.
> 
> Procrun does not need to call the main() method.
> Take a look at Tomcat. We call Bootstrap start/stop methods and main() calls Bootstrap.start (after setting correct mode)
> 
> 
> 
> Regards
> --
> ^TM
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 
 		 	   		  

RE: Commons daemon - stop the service

Posted by James Wang <Ja...@infor.com>.
I have played with procrun a bit more and realized that SCM waits for at most two minutes. After that, it shows a message saying it got no response from the process and displays "stopping" as status on the service. But when the process finally ends, it shows "stopped" status (you have to manually refresh the SCM). 
Procrun has a parameter "StopTimeout", which "Defines the timeout in seconds that procrun waits for service to exit gracefully."  What happens after the timeout period? Does procrun shut down the JVM?
Thanks.

James

-----Original Message-----
From: Mladen Turk [mailto:mturk@apache.org] 
Sent: Thursday, May 31, 2012 2:16 PM
To: user@commons.apache.org
Subject: Re: Commons daemon - stop the service

On 05/31/2012 07:53 PM, James Wang wrote:
> Thanks for the comments.
> I set both start mode and stop mode to JVM.

OK.

> What will happen when a thread needs 20 more minutes to complete its job?

Well for something like that procrun is not the toolkit to use.
First of all, if you need to frequently start/stop service why using the service at the first place?
The sole purpose of service concept is to start the application at boot mode or on demand and to start them in correct order.

Then there is absolute limit which OS imposes on the service shutdown timeout, and this is for all services on the system, see http://support.microsoft.com/kb/Q146092

So the only clean solution is that your shutdown code does the actual cleanup causing the clean exit.

By spec, JVM won't exit if you have non-daemon threads running, so if you cant make them exit, then the only solution is System.exit, which calls OS process _exit() which causes the SCM to think service failed.

Also you should design your application differently for service and command line mode.

Procrun does not need to call the main() method.
Take a look at Tomcat. We call Bootstrap start/stop methods and main() calls Bootstrap.start (after setting correct mode)



Regards
--
^TM

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Commons daemon - stop the service

Posted by Mladen Turk <mt...@apache.org>.
On 05/31/2012 07:53 PM, James Wang wrote:
> Thanks for the comments.
> I set both start mode and stop mode to JVM.

OK.

> What will happen when a thread needs 20 more minutes to complete its job?

Well for something like that procrun is not the toolkit to use.
First of all, if you need to frequently start/stop service why using
the service at the first place?
The sole purpose of service concept is to start the application
at boot mode or on demand and to start them in correct order.

Then there is absolute limit which OS imposes on the service
shutdown timeout, and this is for all services on the system,
see http://support.microsoft.com/kb/Q146092

So the only clean solution is that your shutdown code does
the actual cleanup causing the clean exit.

By spec, JVM won't exit if you have non-daemon threads running,
so if you cant make them exit, then the only solution is
System.exit, which calls OS process _exit() which causes
the SCM to think service failed.

Also you should design your application differently for
service and command line mode.

Procrun does not need to call the main() method.
Take a look at Tomcat. We call Bootstrap start/stop methods
and main() calls Bootstrap.start (after setting correct mode)



Regards
-- 
^TM

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


RE: Commons daemon - stop the service

Posted by James Wang <Ja...@infor.com>.
Thanks for the comments.
I set both start mode and stop mode to JVM. My test showed that worked best for me. The procrun documentation says that to use this mode, the start method should not stop until stop is called. My start method exits immediately but it did not seem to be the cause of my grievances. My stop code also calls System.exit() in a separate thread, after couple of seconds of sleep. I cannot get rid of it because some of our customers still want to start and stop the application using batch scripts. But the stop did not fail consistently with "unexpected" stoppage. That makes me think that there is an issue of timing. 
What I have recently tried is to terminate most, if not all, threads spawned by the applications gracefully. That seemed to have improved the performance (fewer error messages from service manager). But my testing is done when the app is not loaded with much work. What will happen when a thread needs 20 more minutes to complete its job? Would procrun be patient enough to wait (if I set the timeout to 20 minutes)? It is hard to test this scenario because I have not been able to put a heavy load on the application. So I would like know what actually happens after procrun calls the stop class? Does it monitor the JVM in some way? Does procrun eventually call System.exit() to shut down JVM?
Thanks.

James Wang

-----Original Message-----
From: Mladen Turk [mailto:mturk@apache.org] 
Sent: Thursday, May 31, 2012 12:15 PM
To: user@commons.apache.org
Subject: Re: Commons daemon - stop the service

On 05/31/2012 02:37 PM, James Wang wrote:
> I need some insights on how procrun shuts down an application. Procrun 
> has a parameter called "StopTimeout" which Defines the timeout in seconds that procrun waits for service to exit gracefully. What happens after the timeout period? Doe procrun shut down the JVM?
>
> A more general question is: how do the involved parties interact with 
> one another - JVM, procrun, and Windows service manager? I have been perplexed by two errors: one says the process ended unexpectedly. The other says service manager could not get a response from procrun (after a long wait). In both cases, the event viewer showed errors related to prunsrv.exe. These errors are more associated with the 32 bit version. AMD64 seems to work a better.
>

Depends on the mode you select.
In general service stop is treated as separate process which should by some IPC mechanism inform the 'service' to stop.

The simplest one is System.exit, which just calls System.exit() for the service.
More complex involves either special process or method call which must cause main service to exit cleanly.

Shutdown timeout can occur if any of those goes over defined timeout.
Service code should never call System.exit by itself cause that can lead to 'process ended unexpectedly'.
Reason is because 'stop' waits for 'start' to finish and then 'stopped'
is reported to service manager.

So if your service code uses system.exit then you must use java mode which spawns a separate process and stop is then program which must by some IPC (usually socket) inform the 'master' to shut down.

With jvm mode all that happens in the same process (and the same JVM) thus your stop code must set some variable in main program which will cause clean exit from main method. This can be tricky if you depend on daemon threads.

Hope that shares some light.


Regards
--
^TM

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Commons daemon - stop the service

Posted by Mladen Turk <mt...@apache.org>.
On 05/31/2012 02:37 PM, James Wang wrote:
> I need some insights on how procrun shuts down an application. Procrun has a parameter called “StopTimeout” which Defines the timeout in seconds that procrun waits for service to exit gracefully. What happens after the timeout period? Doe procrun shut down
> the JVM?
>
> A more general question is: how do the involved parties interact with one another – JVM, procrun, and Windows service manager? I have been perplexed by two errors: one says the process ended unexpectedly. The other says service manager could not get a
> response from procrun (after a long wait). In both cases, the event viewer showed errors related to prunsrv.exe. These errors are more associated with the 32 bit version. AMD64 seems to work a better.
>

Depends on the mode you select.
In general service stop is treated as separate process which should by some IPC
mechanism inform the 'service' to stop.

The simplest one is System.exit, which just calls System.exit() for the service.
More complex involves either special process or method call which
must cause main service to exit cleanly.

Shutdown timeout can occur if any of those goes over defined timeout.
Service code should never call System.exit by itself cause that can
lead to 'process ended unexpectedly'.
Reason is because 'stop' waits for 'start' to finish and then 'stopped'
is reported to service manager.

So if your service code uses system.exit then you must use java mode
which spawns a separate process and stop is then program which must
by some IPC (usually socket) inform the 'master' to shut down.

With jvm mode all that happens in the same process (and the same JVM)
thus your stop code must set some variable in main program which will
cause clean exit from main method. This can be tricky if you depend on
daemon threads.

Hope that shares some light.


Regards
-- 
^TM

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org