Sha256: 826d7cc134891ac68b01d1b5cf792999404ba58942a3c9900e35d65c894869f4

Contents?: true

Size: 1.08 KB

Versions: 33

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Puma

  # This is an event sink used by `Puma::Server` to handle
  # lifecycle events such as :on_booted, :on_restart, and :on_stopped.
  # Using `Puma::DSL` it is possible to register callback hooks
  # for each event type.
  class Events

    def initialize
      @hooks = Hash.new { |h,k| h[k] = [] }
    end

    # Fire callbacks for the named hook
    def fire(hook, *args)
      @hooks[hook].each { |t| t.call(*args) }
    end

    # Register a callback for a given hook
    def register(hook, obj=nil, &blk)
      if obj and blk
        raise "Specify either an object or a block, not both"
      end

      h = obj || blk

      @hooks[hook] << h

      h
    end

    def on_booted(&block)
      register(:on_booted, &block)
    end

    def on_restart(&block)
      register(:on_restart, &block)
    end

    def on_stopped(&block)
      register(:on_stopped, &block)
    end

    def fire_on_booted!
      fire(:on_booted)
    end

    def fire_on_restart!
      fire(:on_restart)
    end

    def fire_on_stopped!
      fire(:on_stopped)
    end
  end
end

Version data entries

33 entries across 33 versions & 3 rubygems

Version Path
puma-6.2.1 lib/puma/events.rb
puma-6.2.0-java lib/puma/events.rb
puma-6.2.0 lib/puma/events.rb
puma-6.1.1-java lib/puma/events.rb
puma-6.1.1 lib/puma/events.rb
puma-6.1.0-java lib/puma/events.rb
puma-6.1.0 lib/puma/events.rb
puma-6.0.2-java lib/puma/events.rb
puma-6.0.2 lib/puma/events.rb
puma-6.0.1-java lib/puma/events.rb
puma-6.0.1 lib/puma/events.rb
puma-6.0.0-java lib/puma/events.rb
puma-6.0.0 lib/puma/events.rb