module StackifyRubyAPM::Util::TraceLogWatcher

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

Public Class Methods

delete_trace_logs(config) click to toggle source

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.

# 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