Class: ScoutApm::LayawayFile
- Inherits:
-
Object
- Object
- ScoutApm::LayawayFile
- Defined in:
- lib/scout_apm/layaway_file.rb
Instance Method Summary collapse
- #dump(object) ⇒ Object
- #get_data(f) ⇒ Object
- #load(dump) ⇒ Object
- #path ⇒ Object
- #read_and_write ⇒ Object
- #read_until_end(f) ⇒ Object
- #write(f, string) ⇒ Object
Instance Method Details
#dump(object) ⇒ Object
33 34 35 |
# File 'lib/scout_apm/layaway_file.rb', line 33 def dump(object) Marshal.dump(object) end |
#get_data(f) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/scout_apm/layaway_file.rb', line 74 def get_data(f) data = read_until_end(f) result = load(data) f.truncate(0) result end |
#load(dump) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/scout_apm/layaway_file.rb', line 37 def load(dump) if dump.size == 0 ScoutApm::Agent.instance.logger.debug("No data in layaway file.") return nil end Marshal.load(dump) rescue NameError, ArgumentError, TypeError => e ScoutApm::Agent.instance.logger.info("Unable to load data from Layaway file, resetting.") ScoutApm::Agent.instance.logger.debug("#{e.}, #{e.backtrace.join("\n\t")}") nil end |
#path ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/scout_apm/layaway_file.rb', line 4 def path return @path if @path candidates = [ ScoutApm::Agent.instance.config.value("data_file"), "#{ScoutApm::Agent.instance.default_log_path}/scout_apm.db", "#{ScoutApm::Agent.instance.environment.root}/tmp/scout_apm.db" ] candidates.each do |candidate| next if candidate.nil? begin ScoutApm::Agent.instance.logger.debug("Checking Layaway File Location: #{candidate}") File.open(candidate, "w") { |f| } # Open & Close to check that we can # No exception, it is valid ScoutApm::Agent.instance.logger.info("Layaway File location found: #{candidate}") @path = candidate return @path rescue Exception ScoutApm::Agent.instance.logger.debug("Couldn't open layaway file for test write at #{candidate}") end end ScoutApm::Agent.instance.logger.error("No valid layaway file found, please set a location in the configuration key `data_file`") nil end |
#read_and_write ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/scout_apm/layaway_file.rb', line 49 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 ScoutApm::Agent.instance.logger.error("Unable to access the layaway file [#{e.class} - #{e.}]. " + "The user running the app must have read & write access. " + "Change the path by setting the `data_file` key in scout_apm.yml" ) ScoutApm::Agent.instance.logger.debug(e.backtrace.join("\n\t")) # ensure the in-memory metric hash is cleared so data doesn't continue to accumulate. # ScoutApm::Agent.instance.store.metric_hash = {} end |
#read_until_end(f) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/scout_apm/layaway_file.rb', line 91 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 |
#write(f, string) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/scout_apm/layaway_file.rb', line 81 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 |