Module: JetstreamBridge::Rails::Integration

Defined in:
lib/jetstream_bridge/rails/integration.rb

Overview

Rails-specific lifecycle helpers for JetStream Bridge.

Keeps the Railtie thin and makes lifecycle decisions easy to test and reason about.

Class Method Summary collapse

Class Method Details

.active_loggerObject



139
140
141
142
143
144
145
# File 'lib/jetstream_bridge/rails/integration.rb', line 139

def active_logger
  if defined?(::Rails) && ::Rails.respond_to?(:logger)
    ::Rails.logger
  else
    Logging.logger
  end
end

.attach_active_record_hooks!Object

Attach ActiveRecord hooks for serializer setup on reload.



23
24
25
26
27
# File 'lib/jetstream_bridge/rails/integration.rb', line 23

def attach_active_record_hooks!
  ActiveSupport.on_load(:active_record) do
    ActiveSupport::Reloader.to_prepare { JetstreamBridge::ModelCodecSetup.apply! }
  end
end

.auto_enable_test_mode!Object

Auto-enable test mode in test environment when NATS is not configured.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jetstream_bridge/rails/integration.rb', line 49

def auto_enable_test_mode!
  return unless auto_enable_test_mode?

  Logging.info(
    '[JetStream Bridge] Auto-enabling test mode (NATS_URLS not set)',
    tag: 'JetstreamBridge::Railtie'
  )

  require_relative '../test_helpers'
  JetstreamBridge::TestHelpers.enable_test_mode!
end

.auto_enable_test_mode?Boolean



61
62
63
64
65
66
67
# File 'lib/jetstream_bridge/rails/integration.rb', line 61

def auto_enable_test_mode?
  rails_test? &&
    ENV['NATS_URLS'].to_s.strip.empty? &&
    !(defined?(JetstreamBridge::TestHelpers) &&
      JetstreamBridge::TestHelpers.respond_to?(:test_mode?) &&
      JetstreamBridge::TestHelpers.test_mode?)
end

.autostart_disabled?Boolean



69
70
71
72
73
74
75
# File 'lib/jetstream_bridge/rails/integration.rb', line 69

def autostart_disabled?
  return false if force_autostart?

  JetstreamBridge.config.lazy_connect ||
    env_disables_autostart? ||
    rake_task?
end

.autostart_skip_reasonObject



77
78
79
80
81
82
83
# File 'lib/jetstream_bridge/rails/integration.rb', line 77

def autostart_skip_reason
  return 'lazy_connect enabled' if JetstreamBridge.config.lazy_connect
  return 'JETSTREAM_BRIDGE_DISABLE_AUTOSTART set' if env_disables_autostart?
  return 'rake task' if rake_task?

  'unknown'
end

.boot_bridge!Object

Validate config, enable test mode if appropriate, and start the bridge unless auto-start is disabled.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jetstream_bridge/rails/integration.rb', line 30

def boot_bridge!
  auto_enable_test_mode!

  if autostart_disabled?
    message = "Auto-start skipped (reason: #{autostart_skip_reason}; " \
              'enable via lazy_connect=false, unset JETSTREAM_BRIDGE_DISABLE_AUTOSTART, ' \
              'or set JETSTREAM_BRIDGE_FORCE_AUTOSTART=1)'
    Logging.info(message, tag: 'JetstreamBridge::Railtie')
    return
  end

  JetstreamBridge.config.validate!
  JetstreamBridge.startup!
  log_started!
  log_development_connection_details! if rails_development?
  register_shutdown_hook!
end

.configure_logger!Object

Configure logger to use Rails.logger when available.



16
17
18
19
20
# File 'lib/jetstream_bridge/rails/integration.rb', line 16

def configure_logger!
  JetstreamBridge.configure do |config|
    config.logger ||= ::Rails.logger if defined?(::Rails.logger)
  end
end

.env_disables_autostart?Boolean



85
86
87
88
89
90
91
92
93
# File 'lib/jetstream_bridge/rails/integration.rb', line 85

def env_disables_autostart?
  value = ENV.fetch('JETSTREAM_BRIDGE_DISABLE_AUTOSTART', nil)
  return false if value.nil?

  normalized = value.to_s.strip.downcase
  return false if normalized.empty?

  !%w[false 0 no off].include?(normalized)
end

.force_autostart?Boolean



95
96
97
98
99
100
101
# File 'lib/jetstream_bridge/rails/integration.rb', line 95

def force_autostart?
  value = ENV.fetch('JETSTREAM_BRIDGE_FORCE_AUTOSTART', nil)
  return false if value.nil?

  normalized = value.to_s.strip.downcase
  %w[true 1 yes on].include?(normalized)
end

.log_development_connection_details!Object



107
108
109
110
111
112
113
114
# File 'lib/jetstream_bridge/rails/integration.rb', line 107

def log_development_connection_details!
  conn_state = JetstreamBridge::Connection.instance.state
  active_logger&.info("[JetStream Bridge] Connection state: #{conn_state}")
  active_logger&.info("[JetStream Bridge] Connected to: #{JetstreamBridge.config.nats_urls}")
  active_logger&.info("[JetStream Bridge] Stream: #{JetstreamBridge.config.stream_name}")
  active_logger&.info("[JetStream Bridge] Publishing to: #{JetstreamBridge.config.source_subject}")
  active_logger&.info("[JetStream Bridge] Consuming from: #{JetstreamBridge.config.destination_subject}")
end

.log_started!Object



103
104
105
# File 'lib/jetstream_bridge/rails/integration.rb', line 103

def log_started!
  active_logger&.info('[JetStream Bridge] Started successfully')
end

.rails_console?Boolean



131
132
133
# File 'lib/jetstream_bridge/rails/integration.rb', line 131

def rails_console?
  !!defined?(::Rails::Console)
end

.rails_development?Boolean



127
128
129
# File 'lib/jetstream_bridge/rails/integration.rb', line 127

def rails_development?
  defined?(::Rails) && ::Rails.respond_to?(:env) && ::Rails.env.development?
end

.rails_test?Boolean



123
124
125
# File 'lib/jetstream_bridge/rails/integration.rb', line 123

def rails_test?
  defined?(::Rails) && ::Rails.respond_to?(:env) && ::Rails.env.test?
end

.rake_task?Boolean



135
136
137
# File 'lib/jetstream_bridge/rails/integration.rb', line 135

def rake_task?
  File.basename($PROGRAM_NAME) == 'rake'
end

.register_shutdown_hook!Object



116
117
118
119
120
121
# File 'lib/jetstream_bridge/rails/integration.rb', line 116

def register_shutdown_hook!
  return if @shutdown_hook_registered

  at_exit { JetstreamBridge.shutdown! }
  @shutdown_hook_registered = true
end