Module: Dionysus

Defined in:
lib/dionysus.rb,
lib/dionysus/checks.rb,
lib/dionysus/version.rb,
sig/dionysus/rb.rbs

Defined Under Namespace

Modules: Rb, Utils, Version Classes: Checks, Consumer, Monitor, Producer, Railtie, TopicName

Constant Summary collapse

CONSUMER_GROUP_PREFIX =
"dionysus_consumer_group_for"
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.consumer_registryObject



104
105
106
# File 'lib/dionysus.rb', line 104

def self.consumer_registry
  @consumer_registry
end

.enable_outbox_worker_healthcheckObject



90
91
92
93
94
# File 'lib/dionysus.rb', line 90

def self.enable_outbox_worker_healthcheck
  monitor.subscribe("outbox_producer.started") { outbox_worker_health_check.register_heartbeat }
  monitor.subscribe("outbox_producer.stopped") { outbox_worker_health_check.worker_stopped }
  monitor.subscribe("outbox_producer.heartbeat") { outbox_worker_health_check.register_heartbeat }
end

.health_checkObject



82
83
84
# File 'lib/dionysus.rb', line 82

def self.health_check
  @health_check
end

.health_check=(health_check) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dionysus.rb', line 54

def self.health_check=(health_check)
  @health_check = health_check

  Karafka.monitor.subscribe("app.initialized") do |_event|
    health_check = Dionysus.health_check

    health_check&.app_initialized!
  end

  Karafka.monitor.subscribe("statistics.emitted") do |_event|
    health_check = Dionysus.health_check

    health_check&.register_heartbeat
  end

  Karafka.monitor.subscribe("consumer.consumed") do |_event|
    health_check = Dionysus.health_check

    health_check&.register_heartbeat
  end

  Karafka.monitor.subscribe("app.stopped") do |_event|
    health_check = Dionysus.health_check

    health_check&.app_stopped!
  end
end

.initialize_application!(environment:, seed_brokers:, client_id:, logger:, draw_routing: true, consumer_group_prefix: CONSUMER_GROUP_PREFIX, consumer_group_name: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dionysus.rb', line 23

def self.initialize_application!(environment:, seed_brokers:, client_id:, logger:, draw_routing: true, consumer_group_prefix: CONSUMER_GROUP_PREFIX, consumer_group_name: nil)
  ENV["KARAFKA_ENV"] = environment

  karafka_app = Class.new(Karafka::App) do
    setup do |config|
      config.kafka = {
        "bootstrap.servers": seed_brokers.join(","),
        "client.id": client_id
      }
      config.client_id = client_id
      config.logger = logger
      yield config if block_given?
    end
  end

  Object.const_set(:KarafkaApp, karafka_app)
  self.karafka_application = karafka_app
  return unless consumer_registry.present? && draw_routing

  consumer_group_name ||= "#{consumer_group_prefix}_#{karafka_application.config.client_id}"
  evaluate_routing(consumer_group_name: consumer_group_name)
end

.inject_routing!(registry) ⇒ Object



108
109
110
# File 'lib/dionysus.rb', line 108

def self.inject_routing!(registry)
  @consumer_registry = registry
end

.karafka_applicationObject



50
51
52
# File 'lib/dionysus.rb', line 50

def self.karafka_application
  @karafka_application
end

.karafka_application=(karafka_app) ⇒ Object



46
47
48
# File 'lib/dionysus.rb', line 46

def self.karafka_application=(karafka_app)
  @karafka_application = karafka_app
end

.loaderObject



17
18
19
20
21
# File 'lib/dionysus.rb', line 17

def self.loader
  @loader ||= Zeitwerk::Loader.for_gem.tap do |loader|
    loader.ignore("#{__dir__}/dionysus-rb.rb")
  end
end

.loggerObject



96
97
98
99
100
101
102
# File 'lib/dionysus.rb', line 96

def self.logger
  if karafka_application
    karafka_application.config.logger
  else
    Logger.new($stdout)
  end
end

.monitorObject



112
113
114
# File 'lib/dionysus.rb', line 112

def self.monitor
  @monitor ||= Dionysus::Monitor.new
end

.outbox_worker_health_checkObject



86
87
88
# File 'lib/dionysus.rb', line 86

def self.outbox_worker_health_check
  @outbox_worker_health_check ||= Dionysus::Producer::Outbox::HealthCheck.new
end