Class: NatsWork::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/natswork/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/natswork/configuration.rb', line 18

def initialize
  @servers = ['nats://localhost:4222']
  @pool_size = 5
  @pool_timeout = 5
  @max_reconnect_attempts = 10
  @reconnect_time_wait = 2
  @request_timeout = 5
  @user = nil
  @password = nil
  @token = nil
  @tls = nil
  @jetstream_enabled = true
  @jetstream_prefix = 'natswork'
  @use_jetstream = true
  @namespace = 'natswork'
  @max_retries = 3
  @job_timeout = 30
  @sync_timeout = 30
  @logger = nil
end

Instance Attribute Details

#jetstream_enabledObject

Returns the value of attribute jetstream_enabled.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def jetstream_enabled
  @jetstream_enabled
end

#jetstream_prefixObject

Returns the value of attribute jetstream_prefix.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def jetstream_prefix
  @jetstream_prefix
end

#job_timeoutObject

Returns the value of attribute job_timeout.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def job_timeout
  @job_timeout
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def logger
  @logger
end

#max_reconnect_attemptsObject

Returns the value of attribute max_reconnect_attempts.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def max_reconnect_attempts
  @max_reconnect_attempts
end

#max_retriesObject

Returns the value of attribute max_retries.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def max_retries
  @max_retries
end

#namespaceObject

Returns the value of attribute namespace.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def namespace
  @namespace
end

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def password
  @password
end

#pool_sizeObject

Returns the value of attribute pool_size.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def pool_size
  @pool_size
end

#pool_timeoutObject

Returns the value of attribute pool_timeout.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def pool_timeout
  @pool_timeout
end

#reconnect_time_waitObject

Returns the value of attribute reconnect_time_wait.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def reconnect_time_wait
  @reconnect_time_wait
end

#request_timeoutObject

Returns the value of attribute request_timeout.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def request_timeout
  @request_timeout
end

#serversObject

Returns the value of attribute servers.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def servers
  @servers
end

#sync_timeoutObject

Returns the value of attribute sync_timeout.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def sync_timeout
  @sync_timeout
end

#tlsObject

Returns the value of attribute tls.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def tls
  @tls
end

#tokenObject

Returns the value of attribute token.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def token
  @token
end

#use_jetstreamObject

Returns the value of attribute use_jetstream.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def use_jetstream
  @use_jetstream
end

#userObject

Returns the value of attribute user.



12
13
14
# File 'lib/natswork/configuration.rb', line 12

def user
  @user
end

Class Method Details

.from_envObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/natswork/configuration.rb', line 92

def from_env
  config = new

  config.servers = ENV['NATSWORK_SERVERS'].split(',').map(&:strip) if ENV['NATSWORK_SERVERS']

  config.pool_size = ENV['NATSWORK_POOL_SIZE'].to_i if ENV['NATSWORK_POOL_SIZE']
  config.pool_timeout = ENV['NATSWORK_POOL_TIMEOUT'].to_i if ENV['NATSWORK_POOL_TIMEOUT']
  config.max_reconnect_attempts = ENV['NATSWORK_MAX_RECONNECT'].to_i if ENV['NATSWORK_MAX_RECONNECT']
  config.reconnect_time_wait = ENV['NATSWORK_RECONNECT_WAIT'].to_i if ENV['NATSWORK_RECONNECT_WAIT']
  config.request_timeout = ENV['NATSWORK_REQUEST_TIMEOUT'].to_i if ENV['NATSWORK_REQUEST_TIMEOUT']

  config.user = ENV['NATSWORK_USER'] if ENV['NATSWORK_USER']
  config.password = ENV['NATSWORK_PASSWORD'] if ENV['NATSWORK_PASSWORD']
  config.token = ENV['NATSWORK_TOKEN'] if ENV['NATSWORK_TOKEN']

  if ENV['NATSWORK_JETSTREAM_ENABLED']
    config.jetstream_enabled = ENV['NATSWORK_JETSTREAM_ENABLED'].downcase == 'true'
  end

  config.jetstream_prefix = ENV['NATSWORK_JETSTREAM_PREFIX'] if ENV['NATSWORK_JETSTREAM_PREFIX']

  config
end

.from_hash(hash) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/natswork/configuration.rb', line 81

def from_hash(hash)
  config = new

  hash.each do |key, value|
    setter = "#{key}="
    config.send(setter, value) if config.respond_to?(setter)
  end

  config
end

Instance Method Details

#to_connection_optionsObject Also known as: connection_options



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/natswork/configuration.rb', line 39

def to_connection_options
  opts = {
    servers: servers,
    max_reconnect_attempts: max_reconnect_attempts,
    reconnect_time_wait: reconnect_time_wait
  }

  opts[:user] = user if user
  opts[:password] = password if password
  opts[:token] = token if token
  opts[:tls] = tls if tls

  opts
end

#to_pool_optionsObject



56
57
58
59
60
61
62
# File 'lib/natswork/configuration.rb', line 56

def to_pool_options
  {
    size: pool_size,
    timeout: pool_timeout,
    connection_options: to_connection_options
  }
end

#validate!Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/natswork/configuration.rb', line 64

def validate!
  raise ConfigurationError, 'At least one server must be configured' if servers.empty?

  servers.each do |server|
    raise ConfigurationError, "Invalid server URL: #{server}" unless server =~ %r{^nats://}
  end

  raise ConfigurationError, 'Pool size must be positive' if pool_size <= 0
  raise ConfigurationError, 'Pool timeout must be positive' if pool_timeout <= 0
  raise ConfigurationError, 'Max reconnect attempts must be non-negative' if max_reconnect_attempts.negative?
  raise ConfigurationError, 'Reconnect time wait must be positive' if reconnect_time_wait <= 0
  raise ConfigurationError, 'Request timeout must be positive' if request_timeout <= 0

  true
end