What is the correct way to end a WEBrick server process after a timeout?

172 Views Asked by At

I'm trying to implement a small WEBrick server that ends itself when there are no requests after x number of seconds. However, I'm getting nowhere. My very first attempt at simply exiting after 2 seconds fails. Here's the simple code that doesn't work.

server = WEBrick::HTTPServer.new(:Port => 8000)
WEBrick::Utils::TimeoutHandler.register(2, Timeout::Error)
server.start

I thought that would simply exit the process after 2 seconds. Here's what actually happens:

[2020-01-19 15:41:10] INFO  WEBrick 1.4.2
[2020-01-19 15:41:10] INFO  ruby 2.5.1 (2018-03-29) [x86_64-linux-gnu]
[2020-01-19 15:41:10] INFO  WEBrick::HTTPServer#start: pid=16622 port=8000
[2020-01-19 15:41:12] ERROR Timeout::Error: execution timeout
    /usr/lib/ruby/2.5.0/webrick/server.rb:170:in `select'

And then the process keeps running. I have to ctrl-c to end it.

What's the correct way to shut down a server and end the process after a timeout?

0

There are 0 best solutions below