Class: Daemonizer::Config
- Inherits:
-
Object
- Object
- Daemonizer::Config
- 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
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Instance Method Summary collapse
- #init_defaults ⇒ Object
-
#initialize(pool, options) ⇒ Config
constructor
A new instance of Config.
- #initialize_handler ⇒ Object
- #name ⇒ Object
- #validate ⇒ Object
- #validate_file(filename) ⇒ Object
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, ) @pool = pool = init_defaults validate initialize_handler @handler end |
Instance Attribute Details
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
7 8 9 |
# File 'lib/daemonizer/config.rb', line 7 def handler @handler end |
#pool ⇒ Object (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_defaults ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/daemonizer/config.rb', line 27 def init_defaults [:prepare] ||= nil [:start] ||= nil [:workers] ||= 1 [:log_file] ||= "log/#{@pool}.log" [:poll_period] ||= 5 [:pid_file] ||= "pid/#{@pool}.pid" [:handler] ||= nil [:handler_options] ||= {} [:callbacks] ||= {} [:on_poll] ||= [] [:cow_friendly] = true if [:cow_friendly].nil? [:log_level] ||= :info end |
#initialize_handler ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/daemonizer/config.rb', line 18 def initialize_handler if [:start] @handler = FakeHandler.new([:prepare], [:start], [:handler_options]) [:start] = [:prepare] = nil elsif @handler = [:handler].new([:handler_options]) end end |
#name ⇒ Object
91 92 93 |
# File 'lib/daemonizer/config.rb', line 91 def name @pool end |
#validate ⇒ Object
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 [:workers] < 1 raise ConfigError, "Poll period should be more then zero" if [:poll_period] < 1 raise ConfigError, "Log level should be one of [#{VALID_LOG_LEVELS.map(&:to_s).join(',')}]" unless VALID_LOG_LEVELS.include?([:log_level].to_sym) if [:handler] raise ConfigError, "Handler should be a class" unless [:handler].is_a?(Class) raise ConfigError, "Handler should respond to :start" unless [:handler].public_instance_methods.include?('start') raise ConfigError, "Handler set. Don't use :start and :before init in Demfile" if [:prepare] || [:start] else if [:prepare] raise ConfigError, "prepare should have block" unless [:prepare].is_a?(Proc) end raise ConfigError, "start should be set" if [:start].nil? raise ConfigError, "start should have block" unless [: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 |