Sha256: f5e2682657b1ee949b806e5f1b75a22ba36c633b7d3853a8e50e0adc7362fe54

Contents?: true

Size: 1.55 KB

Versions: 26

Compression:

Stored size: 1.55 KB

Contents

# Logic for the serialized file access
class ScoutRails::LayawayFile
  def path
    "#{ScoutRails::Agent.instance.log_path}/scout_rails.db"
  end

  def dump(object)
    Marshal.dump(object)
  end

  def load(dump)
    if dump.size == 0
      ScoutRails::Agent.instance.logger.debug("No data in layaway file.")
      return nil
    end
    Marshal.load(dump)
  rescue ArgumentError, TypeError => e
    ScoutRails::Agent.instance.logger.debug("Error loading data from layaway file: #{e.inspect}")
    ScoutRails::Agent.instance.logger.debug(e.backtrace.inspect)
    nil
  end

  def read_and_write
    File.open(path, File::RDWR | File::CREAT) do |f|
      f.flock(File::LOCK_EX)
      begin
        result = (yield get_data(f))
        f.rewind
        f.truncate(0)
        if result
          write(f, dump(result))
        end
      ensure
        f.flock(File::LOCK_UN)
      end
    end
  rescue Errno::ENOENT, Exception  => e
    ScoutRails::Agent.instance.logger.error(e.message)
    ScoutRails::Agent.instance.logger.debug(e.backtrace.split("\n"))
  end

  def get_data(f)
    data = read_until_end(f)
    result = load(data)
    f.truncate(0)
    result
  end

  def write(f, string)
    result = 0
    while (result < string.length)
      result += f.write_nonblock(string)
    end
  rescue Errno::EAGAIN, Errno::EINTR
    IO.select(nil, [f])
    retry
  end

  def read_until_end(f)
    contents = ""
    while true
      contents << f.read_nonblock(10_000)
    end
  rescue Errno::EAGAIN, Errno::EINTR
    IO.select([f])
    retry
  rescue EOFError
    contents
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
scout_rails-0.0.5.pre lib/scout_rails/layaway_file.rb
scout_rails-0.0.4 lib/scout_rails/layaway_file.rb
scout_rails-0.0.4.pre lib/scout_rails/layaway_file.rb
scout_rails-0.0.3.pre lib/scout_rails/layaway_file.rb
scout_rails-0.0.2 lib/scout_rails/layaway_file.rb
scout_rails-0.0.1 lib/scout_rails/layaway_file.rb