Class: Karafka::Process

Inherits:
Object
  • Object
show all
Extended by:
Core::Taggable
Defined in:
lib/karafka/process.rb

Overview

Note:

There might be only one process - this class is a singleton

Class used to catch signals from ruby Signal class in order to manage Karafka stop

Constant Summary collapse

HANDLED_SIGNALS =

Signal types that we handle

%i[
  SIGINT
  SIGQUIT
  SIGTERM
  SIGTTIN
  SIGTSTP
  SIGCHLD
  SIGUSER1
].freeze

Instance Method Summary collapse

Constructor Details

#initializeProcess

Creates an instance of process and creates empty hash for callbacks



50
51
52
53
# File 'lib/karafka/process.rb', line 50

def initialize
  @callbacks = Hash.new { |hsh, key| hsh[key] = [] }
  @supervised = false
end

Instance Method Details

#clearObject

Clears all the defined callbacks. Useful for post-fork cleanup when parent already defined some signals



57
58
59
# File 'lib/karafka/process.rb', line 57

def clear
  @callbacks.clear
end

#on_any_active(&block) ⇒ Object

Note:

This will only bind to signals that already have at least one callback defined

Assigns a callback that will run on any supported signal that has at least one callback registered already.

Parameters:

  • block (Proc)

    code we want to run



41
42
43
44
45
46
47
# File 'lib/karafka/process.rb', line 41

def on_any_active(&block)
  HANDLED_SIGNALS.each do |signal|
    next unless @callbacks.key?(signal)

    public_send(:"on_#{signal.to_s.downcase}", &block)
  end
end

#superviseObject

Note:

If there are no callbacks, this method will just ignore a given signal that was sent

Method catches all HANDLED_SIGNALS and performs appropriate callbacks (if defined)



63
64
65
66
67
68
69
70
71
72
# File 'lib/karafka/process.rb', line 63

def supervise
  HANDLED_SIGNALS.each do |signal|
    # Supervise only signals for which we have defined callbacks
    next unless @callbacks.key?(signal)

    trap_signal(signal)
  end

  @supervised = true
end

#supervised?Boolean

Is the current process supervised and are trap signals installed

Returns:

  • (Boolean)


75
76
77
# File 'lib/karafka/process.rb', line 75

def supervised?
  @supervised
end