Module: StackifyRubyAPM::Util::TraceLogWatcher

Defined in:
lib/stackify_apm/util/trace_log_watcher.rb

Overview

rubocop:disable all This class will handle the monitor of trace log files.

Class Method Summary collapse

Class Method Details

.delete_trace_logs(config) ⇒ Object

delete_trace_logs - This method will scan the directory ‘/usr/local/stackify/stackify-ruby-apm/log/` for every 1 hour(configurable) and delete all trace log files that are older than 1 hour except the stackify-ruby-apm.log files will remain.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stackify_apm/util/trace_log_watcher.rb', line 13

def self.delete_trace_logs(config)
  format = '%Y-%m-%d %H:%M:%S %z'
  log_trace_path = config.log_trace_path + '/*'
  file_age = config.trace_log_age_in_hour
  interval = config.check_trace_log_per_hour
  scheduler = Rufus::Scheduler.new
  scheduler.every(interval, tag: 'stackify_ruby_apm') do
    Dir[log_trace_path].select { |exclude_file|
      exclude_file =~ /^(?!.*stackify-ruby-apm-[0-9].log).*$/
    }.map { |file|
      modified_time = File.mtime(file)
      t1 = DateTime.parse(modified_time.to_s, format)
      current_time = Time.now.localtime.strftime(format)
      t2 = DateTime.parse(current_time.to_s, format)
      if t2 > t1
        if modified_time < (Time.now - file_age)
          FileUtils.rm(file)
        end
      end
    }
  end
end