Class: NFAgent::Payload

Inherits:
Struct
  • Object
show all
Defined in:
lib/nfagent/payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Payload

Returns a new instance of Payload.

Yields:

  • (_self)

Yield Parameters:



4
5
6
# File 'lib/nfagent/payload.rb', line 4

def initialize
  yield self
end

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum

Returns:

  • (Object)

    the current value of checksum



3
4
5
# File 'lib/nfagent/payload.rb', line 3

def checksum
  @checksum
end

#chunk_expiredObject

Returns the value of attribute chunk_expired

Returns:

  • (Object)

    the current value of chunk_expired



3
4
5
# File 'lib/nfagent/payload.rb', line 3

def chunk_expired
  @chunk_expired
end

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



3
4
5
# File 'lib/nfagent/payload.rb', line 3

def data
  @data
end

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



3
4
5
# File 'lib/nfagent/payload.rb', line 3

def filename
  @filename
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



3
4
5
# File 'lib/nfagent/payload.rb', line 3

def key
  @key
end

#line_countObject

Returns the value of attribute line_count

Returns:

  • (Object)

    the current value of 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

#attemptObject



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

#lockObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/nfagent/payload.rb', line 50

def locked?
  filename && File.exists?(lockfile)
end

#sizeObject



21
22
23
# File 'lib/nfagent/payload.rb', line 21

def size
  (self.data || "").size + 1
end

#to_hashObject



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_laterObject



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