Class: Nonnative::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/nonnative/configuration.rb

Overview

The gem configuration object.

You can populate configuration either programmatically via the DSL (#process, #server, #service), or by loading a YAML file via #load_file.

The configuration is consumed when start is called.

Programmatic configuration

Nonnative.configure do |config|
config.name = 'example'
config.url = 'http://127.0.0.1:8080'
config.log = 'test.log'

config.process do |p|
  p.name = 'api'
  p.command = -> { './bin/api' }
  p.host = '127.0.0.1'
  p.ports = [8080, 9090]
  p.timeout = 10
  p.log = 'api.log'
  p.readiness = [{ kind: 'http', port: 8080, path: '/example/readyz' }]
end
end

File-based configuration

Nonnative.configure do |config|
config.load_file('features/configs/processes.yml')
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

Creates an empty configuration.



39
40
41
42
43
# File 'lib/nonnative/configuration.rb', line 39

def initialize
  @processes = []
  @servers = []
  @services = []
end

Instance Attribute Details

#logString?

Returns path to the Nonnative log file.

Returns:

  • (String, nil)

    path to the Nonnative log file



55
56
57
# File 'lib/nonnative/configuration.rb', line 55

def log
  @log
end

#nameString?

Returns logical system name (used for observability endpoints).

Returns:

  • (String, nil)

    logical system name (used for observability endpoints)



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

def name
  @name
end

#processesArray<Nonnative::ConfigurationProcess>

Returns configured processes.

Returns:



58
59
60
# File 'lib/nonnative/configuration.rb', line 58

def processes
  @processes
end

#serversArray<Nonnative::ConfigurationServer>

Returns configured in-process servers.

Returns:



61
62
63
# File 'lib/nonnative/configuration.rb', line 61

def servers
  @servers
end

#servicesArray<Nonnative::ConfigurationService>

Returns configured external dependencies.

Returns:



64
65
66
# File 'lib/nonnative/configuration.rb', line 64

def services
  @services
end

#urlString?

Returns base URL for observability queries (for example "http://127.0.0.1:8080").

Returns:

  • (String, nil)

    base URL for observability queries (for example "http://127.0.0.1:8080")



52
53
54
# File 'lib/nonnative/configuration.rb', line 52

def url
  @url
end

#versionString?

Returns configuration version.

Returns:

  • (String, nil)

    configuration version



49
50
51
# File 'lib/nonnative/configuration.rb', line 49

def version
  @version
end

Instance Method Details

#load_file(path) ⇒ void

This method returns an undefined value.

Loads a configuration file and appends its runners to this instance.

The file is loaded using safe YAML parsing. ERB and environment placeholders are not evaluated, arbitrary object deserialization is not allowed, and unknown structural keys may be ignored. Top-level supported attributes are copied onto this object, and runner sections are transformed into configuration runner objects.

Parameters:

  • path (String)

    path to a configuration file (typically YAML)



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nonnative/configuration.rb', line 75

def load_file(path)
  cfg = Nonnative::ConfigurationFile.load(path)

  self.version = cfg.version
  self.name = cfg.name
  self.url = cfg.url
  self.log = cfg.log

  add_processes(cfg)
  add_servers(cfg)
  add_services(cfg)
end

#process {|process| ... } ⇒ void

This method returns an undefined value.

Adds a process configuration entry.

Yield Parameters:



92
93
94
95
96
97
# File 'lib/nonnative/configuration.rb', line 92

def process
  process = Nonnative::ConfigurationProcess.new
  yield process

  processes << process
end

#process_by_name(name) ⇒ Nonnative::ConfigurationProcess

Finds a configured process by name.

Parameters:

  • name (String)

Returns:

Raises:



129
130
131
132
133
134
# File 'lib/nonnative/configuration.rb', line 129

def process_by_name(name)
  process = processes.find { |s| s.name == name }
  raise NotFoundError, "Could not find process with name '#{name}'" if process.nil?

  process
end

#server {|server| ... } ⇒ void

This method returns an undefined value.

Adds a server configuration entry.

Yield Parameters:



103
104
105
106
107
108
# File 'lib/nonnative/configuration.rb', line 103

def server
  server = Nonnative::ConfigurationServer.new
  yield server

  servers << server
end

#service {|service| ... } ⇒ void

This method returns an undefined value.

Adds a service configuration entry.

A "service" represents an externally managed dependency. It does not manage a Ruby thread or OS process, but it can wait for TCP readiness and optionally control a proxy for the dependency.

Yield Parameters:



117
118
119
120
121
122
# File 'lib/nonnative/configuration.rb', line 117

def service
  service = Nonnative::ConfigurationService.new
  yield service

  services << service
end