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 =

Supported lifecycle callback names.

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

Frozen empty callback list used for events without registrations.

[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



26
27
28
# File 'lib/mammoth/lifecycle_hooks.rb', line 26

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

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



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

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:



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mammoth/lifecycle_hooks.rb', line 33

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