Module: Shutdown

Defined in:
lib/liquid/ext/shutdown.rb

Class Method Summary collapse

Class Method Details

.register_handler(&block) ⇒ Object

Receives a block which will be registered for execution when the program exits. If multiple hanlders are registered, they are executed in reverse order of registration.

with_handler will not check the execution duration of the given block so only use this for non_blocking code, use register_handler_with_timout if you make potentially blocking calls



31
32
33
34
# File 'lib/liquid/ext/shutdown.rb', line 31

def self.register_handler(&block)
  return unless block_given?
  self.register_handler_with_timeout(nil, &block)
end

.register_handler_with_timeout(seconds, &block) ⇒ Object

Receives a time span in seconds and a block which will be registered for execution when the program exits. If multiple hanlders are registered, they are executed in reverse order of registration.

If a handler throws an exception or takes longer then the specified time span, execution skips to the next handler in line.

If the first parameter is nil, the handler will not check the codes runtime. Be cautios when executing asynchronous code when the ‘seconds’ parameter set to nil value as blocking codes would prevent the process from terminating.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/liquid/ext/shutdown.rb', line 12

def self.register_handler_with_timeout(seconds, &block)
  return unless block_given?
  at_exit do
    $log.info("shutdown", location: block.inspect) if $log
    begin
      Timeout::timeout(seconds, &block)
    rescue => e
      $log.exception e if $log
    end
  end
end