Class: JetstreamBridge::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/jetstream_bridge/core/config.rb

Overview

Configuration object for JetStream Bridge.

Holds all configuration settings including NATS connection details, application identifiers, reliability features, and consumer tuning.

Examples:

Basic configuration

JetstreamBridge.configure do |config|
  config.nats_urls = "nats://localhost:4222"
  config.env = "production"
  config.app_name = "api_service"
  config.destination_app = "worker_service"
  config.use_outbox = true
  config.use_inbox = true
end

Using a preset

JetstreamBridge.configure_for(:production) do |config|
  config.nats_urls = ENV["NATS_URLS"]
  config.app_name = "api"
  config.destination_app = "worker"
end

Defined Under Namespace

Modules: Status

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jetstream_bridge/core/config.rb', line 99

def initialize
  @nats_urls       = ENV['NATS_URLS'] || ENV['NATS_URL'] || 'nats://localhost:4222'
  @env             = ENV['NATS_ENV']  || 'development'
  @app_name        = ENV['APP_NAME']  || 'app'
  @destination_app = ENV.fetch('DESTINATION_APP', nil)

  @max_deliver = 5
  @ack_wait    = '30s'
  @backoff     = %w[1s 5s 15s 30s 60s]

  @use_outbox   = false
  @use_inbox    = false
  @use_dlq      = true
  @outbox_model = 'JetstreamBridge::OutboxEvent'
  @inbox_model  = 'JetstreamBridge::InboxEvent'
  @logger       = nil
  @preset_applied = nil

  # Connection management
  @connect_retry_attempts = 3
  @connect_retry_delay = 2
  @lazy_connect = false
end

Instance Attribute Details

#ack_waitString, Integer

Time to wait for acknowledgment before redelivery

Returns:

  • (String, Integer)


64
65
66
# File 'lib/jetstream_bridge/core/config.rb', line 64

def ack_wait
  @ack_wait
end

#app_nameString

Application name for subject routing

Returns:

  • (String)


58
59
60
# File 'lib/jetstream_bridge/core/config.rb', line 58

def app_name
  @app_name
end

#backoffArray<String>

Backoff delays between retries

Returns:

  • (Array<String>)


67
68
69
# File 'lib/jetstream_bridge/core/config.rb', line 67

def backoff
  @backoff
end

#connect_retry_attemptsInteger

Number of retry attempts for initial connection

Returns:

  • (Integer)


91
92
93
# File 'lib/jetstream_bridge/core/config.rb', line 91

def connect_retry_attempts
  @connect_retry_attempts
end

#connect_retry_delayInteger

Delay between connection retry attempts (in seconds)

Returns:

  • (Integer)


94
95
96
# File 'lib/jetstream_bridge/core/config.rb', line 94

def connect_retry_delay
  @connect_retry_delay
end

#destination_appString

NATS server URL(s), comma-separated for multiple servers

Returns:

  • (String)


49
50
51
# File 'lib/jetstream_bridge/core/config.rb', line 49

def destination_app
  @destination_app
end

#envString

Environment namespace (development, staging, production)

Returns:

  • (String)


55
56
57
# File 'lib/jetstream_bridge/core/config.rb', line 55

def env
  @env
end

#inbox_modelString

ActiveRecord model class name for inbox events

Returns:

  • (String)


76
77
78
# File 'lib/jetstream_bridge/core/config.rb', line 76

def inbox_model
  @inbox_model
end

#lazy_connectBoolean

Enable lazy connection (connect on first use instead of during configure)

Returns:

  • (Boolean)


97
98
99
# File 'lib/jetstream_bridge/core/config.rb', line 97

def lazy_connect
  @lazy_connect
end

#loggerLogger?

Logger instance

Returns:

  • (Logger, nil)


85
86
87
# File 'lib/jetstream_bridge/core/config.rb', line 85

def logger
  @logger
end

#max_deliverInteger

Maximum delivery attempts before moving to DLQ

Returns:

  • (Integer)


61
62
63
# File 'lib/jetstream_bridge/core/config.rb', line 61

def max_deliver
  @max_deliver
end

#nats_urlsString

NATS server URL(s)

Returns:

  • (String)


52
53
54
# File 'lib/jetstream_bridge/core/config.rb', line 52

def nats_urls
  @nats_urls
end

#outbox_modelString

ActiveRecord model class name for outbox events

Returns:

  • (String)


79
80
81
# File 'lib/jetstream_bridge/core/config.rb', line 79

def outbox_model
  @outbox_model
end

#preset_appliedSymbol? (readonly)

Applied preset name

Returns:

  • (Symbol, nil)


88
89
90
# File 'lib/jetstream_bridge/core/config.rb', line 88

def preset_applied
  @preset_applied
end

#use_dlqBoolean

Enable dead letter queue

