Class: Daemonizer::Config

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

Defined Under Namespace

Classes: ConfigError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, options) ⇒ Config

Returns a new instance of Config.



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

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

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



5
6
7
# File 'lib/daemonizer/config.rb', line 5

def handler
  @handler
end

#poolObject (readonly)

Returns the value of attribute pool.



5
6
7
# File 'lib/daemonizer/config.rb', line 5

def pool
  @pool
end

Instance Method Details

#init_defaultsObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/daemonizer/config.rb', line 35

def init_defaults
  @options[:before_init] ||= nil
  @options[:after_init] ||= nil
  @options[:engine] ||= :fork
  @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[:cow_friendly] = true if @options[:cow_friendly].nil?
end

#init_loggerObject



26
27
28
29
30
31
32
33
# File 'lib/daemonizer/config.rb', line 26

def init_logger
  @logger = Logger.new @pool.to_s
  outputter = FileOutputter.new('log', :filename => self.log_file)
  outputter.formatter = PatternFormatter.new :pattern => "%d - %l %g - %m"
  @logger.outputters = outputter
  @logger.level = INFO
  GDC.set "#{Process.pid}/monitor"
end

#initialize_handlerObject



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

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

#loggerObject



82
83
84
# File 'lib/daemonizer/config.rb', line 82

def logger
  @logger
end

#nameObject



78
79
80
# File 'lib/daemonizer/config.rb', line 78

def name
  @pool
end

#validateObject

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/daemonizer/config.rb', line 48

def validate
  raise ConfigError, "Workers count should be more then zero" if @options[:workers] < 1
  raise ConfigError, "Engine #{@options[:engine]} is not known" unless [:fork, :thread].include?(@options[:engine])

  raise ConfigError, "Poll period should be more then zero" if @options[:poll_period] < 1
  if @options[:handler]
    raise ConfigError, "Handler should be a class" unless @options[:handler].is_a?(Class)
    raise ConfigError, "Handler should respond to :after_init" unless @options[:handler].public_instance_methods.include?('after_init')
    raise ConfigError, "Handler set. Don't use :after_init and :before init in Demfile" if @options[:before_init] || @options[:after_init]
  else
    if @options[:before_init]
      raise ConfigError, "before_init should have block" unless @options[:before_init].is_a?(Proc)
    end
    raise ConfigError, "after_init should be set" if @options[:after_init].nil?
    raise ConfigError, "after_init should have block" unless @options[:after_init].is_a?(Proc)
  end
end