Creating fresh daemons
May 22nd, 2007
If you need to create daemons in Ruby then I would recommend having a look at the Daemons gem. One problem with using Daemon.call is that everything that has been loaded by the parent process will still be loaded by the new daemon. This can create some annoying problems when the daemon isn’t really alone in it’s process.
I had the above problem recently when writing tests for a daemon. To write the test I spawned several daemons which were then tested. I tried removing and reloading classes, but I were using active records, which interferes with that, so I had to come up with something else. Originally I didn’t want to spawn a new interpreter through e.g. system because I didn’t want to spread the testing codes into several files (one which the new interpreter would be given as argument). Luckily the -e option exists so what I did was to basically spawn a daemon with Daemons.call and then write the script in the daemon’s block and then replace the daemon process with a new interpreter using exec. This is how it turned out.
def setup
# ...
@daemon1 = Daemons.call(:multiple => true, :backtrace => true) do
script =<<-"end_script"
# Set up the daemon along with any test-specific configuration.
end_script
exec "ruby -e '#{script}'"
end
# ...
end
I went through quite a bit of approaches that didn’t work before the above. So if you need to spawn a fresh daemon with its own unused interpreter state then please save yourself some trouble and just use the above.
Sorry, comments are closed for this article.