Returns:

  • (Boolean)


82
83
84
# File 'lib/jetstream_bridge/core/config.rb', line 82

def use_dlq
  @use_dlq
end

#use_inboxBoolean

Enable idempotent inbox pattern

Returns:

  • (Boolean)


73
74
75
# File 'lib/jetstream_bridge/core/config.rb', line 73

def use_inbox
  @use_inbox
end

#use_outboxBoolean

Enable transactional outbox pattern

Returns:

  • (Boolean)


70
71
72
# File 'lib/jetstream_bridge/core/config.rb', line 70

def use_outbox
  @use_outbox
end

Instance Method Details

#apply_preset(preset_name) ⇒ self

Apply a configuration preset

Parameters:

  • preset_name (Symbol, String)

    Name of preset (e.g., :production, :development)

Returns:

  • (self)


127
128
129
130
131
132
# File 'lib/jetstream_bridge/core/config.rb', line 127

def apply_preset(preset_name)
  require_relative 'config_preset'
  ConfigPreset.apply(self, preset_name)
  @preset_applied = preset_name.to_sym
  self
end

#destination_subjectString

Get the NATS subject this application subscribes to.

Examples:

config.env = "production"
config.app_name = "api"
config.destination_app = "worker"
config.destination_subject  # => "production.worker.sync.api"

Returns:

  • (String)

    Destination subject for consuming

Raises:



174
175
176
177
178
179
# File 'lib/jetstream_bridge/core/config.rb', line 174

def destination_subject
  validate_subject_component!(env, 'env')
  validate_subject_component!(app_name, 'app_name')
  validate_subject_component!(destination_app, 'destination_app')
  "#{env}.#{destination_app}.sync.#{app_name}"
end

#dlq_subjectString

Get the dead letter queue subject for this application.

Each app has its own DLQ for better isolation and monitoring.

Examples:

config.env = "production"
config.app_name = "api"
config.dlq_subject  # => "production.api.sync.dlq"

Returns:

  • (String)

    DLQ subject in format “#env.#app_name.sync.dlq”

Raises:



192
193
194
195
196
# File 'lib/jetstream_bridge/core/config.rb', line 192

def dlq_subject
  validate_subject_component!(env, 'env')
  validate_subject_component!(app_name, 'app_name')
  "#{env}.#{app_name}.sync.dlq"
end

#durable_nameString

Get the durable consumer name for this application.

Examples:

config.env = "production"
config.app_name = "api"
config.durable_name  # => "production-api-workers"

Returns:

  • (String)

    Durable name in format “#env-#app_name-workers”



205
206
207
# File 'lib/jetstream_bridge/core/config.rb', line 205

def durable_name
  "#{env}-#{app_name}-workers"
end

#source_subjectString

Get the NATS subject this application publishes to.

Producer publishes to: #env.app.sync.dest Consumer subscribes to: #env.dest.sync.app

Examples:

config.env = "production"
config.app_name = "api"
config.destination_app = "worker"
config.source_subject  # => "production.api.sync.worker"

Returns:

  • (String)

    Source subject for publishing

Raises:



157
158
159
160
161
162
# File 'lib/jetstream_bridge/core/config.rb', line 157

def source_subject
  validate_subject_component!(env, 'env')
  validate_subject_component!(app_name, 'app_name')
  validate_subject_component!(destination_app, 'destination_app')
  "#{env}.#{app_name}.sync.#{destination_app}"
end

#stream_nameString

Get the JetStream stream name for this environment.

Examples:

config.env = "production"
config.stream_name  # => "production-jetstream-bridge-stream"

Returns:

  • (String)

    Stream name in format “#env-jetstream-bridge-stream”



140
141
142
# File 'lib/jetstream_bridge/core/config.rb', line 140

def stream_name
  "#{env}-jetstream-bridge-stream"
end

#validate!true

Validate all configuration settings.

Checks that required settings are present and valid. Raises errors for any invalid configuration.

Examples:

config.validate!  # Raises if destination_app is missing

Returns:

  • (true)

    If configuration is valid

Raises:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/jetstream_bridge/core/config.rb', line 218

def validate!
  errors = []
  errors << 'destination_app is required' if destination_app.to_s.strip.empty?
  errors << 'nats_urls is required' if nats_urls.to_s.strip.empty?
  errors << 'env is required' if env.to_s.strip.empty?
  errors << 'app_name is required' if app_name.to_s.strip.empty?
  errors << 'max_deliver must be >= 1' if max_deliver.to_i < 1
  errors << 'backoff must be an array' unless backoff.is_a?(Array)
  errors << 'backoff must not be empty' if backoff.is_a?(Array) && backoff.empty?

  raise ConfigurationError, "Configuration errors: #{errors.join(', ')}" if errors.any?

  true
end