From fdb4f7021dd743547c26111b5e762ef85ec18e66 Mon Sep 17 00:00:00 2001 From: Peter Fern Date: Wed, 6 Jun 2012 19:16:39 +1000 Subject: [PATCH] Allow passing environment to daemons via :env => Hash option --- lib/daemon_controller.rb | 10 ++++++++-- spec/daemon_controller_spec.rb | 8 ++++++++ spec/echo_server.rb | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/daemon_controller.rb b/lib/daemon_controller.rb index f6abdb9..2ce0533 100644 --- a/lib/daemon_controller.rb +++ b/lib/daemon_controller.rb @@ -170,6 +170,10 @@ class DaemonizationTimeout < TimeoutError # descriptors except stdin, stdout and stderr. However if there are any file # descriptors you want to keep open, specify the IO objects here. This must be # an array of IO objects. + # + # [:env] + # This must be a Hash. The hash will contain the environment variables available + # to be made available to the daemon. Hash keys must be strings, not symbols. def initialize(options) [:identifier, :start_command, :ping_command, :pid_file, :log_file].each do |option| if !options.has_key?(option) @@ -190,6 +194,7 @@ def initialize(options) @daemonize_for_me = options[:daemonize_for_me] @keep_ios = options[:keep_ios] || [] @lock_file = determine_lock_file(options, @identifier, @pid_file) + @env = options[:env] || {} end # Start the daemon and wait until it can be pinged. @@ -579,10 +584,10 @@ def run_command(command) Config::CONFIG['bindir'], Config::CONFIG['RUBY_INSTALL_NAME'] ) + Config::CONFIG['EXEEXT'] - pid = Process.spawn(ruby_interpreter, SPAWNER_FILE, + pid = Process.spawn(@env, ruby_interpreter, SPAWNER_FILE, command, options) else - pid = Process.spawn(command, options) + pid = Process.spawn(@env, command, options) end else pid = safe_fork(@daemonize_for_me) do @@ -594,6 +599,7 @@ def run_command(command) STDIN.reopen("/dev/null", "r") STDOUT.reopen(tempfile_path, "w") STDERR.reopen(tempfile_path, "w") + ENV.update(@env) exec(command) end end diff --git a/spec/daemon_controller_spec.rb b/spec/daemon_controller_spec.rb index 31be7bb..1824a76 100644 --- a/spec/daemon_controller_spec.rb +++ b/spec/daemon_controller_spec.rb @@ -234,6 +234,14 @@ @controller.stop end end + + it "receives environment variables" do + new_controller(:env => {'ENV_FILE' => 'spec/env_file.tmp'}) + File.unlink('spec/env_file.tmp') if File.exist?('spec/env_file.tmp') + @controller.start + File.exist?('spec/env_file.tmp').should be_true + @controller.stop + end end describe DaemonController, "#stop" do diff --git a/spec/echo_server.rb b/spec/echo_server.rb index d7338a6..0b68604 100755 --- a/spec/echo_server.rb +++ b/spec/echo_server.rb @@ -61,6 +61,10 @@ end end +if ENV['ENV_FILE'] + options[:env_file] = File.expand_path(ENV['ENV_FILE']) +end + def main(options) STDIN.reopen("/dev/null", 'r') STDOUT.reopen(options[:log_file], 'a') @@ -69,6 +73,15 @@ def main(options) STDERR.sync = true Dir.chdir(options[:chdir]) File.umask(0) + + if options[:env_file] + File.open(options[:env_file], 'w') do |f| + f.write("\0") + end + at_exit do + File.unlink(options[:env_file]) rescue nil + end + end if options[:pid_file] sleep(options[:wait1]) @@ -122,4 +135,4 @@ def main(options) end else main(options) -end \ No newline at end of file +end