0

I'm using Sinatra within event-machine, and I want to shut down the server and exit upon receiving a DELETE request, and return a 200 OK. However, right now I can't get to that point, and always end u returning an empty response before exiting. How would I achieve this? Here's the relevant code:

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

  Server.run!
end

What happens is that I get an empty reply, and get the following stacktrace:

/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

I've also tried many similar ways but couldn't find a way to send a 200, then shut down the server.

Suan
  • 34,563
  • 13
  • 47
  • 61
  • Does this work? `EM.defer proc{ status 200; EM.stop }`. You don't necessarily need a callback. I still am wondering why you'd use `EM.defer` for that snippet. – Kashyap Dec 05 '12 at 10:45
  • 1
    Thanks @Kashyap, but no dice. What happens when I do that is that I don't get a stacktrace, but the client gets an empty response, and the server doesn't shut down. The reason I was using `EM.defer` is because I thought that `EM.stop` was getting executed before sinatra had a chance to complete the response. – Suan Dec 08 '12 at 16:00

1 Answers1

0

Ended up doing this:

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

Which is a hack, but at least works.

Suan
  • 34,563
  • 13
  • 47
  • 61