Class: NFAgent::Payload
Instance Attribute Summary collapse
-
#checksum ⇒ Object
Returns the value of attribute checksum.
-
#chunk_expired ⇒ Object
Returns the value of attribute chunk_expired.
-
#data ⇒ Object
Returns the value of attribute data.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#key ⇒ Object
Returns the value of attribute key.
-
#line_count ⇒ Object
Returns the value of attribute line_count.
Class Method Summary collapse
Instance Method Summary collapse
- #attempt ⇒ Object
- #attempt=(value) ⇒ Object
- #destroy! ⇒ Object
- #increment_attempt! ⇒ Object
-
#initialize {|_self| ... } ⇒ Payload
constructor
A new instance of Payload.
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #size ⇒ Object
- #to_hash ⇒ Object
- #try_again_later ⇒ Object
- #write_to_disk(directory) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Payload
Returns a new instance of Payload.
4 5 6 |
# File 'lib/nfagent/payload.rb', line 4 def initialize yield self end |
Instance Attribute Details
#checksum ⇒ Object
Returns the value of attribute checksum
3 4 5 |
# File 'lib/nfagent/payload.rb', line 3 def checksum @checksum end |
#chunk_expired ⇒ Object
Returns the value of attribute chunk_expired
3 4 5 |
# File 'lib/nfagent/payload.rb', line 3 def chunk_expired @chunk_expired end |
#data ⇒ Object
Returns the value of attribute data
3 4 5 |
# File 'lib/nfagent/payload.rb', line 3 def data @data end |
#filename ⇒ Object
Returns the value of attribute filename
3 4 5 |
# File 'lib/nfagent/payload.rb', line 3 def filename @filename end |
#key ⇒ Object
Returns the value of attribute key
3 4 5 |
# File 'lib/nfagent/payload.rb', line 3 def key @key end |
#line_count ⇒ Object
Returns the value of attribute line_count
3 4 5 |
# File 'lib/nfagent/payload.rb', line 3 def line_count @line_count end |
Class Method Details
.read_from_file(filename, dir = Config.dump_dir) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/nfagent/payload.rb', line 54 def self.read_from_file(filename, dir = Config.dump_dir) # Ensure the file is only relative filename = File.basename(filename) self.new do |payload| payload.filename = filename payload.checksum, payload.attempt, payload.key = filename.split("-") payload.data = "" ref = File.join(dir, filename) File.open(ref, "r") do |file| payload.data << file.read end end end |
Instance Method Details
#attempt ⇒ Object
8 9 10 |
# File 'lib/nfagent/payload.rb', line 8 def attempt @attempt || 0 end |
#attempt=(value) ⇒ Object
12 13 14 |
# File 'lib/nfagent/payload.rb', line 12 def attempt=(value) @attempt = value.to_i end |
#destroy! ⇒ Object
68 69 70 |
# File 'lib/nfagent/payload.rb', line 68 def destroy! FileUtils.rm_f(File.join(Config.dump_dir, self.filename)) if self.filename end |
#increment_attempt! ⇒ Object
16 17 18 19 |
# File 'lib/nfagent/payload.rb', line 16 def increment_attempt! @attempt ||= 1 @attempt += 1 end |
#lock ⇒ Object
43 44 45 46 47 48 |
# File 'lib/nfagent/payload.rb', line 43 def lock return if locked? FileUtils.touch(lockfile) if filename yield FileUtils.rm_f(lockfile) if filename end |
#locked? ⇒ Boolean
50 51 52 |
# File 'lib/nfagent/payload.rb', line 50 def locked? filename && File.exists?(lockfile) end |
#size ⇒ Object
21 22 23 |
# File 'lib/nfagent/payload.rb', line 21 def size (self.data || "").size + 1 end |
#to_hash ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nfagent/payload.rb', line 25 def to_hash ret = { "payload" => self.data, "checksum" => self.checksum, "line_count" => self.line_count, "chunk_expired" => self.chunk_expired } ret["key"] = self.key unless self.key.blank? ret end |
#try_again_later ⇒ Object
72 73 74 75 76 |
# File 'lib/nfagent/payload.rb', line 72 def try_again_later # TODO: Move the file to a new name with a later timetamp new_filename = [ self.checksum, self.attempt, self.key ].compact.join("-") FileUtils.mv(File.join(Config.dump_dir, self.filename), File.join(Config.dump_dir, new_filename)) end |
#write_to_disk(directory) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/nfagent/payload.rb', line 36 def write_to_disk(directory) filename = [ self.checksum, self.attempt, self.key ].compact.join("-") File.open(File.join(directory, filename), "w") do |file| file << self.data end end |