Class: Daemonizer::Config

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

Defined Under Namespace

Classes: ConfigError

Constant Summary collapse

VALID_LOG_LEVELS =
[:debug, :info, :warn, :error, :fatal]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, options) ⇒ Config

Returns a new instance of Config.



9
10
11
12
13
14
15
16
# File 'lib/daemonizer/config.rb', line 9

def initialize(pool, options)
  @pool = pool
  @options = options
  init_defaults
  validate
  initialize_handler
  @handler
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



7
8
9
# File 'lib/daemonizer/config.rb', line 7

def handler
  @handler
end

#poolObject (readonly)

Returns the value of attribute pool.



7
8
9
# File 'lib/daemonizer/config.rb', line 7

def pool
  @pool
end

Instance Method Details

#init_defaultsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/daemonizer/config.rb', line 27

def init_defaults
  @options[:prepare] ||= nil
  @options[:start] ||= nil
  @options[:workers] ||= 1
  @options[:log_file] ||= "log/#{@pool}.log"
  @options[:poll_period] ||= 5
  @options[:pid_file] ||= "pid/#{@pool}.pid"
  @options[:handler] ||= nil
  @options[:handler_options] ||= {}
  @options[:callbacks] ||= {}
  @options[:on_poll] ||= []
  @options[:cow_friendly] = true if @options[:cow_friendly].nil?
  @options[:log_level] ||= :info
end

#initialize_handlerObject



18
19
20
21
22
23
24
25
# File 'lib/daemonizer/config.rb', line 18

def initialize_handler
  if @options[:start]
    @handler = FakeHandler.new(@options[:prepare], @options[:start], @options[:handler_options])
    @options[:start] = @options[:prepare] = nil
  elsif
    @handler = @options[:handler].new(@options[:handler_options])
  end
end

#nameObject



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

def name
  @pool
end

#validateObject

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/daemonizer/config.rb', line 59

def validate
  raise ConfigError, "Workers count should be more then zero" if @options[:workers] < 1
  raise ConfigError, "Poll period should be more then zero" if @options[:poll_period] < 1
  raise ConfigError, "Log level should be one of [#{VALID_LOG_LEVELS.map(&:to_s).join(',')}]" unless VALID_LOG_LEVELS.include?(@options[:log_level].to_sym)
  if @options[:handler]
    raise ConfigError, "Handler should be a class" unless @options[:handler].is_a?(Class)
    raise ConfigError, "Handler should respond to :start" unless @options[:handler].public_instance_methods.include?('start')
    raise ConfigError, "Handler set. Don't use :start and :before init in Demfile" if @options[:prepare] || @options[:start]
  else
    if @options[:prepare]
      raise ConfigError, "prepare should have block" unless @options[:prepare].is_a?(Proc)
    end
    raise ConfigError, "start should be set" if @options[:start].nil?
    raise ConfigError, "start should have block" unless @options[:start].is_a?(Proc)
  end

  validate_file(self.log_file)
  validate_file(self.pid_file)
end

#validate_file(filename) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/daemonizer/config.rb', line 42

def validate_file(filename)
  # file validation
  if File.exist?(filename)
    if !File.file?(filename)
      raise ConfigError, "'#{filename}' is not a regular file"
    elsif !File.writable?(filename)
      raise ConfigError, "'#{filename}' is not writable!"
    end
  else # ensure directory is writable
    dir = File.dirname(filename)
    if not File.writable?(dir)
      raise ConfigError, "'#{dir}' is not writable!"
    end
    File.open(filename, 'w') { |f| f.write('') } #creating empty file
  end
end