在停止EventMachine之前返回Sinatra响应

时间:2012-12-03 14:42:15

标签: sinatra eventmachine

我在事件机器中使用Sinatra,我想关闭服务器并在收到DELETE请求时退出,并返回200 OK。但是,现在我无法达到这一点,并且总是在退出之前返回空响应。我怎么做到这一点?这是相关代码:

EM.run do
  class Server < Sinatra::Base
    delete '*' do
      EM.defer proc{ halt 200 }, proc{ EM.stop }
     end
  end

  Server.run!
end

我会得到一个空的回复,然后得到以下的堆栈跟踪:

/Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/sinatra-1.3.3/lib/sinatra/base.rb:803:in `throw': uncaught throw `halt' in thread 0x7fa4225f2020 (ThreadError)
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/sinatra-1.3.3/lib/sinatra/base.rb:803:in `halt'
    from instant-markdown-d.rb:39:in `DELETE *'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1037:in `call'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1037:in `spawn_threadpool'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `initialize'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `new'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1033:in `spawn_threadpool'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:1023:in `defer'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/thin-1.5.0/lib/thin/connection.rb:51:in `process'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/thin-1.5.0/lib/thin/connection.rb:39:in `receive_data'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
    from /Users/syeo/.rvm/gems/ruby-1.8.7-p352@instant-markdown-d/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
    from instant-markdown-d.rb:10

我也尝试过很多类似的方法,但无法找到发送200的方法,然后关闭服务器。

1 个答案:

答案 0 :(得分:0)

结束这样做:

EM.run do
  class Server < Sinatra::Base
    delete '*' do
      EM.add_timer(0.2) do
        EM.stop
        exit
      end
      status 200
    end
  end

  Server.run!
end

哪个是黑客,但至少有效。