in reply to Re: Restarting a script without dropping clients
in thread Restarting a script without dropping clients

You can't KILL a script, start a new instance of that script and pass open TCP socket connections from the deceased script to the new process easily. You can fork of course but all that gives you is a copy of the existing process running at the same point so it does not solve your issue.

You say reload the script completely but you don't seem to want that. You want to reinitialize some stuff but keep some stuff in the running code. You can start a brand new script (mutating the running one into it) with exec() but I doubt this is what you want.

If the issue is maintaining a login state then use some sort of persistent session arrangement, almost everyone has written one ;-) but there are lots of good ones on CPAN depending on context.

An alternative would still be to use the HUP signal but arrange it so that when a process recieves the HUP:

  1. it handles any existing connections
  2. does not accept any new ones
  3. exits when done

You can then start a new process which will handle new connections while the old process handles the existing ones. This is the basis of the apache graceful command. The major issue you will probably have is that the old process may be bound to a particular listen port and thus prevent the new process from starting......

cheers

tachyon

  • Comment on Re: Re: Restarting a script without dropping clients

Replies are listed 'Best First'.
Re: Re: Re: Restarting a script without dropping clients
by simonm (Vicar) on Dec 15, 2003 at 18:35 UTC
    You can't KILL a script, start a new instance of that script and pass open TCP socket connections from the deceased script to the new process easily.

    Agreed.

    You say reload the script completely but you don't seem to want that. You want to reinitialize some stuff but keep some stuff in the running code.

    Depending on the code, and based on the OP's question, it may be sufficient to reload any modules that have changed on disk, and perhaps also any configuration files.

    Tachyon's earlier post gives a good skeleton for this; what's missing is something to reload any modules that have changed. I think that a hacked copy of Apache::StatINC would do the job -- take the code from handler sub, without the first four lines which are Apache-specific.

    However, note that reloading the code this way is a bit unusual, and some of your code may need to be readjusted a bit to avoid some "gotchas," such as re-initializing package variables, leaving dangling references to old data, or other unexpected surprises.