Created
October 20, 2009 04:21
-
-
Save cmiles74/213990 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
# | |
# A simple pre-forking server that tests to make sure the MySQL server | |
# is available. | |
# | |
# Based so heavily on the following article by Ryan Tomakyo that it's | |
# practically a verbatim copy. | |
# | |
# http://tomayko.com/writings/unicorn-is-unix | |
# | |
# Pasted at http://gist.github.com/213990 | |
# | |
require 'socket' | |
require 'mysql' | |
# configuration | |
BIND_ADDRESS = '127.0.0.1' | |
BIND_PORT = '9999' | |
NUM_PROCESSES = 5 | |
MYSQL_HOST = 'beatrix.escapecs.com' | |
MYSQL_USER = 'someone' | |
MYSQL_PASSWORD = 'pa$$w0rd' | |
MYSQL_DATABASE = 'testdb' | |
# create a socket and bind to port | |
acceptor = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) | |
address = Socket.pack_sockaddr_in(BIND_PORT, BIND_ADDRESS) | |
acceptor.bind(address) | |
# start listening on the socket | |
acceptor.listen(10) | |
# trap for a process exit and stop listening | |
trap('EXIT') { | |
acceptor.close | |
} | |
# fork the child processes | |
NUM_PROCESSES.times do | |
fork do | |
# trap for process break and exit | |
trap('INT') { exit } | |
puts "child #$$ accepting on shared socket (localhost:9999)" | |
loop { | |
# block until a new connection is ready to be de-queued | |
socket, addr = acceptor.accept | |
# get the incoming message | |
incoming_message = socket.gets | |
# flag to indicate successful connection | |
mysql_server_connect = false | |
begin | |
# connect to the MySQL server begin connect = connection = | |
connection = Mysql.real_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, | |
MYSQL_DATABASE) | |
server_version = connection.get_server_info | |
message = "MySQL Server (version #{server_version}) is A-Okay!\n\n" | |
mysql_server_connect = true | |
rescue | |
message = "Uh-oh! The MySQL Server did not respond. :(\n\n" | |
ensure | |
connection.close if connection | |
end | |
# send our status | |
if mysql_server_connect | |
socket.write "HTTP/1.1 200 OK\n" | |
else | |
socket.write "HTTP/1.1 503 Service Unavailable\n" | |
end | |
# send the header | |
socket.write "Date: #{Time.now}\r\n" | |
socket.write "Server: Simple Ruby Server\r\n" | |
socket.write "Expires: #{Time.now}\r\n" | |
socket.write "Content-Type: text/html; charset=UTF-8\r\n" | |
socket.write "\r\n" | |
# send out message | |
socket.write message | |
# close the socket | |
socket.flush | |
socket.close | |
puts "child #$$ invoked with: '#{incoming_message.strip}'" | |
} | |
end | |
end | |
# trap interrupt and exit | |
trap('INT') { | |
puts "Exiting..." | |
exit | |
} | |
# wait for all child processes to exit | |
Process.waitall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment