Gracefully exiting a thread in Ruby

I am trying to execute Mongrel and using the following code:

require 'rubygems'
require 'mongrel'

class SimpleHandler < Mongrel::HttpHandler
    def process(request, response)
        response.start(200) do |head, out|
            head["Content-Type"] = "text/plain"
            out.write("Hello World!\n")
        end
    end
end

h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/test", SimpleHandler.new)
puts "Press Control-C to exit"
h.run.join

trap("INT") do
    puts "Exiting..."
end

Basically, it just prints "Hello World!" when i go to localhost: 3000 / test. It works great, and I can close the program using Control-C. But when I press Control-C, this is output:

my_web_server.rb:17:in `join': Interrupt
from my_web_server.rb:17

So, I tried to put this statement trap("INT")at the end, but it is not called. Decision?

Thank.

+3
source share
1 answer

INT, , , - . -c "". , control-C , :

begin
  ... # do stuff
rescue Interrupt
  puts "Exiting..."
end
+7

Source: https://habr.com/ru/post/1737870/


All Articles