class Concurrent::AtExitImplementation

Provides ability to add and remove handlers to be run at `Kernel#at_exit`, order is undefined. Each handler is executed at most once.

@!visibility private

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/concurrent/utility/at_exit.rb, line 13
def initialize(*args)
  super()
  synchronize { ns_initialize(*args) }
end

Public Instance Methods

add(handler_id = nil, &handler) click to toggle source

Add a handler to be run at `Kernel#at_exit` @param [Object] handler_id optionally provide an id, if allready present, handler is replaced @yield the handler @return id of the handler

# File lib/concurrent/utility/at_exit.rb, line 22
def add(handler_id = nil, &handler)
  id = handler_id || handler.object_id
  synchronize { @handlers[id] = handler }
  id
end
delete(handler_id) click to toggle source

Delete a handler by handler_id @return [true, false]

# File lib/concurrent/utility/at_exit.rb, line 30
def delete(handler_id)
  !!synchronize { @handlers.delete handler_id }
end
enabled=(value) click to toggle source

Configure if it runs during `Kernel#at_exit`

# File lib/concurrent/utility/at_exit.rb, line 62
def enabled=(value)
  synchronize { @enabled = value }
end
enabled?() click to toggle source

Will it run during `Kernel#at_exit`

# File lib/concurrent/utility/at_exit.rb, line 57
def enabled?
  synchronize { @enabled }
end
handler?(handler_id) click to toggle source

Is handler with handler_id rpesent? @return [true, false]

# File lib/concurrent/utility/at_exit.rb, line 36
def handler?(handler_id)
  synchronize { @handlers.key? handler_id }
end
handlers() click to toggle source

@return copy of the handlers

# File lib/concurrent/utility/at_exit.rb, line 41
def handlers
  synchronize { @handlers }.clone
end
install() click to toggle source

install `Kernel#at_exit` callback to execute added handlers

# File lib/concurrent/utility/at_exit.rb, line 46
def install
  synchronize do
    @installed ||= begin
                     at_exit { runner }
                     true
                   end
    self
  end
end
run() click to toggle source

run the handlers manually @return ids of the handlers

# File lib/concurrent/utility/at_exit.rb, line 68
def run
  handlers, _ = synchronize { handlers, @handlers = @handlers, {} }
  handlers.each do |_, handler|
    begin
      handler.call
    rescue => error
      Concurrent.global_logger.call(ERROR, error)
    end
  end
  handlers.keys
end

Private Instance Methods

ns_initialize(enabled = true) click to toggle source
# File lib/concurrent/utility/at_exit.rb, line 82
def ns_initialize(enabled = true)
  @handlers = {}
  @enabled  = enabled
end
runner() click to toggle source
# File lib/concurrent/utility/at_exit.rb, line 87
def runner
  run if synchronize { @enabled }
end