Class: LogTwuncator::Daemon
- Inherits:
-
Win32::Daemon
- Object
- Win32::Daemon
- LogTwuncator::Daemon
- Defined in:
- lib/log_twuncator/daemon.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#delay ⇒ Object
readonly
Returns the value of attribute delay.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#truncators ⇒ Object
readonly
Returns the value of attribute truncators.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options) ⇒ Daemon
constructor
A new instance of Daemon.
-
#load_config ⇒ Object
Loads config yaml file.
- #service_init ⇒ Object
- #service_main ⇒ Object
- #service_stop ⇒ Object
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() @config, @delay, @log = [:config], [:delay], [:log] @truncators = [] @logger = Logger.new(@log) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/log_twuncator/daemon.rb', line 14 def config @config end |
#delay ⇒ Object (readonly)
Returns the value of attribute delay.
14 15 16 |
# File 'lib/log_twuncator/daemon.rb', line 14 def delay @delay end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
14 15 16 |
# File 'lib/log_twuncator/daemon.rb', line 14 def logger @logger end |
#truncators ⇒ Object (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_config ⇒ Object
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, | [:logger] = @logger [:name] = key begin @truncators << LogTwuncator::Truncator.new(symbolize_keys()) 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_init ⇒ Object
26 27 28 29 |
# File 'lib/log_twuncator/daemon.rb', line 26 def service_init log "starting service" load_config end |
#service_main ⇒ Object
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_stop ⇒ Object
40 41 42 |
# File 'lib/log_twuncator/daemon.rb', line 40 def service_stop log "stopping service" end |