tags:

views:

349

answers:

2

I'd like to be able to write a ruby program that can restart without dropping it's socket connections.

A: 

You're talking about network sockets, not UNIX sockets I assume?

I'm not sure this suits your needs, but the way I would do it is by seperating the networking and logic part, and only restart the logic part, then reconnect the logic part to the networking part.

wvdschel
+1  A: 

This program gets Google's homepage and then when you pass it SIG_INT via ctrl-c it restarts the program and reads the output of the homepage from the open socket with Google.

#!/usr/bin/ruby
#simple_connector.rb
require 'socket'

puts "Started."

if ARGV[0] == "restart"
  sock = IO.open(ARGV[1].to_i)
  puts sock.read
  exit
else
  sock = TCPSocket.new('google.com', 80)
  sock.write("GET /\n")
end

Signal.trap("INT") do
  puts "Restarting..."
  exec("ruby simple_connector.rb restart #{sock.fileno}")
end

while true
  sleep 1
end
RyanTM