开发者

how to start and stop Ruby Thin server?

开发者 https://www.devze.com 2023-04-11 21:08 出处:网络
I need to start and stop Thin server programmatically more than one time. I am using the following code:

I need to start and stop Thin server programmatically more than one time. I am using the following code:

require "thin"

def running?
  !TCPSocket.new('127.0.0.1', 3000).close
rescue Exception
  # not running
end

lo开发者_JS百科op do
    server = Thin::Server.new('0.0.0.0', 3000, lambda {|env| [200, {}, ""]})
    thread = Thread.new {server.start}
    t = Time.now
    until running?
      sleep 0.1
    end
    puts "Started in #{Time.now - t}"

    server.stop!

    t = Time.now
    while running?
      sleep 0.1
    end
    puts "Stopped in #{Time.now - t}"
end

I hoped that Thin::Server#running? would tell me when server is not running anymore, but i was wrong and had to create my own #running? method.

Also, it will stop in about 11-12 seconds (!?!?!) the first time and won't print "Started" for the second time - e.g. it won't start properly second time, but Thin prints the familiar lines as if everything would be ok. This is the output i'm getting from this script:

Thin web server (v1.2.11 codename Bat-Shit Crazy)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
Started in 0.002001
Stopping ...
Stopped in 11.441654
Thin web server (v1.2.11 codename Bat-Shit Crazy)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop

And it's just blocking indefinitely. How can i stop and start the server properly?


Haa! It seems that i've managed to solve this problem by using EventMachine 1.0.0.beta 4.1 :)

It's fast and i can use Thin::Server#running? instead of my #running? method.

0

精彩评论

暂无评论...
验证码 换一张
取 消