Class: ScoutApm::LayawayFile

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/layaway_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, path) ⇒ LayawayFile

Returns a new instance of LayawayFile.



7
8
9
10
# File 'lib/scout_apm/layaway_file.rb', line 7

def initialize(context, path)
  @path = path
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/scout_apm/layaway_file.rb', line 5

def context
  @context
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/scout_apm/layaway_file.rb', line 4

def path
  @path
end

Instance Method Details

#deserialize(data) ⇒ Object



35
36
37
# File 'lib/scout_apm/layaway_file.rb', line 35

def deserialize(data)
  Marshal.load(data)
end

#loadObject



16
17
18
19
20
21
22
23
24
# File 'lib/scout_apm/layaway_file.rb', line 16

def load
  data = File.open(path, "r") { |f| read_raw(f) }
  deserialize(data)
rescue NameError, ArgumentError, TypeError => e
  # Marshal error
  logger.info("LayawayFile: Unable to load data")
  logger.debug("#{e.message}, #{e.backtrace.join("\n\t")}")
  nil
end

#loggerObject



12
13
14
# File 'lib/scout_apm/layaway_file.rb', line 12

def logger
  context.logger
end

#read_raw(f) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/scout_apm/layaway_file.rb', line 39

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

#serialize(data) ⇒ Object



31
32
33
# File 'lib/scout_apm/layaway_file.rb', line 31

def serialize(data)
  Marshal.dump(data)
end

#write(data) ⇒ Object



26
27
28
29
# File 'lib/scout_apm/layaway_file.rb', line 26

def write(data)
  serialized_data = serialize(data)
  File.open(path, "w") { |f| write_raw(f, serialized_data) }
end

#write_raw(f, data) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/scout_apm/layaway_file.rb', line 51

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