Created
January 24, 2012 17:38
-
-
Save sgonyea/1671400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module OurService | |
def run_thrift_server | |
handler = TcUser::Thrift::UserServiceHandler.new | |
processor = TcUser::Thrift::UserService::Processor.new(handler) | |
transport = ::Thrift::ServerSocket.new(port) | |
t_factory = ::Thrift::BufferedTransportFactory.new | |
@server = ::Thrift::MutexableThreadPoolServer.new(processor, transport, t_factory, nil, {:num => 20, :mutex => mutex}) | |
info "Service '#{name}' started with Process ID #{$$}" | |
zookeeper_connect | |
info "Writing PID to #{pid_file}..." | |
write_pid! | |
handle_signals! | |
info "Done." | |
info "Starting the #{TcUser::Thrift.name} server on port #{port}..." | |
info "Running Serve Loop..." | |
@server.serve { | |
info "Serve Callback Called. Enabling HAProxy..." | |
enable_haproxy! | |
} | |
info "Done?" | |
end | |
def handle_signals! | |
%w{INT TERM USR1}.each do |sig| | |
::Signal.trap(sig) { | |
info "received signal #{sig}. Stopping..." | |
disable_haproxy! # Tell HAProxy to stop sending traffic. | |
info "Locking Mutex..." # Lock the Mutex, so the threads eventually die. | |
mutex.lock | |
# Give them 60 seconds to seppuku. | |
Thread.new do | |
info "Sleeping for 60-seconds, before hard exit." | |
sleep(60) | |
info "!! Done sleeping..." | |
info "!! Begin hard exit... Deleting PID..." | |
delete_pid! | |
info "!! Hard Exiting." | |
exit! | |
end | |
info "Stopping threads." # Exit early if they end before those 60-seconds. This is how it *should* happen. | |
@server.stop_threads | |
info "Deleting PID..." | |
delete_pid! | |
info "Exiting..." | |
# Bye. | |
exit! | |
} | |
end | |
end | |
def enable_haproxy! | |
shell_command = %{echo "enable server tc-user/#{name}" | socat unix-connect:/tmp/proxystats stdio} | |
response = `#{shell_command}` | |
return(true) if response == "\n" | |
raise RuntimeError, "Failed to enable HAProxy. Received the message #{response}" | |
end | |
def disable_haproxy! | |
shell_command = %{echo "disable server tc-user/#{name}" | socat unix-connect:/tmp/proxystats stdio} | |
`#{shell_command}` == "\n" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment