0

我在事件机器中使用 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 的方法,然后关闭服务器。

4

1 回答 1

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

这是一个黑客,但至少有效。

于 2012-12-08T16:48:45.587 回答