Class: Mammoth::LifecycleHooks

Inherits:
Object
  • Object
show all
Defined in:
lib/mammoth/lifecycle_hooks.rb

Overview

Executes optional lifecycle callbacks around local Mammoth operations.

Hooks are intentionally in-process and explicit. They give extensions and future control agents stable observation points without changing the data plane or introducing remote behavior into OSS.

Constant Summary collapse

EVENTS =
%i[
  before_start
  after_start
  before_shutdown
  after_shutdown
  before_replay
  after_replay
].freeze
EMPTY_CALLBACKS =
[nil].compact.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callbacks = {}) ⇒ LifecycleHooks

Returns a new instance of LifecycleHooks.

Parameters:

  • callbacks (Hash) (defaults to: {})

    lifecycle callbacks keyed by event name



23
24
25
# File 'lib/mammoth/lifecycle_hooks.rb', line 23

def initialize(callbacks = {})
  @callbacks = normalize(callbacks || {})
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



20
21
22
# File 'lib/mammoth/lifecycle_hooks.rb', line 20

def callbacks
  @callbacks
end

Instance Method Details

#call(event, context = {}) ⇒ void

This method returns an undefined value.

Parameters:

  • event (Symbol, String)

    lifecycle event

  • context (Hash) (defaults to: {})

    event context

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mammoth/lifecycle_hooks.rb', line 30

def call(event, context = {})
  event = event.to_sym
  raise ConfigurationError, "unknown lifecycle hook: #{event}" unless EVENTS.include?(event)

  callback_list = callbacks.fetch(event, EMPTY_CALLBACKS)
  # @type var callback_list: untyped
  Array(callback_list).each do |callback|
    # @type var callback: untyped
    callback.call(context.merge(event: event))
  end
  nil
end