class Karafka::Process

Class used to catch signals from ruby Signal class in order to manage Karafka stop @note There might be only one process - this class is a singleton

Constants

HANDLED_SIGNALS

Signal types that we handle

Public Class Methods

new() click to toggle source

Creates an instance of process and creates empty hash for callbacks

# File lib/karafka/process.rb, line 29
def initialize
  @callbacks = Hash.new { |hsh, key| hsh[key] = [] }
end

Public Instance Methods

supervise() click to toggle source

Method catches all HANDLED_SIGNALS and performs appropriate callbacks (if defined) @note If there are no callbacks, this method will just ignore a given signal that was sent

# File lib/karafka/process.rb, line 35
def supervise
  HANDLED_SIGNALS.each { |signal| trap_signal(signal) }
end

Private Instance Methods

notice_signal(signal) click to toggle source

Informs monitoring about trapped signal @param [Symbol] signal type that we received @note We cannot perform logging from trap context, that's why

we have to spin up a new thread to do this
# File lib/karafka/process.rb, line 54
def notice_signal(signal)
  Thread.new do
    Karafka.monitor.instrument('process.notice_signal', caller: self, signal: signal)
  end
end
trap_signal(signal) click to toggle source

Traps a single signal and performs callbacks (if any) or just ignores this signal @param [Symbol] signal type that we want to catch

# File lib/karafka/process.rb, line 43
def trap_signal(signal)
  trap(signal) do
    notice_signal(signal)
    (@callbacks[signal] || []).each(&:call)
  end
end