[Backgroundrb-devel] Starting / stopping background rb when apache starts/stops

Marc Evans Marc at SoftwareHackery.Com
Fri Feb 23 12:49:25 EST 2007


On Fri, 16 Feb 2007, Marc Evans wrote:

> Hello -
>
> I would like to figure out a way to have backgroundrb start/stop with the 
> same UID that dispatch.fcgi uses, at the same point in time that apache is 
> started/stopped. I see a technique that seems plausable in this archive 
> message:
>
> http://rubyforge.org/pipermail/backgroundrb-devel/2007-February/000723.html
>
> which addresses the starting aspect, in a lazy fashion. That seems fine, 
> particularly given it's resilience to unexpected terminations of the 
> backgroundrb processes.
>
> What I have not seen any discussion of is how to insure that backgroundrb is 
> terminated (ideally cleanly) at the point in time that apache (or whatever 
> web server you use) is terminated. In other words, I am hoping to use 
> "apachectl {start|stop}" to control dispatch.fcgi and backgroundrb.
>
> Does anyone have any suggestions?

In response to my own message, but in hopes of either being told that I am 
doing idiotic things, or helping others with the same problem, here is the 
solution that I have finally come up with.

My answer was to use the following code within my config/environment.rb 
file:

require 'pathname'
BACKGROUNDRB_STANDALONE = false
BACKGROUNDRB_ROOT = Pathname.new(RAILS_ROOT).realpath.to_s
BACKGROUNDRB_CODE = BACKGROUNDRB_ROOT + '/vendor/plugins/backgroundrb'
$LOAD_PATH << File.join(BACKGROUNDRB_CODE + '/lib')
$LOAD_PATH << File.join(BACKGROUNDRB_CODE + '/server/lib')
require 'backgroundrb_server'
Process.fork {
   # Code lifted from script/backgroundrb
   ARGV.clear
   ARGV << 'start'
   exit(0) if File.exists?(BACKGROUNDRB_ROOT + '/log/backgroundrb.ppid');
   BackgrounDRb::Server.new.run
}
at_exit {
   ARGV.clear
   ARGV << 'stop'
   exit(0) unless File.exists?(BACKGROUNDRB_ROOT + '/log/backgroundrb.ppid');
   BackgrounDRb::Server.new.run
}

For reasons not understood at this time, I am finding that invoking 
script/backgroundrb {start|stop} does not work. Copying chunks of the code 
as shown above, does.

Yes, there are some holes, such as relying on the ppid file and not 
checking if the pid is actually running; such as relying on at_exit to 
perform proper cleanup; such as not cleaning up open file descriptors in 
the forked child prior to calling run.

If you have better suggestions, I would love to hear them.

Thanks in advance - Marc


More information about the Backgroundrb-devel mailing list