Skip to content

Commit

Permalink
Merge pull request #8 from pdf/master
Browse files Browse the repository at this point in the history
Allow passing environment to daemons via :env => Hash option (r2)
  • Loading branch information
FooBarWidget committed Sep 27, 2012
2 parents 20135f5 + fdb4f70 commit 4b01abc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/daemon_controller.rb
Expand Up @@ -177,6 +177,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)
Expand All @@ -198,6 +202,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.
Expand Down Expand Up @@ -598,10 +603,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
Expand All @@ -613,6 +618,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
Expand Down
8 changes: 8 additions & 0 deletions spec/daemon_controller_spec.rb
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion spec/echo_server.rb
Expand Up @@ -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')
Expand All @@ -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])
Expand Down Expand Up @@ -122,4 +135,4 @@ def main(options)
end
else
main(options)
end
end

0 comments on commit 4b01abc

Please sign in to comment.