Class: Daemonic::Configuration

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

Constant Summary collapse

Invalid =
Class.new(ArgumentError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_options, pwd) ⇒ Configuration

Returns a new instance of Configuration.



11
12
13
14
15
# File 'lib/daemonic/configuration.rb', line 11

def initialize(given_options, pwd)
  @given_options = given_options
  @pwd = pwd
  @eventual_config = {}
end

Instance Attribute Details

#eventual_configObject (readonly)

Returns the value of attribute eventual_config.



9
10
11
# File 'lib/daemonic/configuration.rb', line 9

def eventual_config
  @eventual_config
end

#given_optionsObject (readonly)

Returns the value of attribute given_options.



9
10
11
# File 'lib/daemonic/configuration.rb', line 9

def given_options
  @given_options
end

Instance Method Details

#commandObject



17
18
19
# File 'lib/daemonic/configuration.rb', line 17

def command
  self[:command]
end

#config_fileObject



25
26
27
# File 'lib/daemonic/configuration.rb', line 25

def config_file
  self[:config_file]
end

#daemonize?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/daemonic/configuration.rb', line 21

def daemonize?
  self[:daemonize]
end

#logfileObject



56
57
58
# File 'lib/daemonic/configuration.rb', line 56

def logfile
  self[:logfile] || STDOUT
end

#loggerObject



60
61
62
63
64
65
66
67
# File 'lib/daemonic/configuration.rb', line 60

def logger
  @logger ||= ::Logger.new(logfile).tap { |logger|
    logger.formatter = proc { |severity, datetime, progname, msg|
      "[#{severity}] [#{datetime}] [#{program_name}] [#{Process.pid}] #{msg}\n"
    }
    logger.level = ::Logger.const_get(loglevel)
  }
end

#loglevelObject



73
74
75
# File 'lib/daemonic/configuration.rb', line 73

def loglevel
  (self[:loglevel] || "INFO").to_s.upcase
end

#pidfileObject



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

def pidfile
  self[:pidfile] || File.join(working_dir, "tmp/#{program_name}.pid")
end

#program_nameObject



48
49
50
# File 'lib/daemonic/configuration.rb', line 48

def program_name
  self[:program_name] || File.basename(working_dir)
end

#reloadObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/daemonic/configuration.rb', line 33

def reload
  @eventual_config = {}
  @logger = nil
  load_options defaults
  load_options given_options
  load_options config_file_options if config_file
  load_options given_options
  logger.debug { to_h.inspect }
  validate
end

#to_hObject



69
70
71
# File 'lib/daemonic/configuration.rb', line 69

def to_h
  eventual_config
end

#workersObject



29
30
31
# File 'lib/daemonic/configuration.rb', line 29

def workers
  Integer(self[:workers] || "1")
end

#working_dirObject



44
45
46
# File 'lib/daemonic/configuration.rb', line 44

def working_dir
  self[:working_dir]
end