Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #8 from pdf/master

Allow passing environment to daemons via :env => Hash option (r2)
  • Loading branch information...
commit 4b01abc195e1ed4238113b3178fa26405620058f 2 parents 20135f5 + fdb4f70
Hongli Lai authored September 27, 2012
10  lib/daemon_controller.rb
@@ -177,6 +177,10 @@ class DaemonizationTimeout < TimeoutError
177 177
 	#  descriptors except stdin, stdout and stderr. However if there are any file
178 178
 	#  descriptors you want to keep open, specify the IO objects here. This must be
179 179
 	#  an array of IO objects.
  180
+	#
  181
+	# [:env]
  182
+	#  This must be a Hash.  The hash will contain the environment variables available
  183
+	#  to be made available to the daemon. Hash keys must be strings, not symbols.
180 184
 	def initialize(options)
181 185
 		[:identifier, :start_command, :ping_command, :pid_file, :log_file].each do |option|
182 186
 			if !options.has_key?(option)
@@ -198,6 +202,7 @@ def initialize(options)
198 202
 		@daemonize_for_me = options[:daemonize_for_me]
199 203
 		@keep_ios = options[:keep_ios] || []
200 204
 		@lock_file = determine_lock_file(options, @identifier, @pid_file)
  205
+		@env = options[:env] || {}
201 206
 	end
202 207
 	
203 208
 	# Start the daemon and wait until it can be pinged.
@@ -598,10 +603,10 @@ def run_command(command)
598 603
 						Config::CONFIG['bindir'],
599 604
 						Config::CONFIG['RUBY_INSTALL_NAME']
600 605
 					) + Config::CONFIG['EXEEXT']
601  
-					pid = Process.spawn(ruby_interpreter, SPAWNER_FILE,
  606
+					pid = Process.spawn(@env, ruby_interpreter, SPAWNER_FILE,
602 607
 						command, options)
603 608
 				else
604  
-					pid = Process.spawn(command, options)
  609
+					pid = Process.spawn(@env, command, options)
605 610
 				end
606 611
 			else
607 612
 				pid = safe_fork(@daemonize_for_me) do
@@ -613,6 +618,7 @@ def run_command(command)
613 618
 					STDIN.reopen("/dev/null", "r")
614 619
 					STDOUT.reopen(tempfile_path, "w")
615 620
 					STDERR.reopen(tempfile_path, "w")
  621
+					ENV.update(@env)
616 622
 					exec(command)
617 623
 				end
618 624
 			end
8  spec/daemon_controller_spec.rb
@@ -234,6 +234,14 @@
234 234
 			@controller.stop
235 235
 		end
236 236
 	end
  237
+
  238
+	it "receives environment variables" do
  239
+		new_controller(:env => {'ENV_FILE' => 'spec/env_file.tmp'})
  240
+		File.unlink('spec/env_file.tmp') if File.exist?('spec/env_file.tmp')
  241
+		@controller.start
  242
+		File.exist?('spec/env_file.tmp').should be_true
  243
+		@controller.stop
  244
+	end
237 245
 end
238 246
 
239 247
 describe DaemonController, "#stop" do
15  spec/echo_server.rb
@@ -61,6 +61,10 @@
61 61
 	end
62 62
 end
63 63
 
  64
+if ENV['ENV_FILE']
  65
+	options[:env_file] = File.expand_path(ENV['ENV_FILE'])
  66
+end
  67
+
64 68
 def main(options)
65 69
 	STDIN.reopen("/dev/null", 'r')
66 70
 	STDOUT.reopen(options[:log_file], 'a')
@@ -69,6 +73,15 @@ def main(options)
69 73
 	STDERR.sync = true
70 74
 	Dir.chdir(options[:chdir])
71 75
 	File.umask(0)
  76
+
  77
+	if options[:env_file]
  78
+		File.open(options[:env_file], 'w') do |f|
  79
+			f.write("\0")
  80
+		end
  81
+		at_exit do
  82
+			File.unlink(options[:env_file]) rescue nil
  83
+		end
  84
+	end
72 85
 	
73 86
 	if options[:pid_file]
74 87
 		sleep(options[:wait1])
@@ -122,4 +135,4 @@ def main(options)
122 135
 	end
123 136
 else
124 137
 	main(options)
125  
-end
  138
+end

0 notes on commit 4b01abc

Please sign in to comment.
Something went wrong with that request. Please try again.