Sha256: 1a868a3970dca75211658169ccdcc8d4aa8ff5f8e03feb53423ef39353867486

Contents?: true

Size: 1.37 KB

Versions: 21

Compression:

Stored size: 1.37 KB

Contents

class LogLocationCache
  TIME_LIMIT = 6000

  def initialize
    clear
  end

  def [](pid)
    pid = pid.to_i
    unless pids.include?(pid)
      set_pid_log_location(pid, get_pid_location(pid))
    end
    pids[pid][:value]
  end

private
  attr_accessor :query_counter
  attr_accessor :pids

  def clear
    self.query_counter = 0
    self.pids = {}
    self
  end

  def remove_old_values
    self.query_counter = query_counter + 1
    if query_counter < 1000
      return
    end
    self.query_counter = 0
    limit = Time.now - TIME_LIMIT
    pids.keys.each do |pid|
      if pids[pid][:time] < limit
        pids.remove(pid)
      end
    end
    self
  end

  def get_pid_location(lpid)
    folder = log_folder(lpid)
    return nil if folder.nil?
    File.join(folder, 'current')
  end

  def log_command(lpid)
    return nil if lpid.nil?
    ps_output = `ps -o args -p #{lpid} 2>&1`.split("\n")
    ps_output.shift
    cmd = ps_output.first
    cmd = cmd.chomp unless cmd.nil?
    cmd = nil if cmd == ''
    cmd
  end

  def log_folder(lpid)
    cmd = log_command(lpid)
    return nil if cmd.nil?
    args = cmd.split(/\s+/).select { |arg| arg !~ /^\-/ }
    return nil if args.shift != 'svlogd'
    args.shift
  end

  def set_pid_log_location(pid, log_location)
    remove_old_values
    pids[pid.to_i] = {
      :value => log_location,
      :time  => Time.now
    }
    self
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
runit-man-1.6.4 lib/runit-man/log_location_cache.rb
runit-man-1.6.3 lib/runit-man/log_location_cache.rb
runit-man-1.6.2 lib/runit-man/log_location_cache.rb
runit-man-1.6.1 lib/runit-man/log_location_cache.rb
runit-man-1.6.0 lib/runit-man/log_location_cache.rb
runit-man-1.5.4 lib/runit-man/log_location_cache.rb
runit-man-1.5.3 lib/runit-man/log_location_cache.rb
runit-man-1.5.2 lib/runit-man/log_location_cache.rb
runit-man-1.5.1 lib/runit-man/log_location_cache.rb
runit-man-1.5.0 lib/runit-man/log_location_cache.rb
runit-man-1.4.9 lib/runit-man/log_location_cache.rb
runit-man-1.4.8 lib/runit-man/log_location_cache.rb
runit-man-1.4.7 lib/runit-man/log_location_cache.rb
runit-man-1.4.6 lib/runit-man/log_location_cache.rb
runit-man-1.4.5 lib/runit-man/log_location_cache.rb
runit-man-1.4.4 lib/runit-man/log_location_cache.rb
runit-man-1.4.3 lib/runit-man/log_location_cache.rb
runit-man-1.4.1 lib/runit-man/log_location_cache.rb
runit-man-1.4 lib/runit-man/log_location_cache.rb
runit-man-1.3 lib/runit-man/log_location_cache.rb