lib/service_manager/service.rb in service_manager-0.6.2 vs lib/service_manager/service.rb in service_manager-0.6.3
- old
+ new
@@ -2,11 +2,11 @@
require "timeout"
class ServiceManager::Service
CHDIR_SEMAPHORE = Mutex.new
ANSI_COLOR_RESET = 0
- attr_accessor :name, :host, :port, :cwd, :reload_uri, :start_cmd, :process, :loaded_cue, :timeout, :color, :pid_file
+ attr_accessor :name, :host, :port, :cwd, :reload_uri, :start_cmd, :process, :loaded_cue, :timeout, :color, :pid_file, :manage_pid_file
class ServiceDidntStart < Exception; end
def initialize(options = {})
options.each { |k,v| send("#{k}=", v) }
@@ -51,22 +51,23 @@
puts "Server for #{colorized_service_name} detected as running."
reload || puts("Reloading not supported. Any changes made to code for #{colorized_service_name} will not take effect!")
return false
end
- puts "Starting #{colorized_service_name} in #{cwd} with '#{start_cmd}'"
CHDIR_SEMAPHORE.synchronize do
+ puts "Starting #{colorized_service_name} in #{cwd} with '#{start_cmd}'\n"
Dir.chdir(cwd) do
without_bundler_env do
# system("bash -c set")
self.process = PTYBackgroundProcess.run(start_cmd)
end
end
end
at_exit { stop }
wait
puts "Server #{colorized_service_name} is up."
+ File.open(pid_file, "wb") { |f| f << process.pid } if manage_pid_file
end
# stop the service. If we didn't start it, do nothing.
def stop
return unless process
@@ -77,10 +78,11 @@
process.kill("KILL") # ok... no more Mr. Nice Guy.
process.wait
end
puts "Server #{colorized_service_name} (#{process.pid}) is shut down"
self.process = nil
+ FileUtils.rm(pid_file) if manage_pid_file && File.exist?(pid_file)
true
end
# reload the service by hitting the configured reload_url. In this case, the service needs to be a web service, and needs to have an action that you can hit, in test mode, that will cause the process to gracefully reload itself.
def reload
@@ -91,14 +93,16 @@
true
end
# detects if the service is running on the configured host and port (will return true if we weren't the ones who started it)
def running?
- if pid_file
+ case
+ when pid_file
running_via_pid_file?
- else
- TCPSocket.listening_service?(:port => port, :host => host)
+ when port
+ TCPSocket.listening_service?(:port => port, :host => host || "127.0.0.1")
+ else raise "Service Manager needs to be able to tell if the service is already running or not. You'll need to specify a pid file or a TCP port to check."
end
end
protected
@@ -107,10 +111,11 @@
rescue Errno::ESRCH, Errno::ENOENT
return false
end
def colorize(output)
- "\e[0;#{color}m#{output}\e[0;#{ANSI_COLOR_RESET}m"
+ no_color = output.gsub(/\e\[(\d*\;|)\d*m\d*/, "")
+ "\e[0;#{color}m#{no_color}\e[0;#{ANSI_COLOR_RESET}m"
end
def colorized_service_name
if process
colorize("#{name} (#{process.pid})")