简体   繁体   English

如何启动和停止Ruby Thin服务器?

[英]how to start and stop Ruby Thin server?

I need to start and stop Thin server programmatically more than one time. 我需要以编程方式启动和停止Thin服务器多次。 I am using the following code: 我正在使用以下代码:

require "thin"

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

loop 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? 我希望Thin :: Server#running? would tell me when server is not running anymore, but i was wrong and had to create my own #running? 会告诉我服务器何时不再运行,但是我错了,必须创建自己的#running? method. 方法。

Also, it will stop in about 11-12 seconds (!?!?!) the first time and won't print "Started" for the second time - eg it won't start properly second time, but Thin prints the familiar lines as if everything would be ok. 此外,它将在大约11-12秒(!?!?!)中第一次停止,并且第二次将不会打印“已启动”-例如,它将无法第二次正确启动,但是Thin将打印熟悉的行好像一切都会好的。 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 :) 看来我已经通过使用EventMachine 1.0.0.beta 4.1解决了这个问题:)

It's fast and i can use Thin::Server#running? 快速,我可以使用Thin :: Server#running? instead of my #running? 而不是我的#running? method. 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM