Class: Uc::Config

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/uc/config.rb

Instance Method Summary collapse

Methods included from Logger

#event_stream, event_stream, logger, #logger, stderr, #stderr

Constructor Details

#initialize(app_dir, config_file = nil) ⇒ Config

Returns a new instance of Config.



10
11
12
13
# File 'lib/uc/config.rb', line 10

def initialize(app_dir, config_file = nil)
  @config_file = config_file
  @app_dir = app_dir
end

Instance Method Details

#app_dirObject



77
78
79
# File 'lib/uc/config.rb', line 77

def app_dir
  config[:working_dir]
end

#backlog(queue_size) ⇒ Object



57
58
59
# File 'lib/uc/config.rb', line 57

def backlog(queue_size)
  config[:queue_size] = queue_size
end

#configObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/uc/config.rb', line 19

def config
  return @config if @config
  @config = {
    instances: 2,
    queue_size: 1024,
    timeout: 30,
    prestart_url: "/",
    working_dir: @app_dir,
    event_queue: "unicorn_#{Process.uid}",
    ready_wait: 5,
    listen: []
  }
  load_env_yml "#{app_dir}/config/env.yml"
  load_env_yml "#{app_dir}/.env.yml"
  read_from_file
  return @config
end

#config_fileObject



15
16
17
# File 'lib/uc/config.rb', line 15

def config_file
  @config_file ||= "#{app_dir}/config/uc.rb"
end

#env_yml(path, required: true) ⇒ Object



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

def env_yml(path, required: true)
  load_env_yml(path, required: required)
end

#event_queue(event_queue) ⇒ Object



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

def event_queue(event_queue)
  config[:event_queue] = event_queue
end

#event_queue_nameObject



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

def event_queue_name
  config[:event_queue]
end

#instances(num_instances) ⇒ Object



53
54
55
# File 'lib/uc/config.rb', line 53

def instances(num_instances)
  config[:instances] = num_instances
end

#listen(*ports) ⇒ Object



41
42
43
# File 'lib/uc/config.rb', line 41

def listen(*ports)
  config[:listen] = ports
end

#load_envObject



89
90
91
92
# File 'lib/uc/config.rb', line 89

def load_env
  # all environment variables will be loaded on config parsing
  config
end

#load_env_yml(path, required: false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/uc/config.rb', line 94

def load_env_yml(path, required: false)
  if not File.readable? path
    raise Error, "env file #{path} unreadable" if required
    logger.debug "skipped loading env from #{path}"
    return {}
  end
  logger.debug "loading env from #{path}"
  h = YAML.load_file(path)
  h.each { |k,v| ENV[k] = v.to_s }
rescue => e
  raise Error, "failed to load env from #{path} : #{e.message}" if required
  logger.debug "failed to load env from #{path} : #{e.message}"
end

#prestart_url(url) ⇒ Object



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

def prestart_url(url)
  config[:prestart_url] = url
end

#read_from_fileObject



108
109
110
111
112
113
# File 'lib/uc/config.rb', line 108

def read_from_file
  return if not File.readable? config_file
  instance_eval(File.read(config_file))
rescue NoMethodError => e
  logger.warn "invalid option used in config: #{e.name}"
end

#ready_wait(wait_timeout) ⇒ Object



45
46
47
# File 'lib/uc/config.rb', line 45

def ready_wait(wait_timeout)
  config[:ready_wait] = wait_timeout.to_i
end

#skip_clean_env(value) ⇒ Object



81
82
83
# File 'lib/uc/config.rb', line 81

def skip_clean_env(value)
  config[:skip_clean_env] = value
end

#timeout(secs) ⇒ Object



65
66
67
# File 'lib/uc/config.rb', line 65

def timeout(secs)
  config[:timeout] = secs
end

#to_hObject



37
38
39
# File 'lib/uc/config.rb', line 37

def to_h
  config
end

#working_dir(working_dir) ⇒ Object



69
70
71
# File 'lib/uc/config.rb', line 69

def working_dir(working_dir)
  config[:working_dir] = working_dir
end