Class: LogTwuncator::Daemon

Inherits:
Win32::Daemon
  • Object
show all
Defined in:
lib/log_twuncator/daemon.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Daemon

Returns a new instance of Daemon.



20
21
22
23
24
# File 'lib/log_twuncator/daemon.rb', line 20

def initialize(options)      
  @config, @delay, @log = options[:config], options[:delay], options[:log]
  @truncators = []      
  @logger = Logger.new(@log)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/log_twuncator/daemon.rb', line 14

def config
  @config
end

#delayObject (readonly)

Returns the value of attribute delay.



14
15
16
# File 'lib/log_twuncator/daemon.rb', line 14

def delay
  @delay
end

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/log_twuncator/daemon.rb', line 14

def logger
  @logger
end

#truncatorsObject (readonly)

Returns the value of attribute truncators.



14
15
16
# File 'lib/log_twuncator/daemon.rb', line 14

def truncators
  @truncators
end

Class Method Details

.run(args = ARGV) ⇒ Object



16
17
18
# File 'lib/log_twuncator/daemon.rb', line 16

def self.run(args = ARGV)
  new LogTwuncator::Options.parse(args)      
end

Instance Method Details

#load_configObject

Loads config yaml file. Sets the truncator class defaults from the config section if one is labelled ‘defaults’. Create truncators for each config section, which are run in the service_main loop. Config file can have ERB tags embedded for power and convenience



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

def load_config
  raise LogTwuncator::ConfigFileNotFound unless File.exists?(@config)
  config = YAML::load(ERB.new(IO.read(@config)).result)
  set_defaults(symbolize_keys(config.delete('defaults'))) if config['defaults']
  config.map do |key, options|       
    options[:logger] = @logger
    options[:name] = key
    begin
      @truncators << LogTwuncator::Truncator.new(symbolize_keys(options))
    rescue InvalidTruncatorOptions => e
      log "Config set '#{key}' was skipped due to the following errors:\n#{e.message}"
    end
  end
rescue ConfigFileNotFound
  log "Config file not found"
rescue => e
  log "Unknown error occurred: #{e}"
end

#service_initObject



26
27
28
29
# File 'lib/log_twuncator/daemon.rb', line 26

def service_init
  log "starting service"
  load_config
end

#service_mainObject



31
32
33
34
35
36
37
38
# File 'lib/log_twuncator/daemon.rb', line 31

def service_main
  while running?
    @truncators.each {|truncator| truncator.truncate }
    
    # sleep for 1 second for number of seconds in delay minutes which prevents process blocking
    (@delay * 60).times {sleep 1}
  end
end

#service_stopObject



40
41
42
# File 'lib/log_twuncator/daemon.rb', line 40

def service_stop
  log "stopping service"
end