diff --git a/bin/god b/bin/god index 149df077..c07481a2 100755 --- a/bin/god +++ b/bin/god @@ -60,6 +60,10 @@ begin options[:pid] = x end + opts.on("-SDIR", "--socket_path DIR", "Where to write the socket file") do |x| + options[:socket_dir] = x + end + opts.on("-lFILE", "--log FILE", "Where to write the log file") do |x| options[:log] = x end diff --git a/lib/god.rb b/lib/god.rb index 4b971b3a..a3c43ed8 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -204,7 +204,8 @@ class << self :terminate_timeout, :socket_user, :socket_group, - :socket_perms + :socket_perms, + :socket_dir # internal attr_accessor :inited, @@ -443,7 +444,7 @@ def self.uncontact(contact) end def self.watches_by_name(name) - case name + case name when "", nil then self.watches.values.dup else Array(self.watches[name] || self.groups[name]).dup end diff --git a/lib/god/cli/run.rb b/lib/god/cli/run.rb index 628cc26a..d5b4d36c 100644 --- a/lib/god/cli/run.rb +++ b/lib/god/cli/run.rb @@ -50,6 +50,10 @@ def default_run God.port = @options[:port] end + if @options[:socket_dir] + God.socket_dir = @options[:socket_dir] + end + if @options[:events] God::EventHandler.load end diff --git a/lib/god/socket.rb b/lib/god/socket.rb index 1eeeb42d..db68d309 100644 --- a/lib/god/socket.rb +++ b/lib/god/socket.rb @@ -11,7 +11,8 @@ class Socket # # Returns String (file location) def self.socket_file(port) - "/tmp/god.#{port}.sock" + dir = File.absolute_path(God.socket_dir || "/tmp") + "#{dir}/god.#{port}.sock" end # The address of the socket for a given port @@ -93,7 +94,7 @@ def start end end - if File.exists?(self.socket_file) + if File.exist?(self.socket_file) if @user user_method = @user.is_a?(Integer) ? :getpwuid : :getpwnam uid = Etc.send(user_method, @user).uid diff --git a/test/test_socket.rb b/test/test_socket.rb index 91fcab1a..236fc2ab 100644 --- a/test/test_socket.rb +++ b/test/test_socket.rb @@ -13,7 +13,7 @@ def test_should_start_a_drb_server end def test_should_use_supplied_port_and_host - DRb.expects(:start_service).with { |uri, object| uri == "drbunix:///tmp/god.9999.sock" && object.is_a?(God::Socket) } + DRb.expects(:start_service).with { |uri, object| uri == "drbunix://#{God::Socket.socket_file(9999)}" && object.is_a?(God::Socket) } server = God::Socket.new(9999) end @@ -31,4 +31,10 @@ def test_ping_should_return_true server = God::Socket.new assert server.ping end + + def test_should_use_socket_dir_param + God.socket_dir = File.absolute_path(".") + socket_file = God::Socket.socket_file(9999) + assert_equal "#{File.absolute_path(".")}/god.9999.sock", socket_file + end end