Module: RFlow::Components::File::OutputToDisk

Included in:
OutputRawToFiles
Defined in:
lib/rflow/components/file/output_to_disk.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  'directory_path'  => '/tmp',
  'file_name_prefix' => 'output.',
  'file_name_suffix' => '.out',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/rflow/components/file/output_to_disk.rb', line 11

def config
  @config
end

#directory_pathObject

Returns the value of attribute directory_path.



11
12
13
# File 'lib/rflow/components/file/output_to_disk.rb', line 11

def directory_path
  @directory_path
end

#file_name_prefixObject

Returns the value of attribute file_name_prefix.



11
12
13
# File 'lib/rflow/components/file/output_to_disk.rb', line 11

def file_name_prefix
  @file_name_prefix
end

#file_name_suffixObject

Returns the value of attribute file_name_suffix.



11
12
13
# File 'lib/rflow/components/file/output_to_disk.rb', line 11

def file_name_suffix
  @file_name_suffix
end

Instance Method Details

#configure!(config) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rflow/components/file/output_to_disk.rb', line 13

def configure!(config)
  @config = DEFAULT_CONFIG.merge config
  @directory_path  = ::File.expand_path(@config['directory_path'])
  @file_name_prefix = @config['file_name_prefix']
  @file_name_suffix = @config['file_name_suffix']

  unless ::File.directory?(@directory_path)
    raise ArgumentError, "Invalid directory '#{@directory_path}'"
  end

  unless ::File.writable?(@directory_path)
    raise ArgumentError, "Unable to read from directory '#{@directory_path}'"
  end

  # TODO: more error checking of input config
end

#write_to_file(properties) ⇒ Object

if passed properties, will look for data_uuid property and use as suffix preamble



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rflow/components/file/output_to_disk.rb', line 31

def write_to_file(properties)
  properties ||= {}
  @output_file_entropy = 1
  begin
    final_output_file_name = output_file_name(properties)

    temp_output_file_path = ::File.join(directory_path, ".#{final_output_file_name}")
    final_output_file_path = ::File.join(directory_path, "#{final_output_file_name}")

    RFlow.logger.debug { "#{self.class}: Outputting message to #{final_output_file_path} (via #{temp_output_file_path})" }

    ::File.open(temp_output_file_path, ::File::CREAT|::File::EXCL|::File::RDWR, 0644, :external_encoding => 'BINARY') do |file|
      file.flock(::File::LOCK_EX)
      bytes_written = yield file

      file.flush
      raise IOError, "file size of '#{::File.size(temp_output_file_path)}' does not match expected size of '#{bytes_written}'" unless ::File.size(temp_output_file_path) == bytes_written
    end
    ::File.rename(temp_output_file_path, final_output_file_path)
    final_output_file_path
  rescue Errno::EEXIST => e
    RFlow.logger.debug { "#{self.class}: File #{temp_output_file_path} exists, increasing entropy" }
    @output_file_entropy += 1
    retry
  rescue StandardError => e
    RFlow.logger.error { "#{self.class} encountered #{e.message} when creating #{temp_output_file_path}" }
    begin
      ::File.delete(temp_output_file_path)
    rescue => f
      RFlow.logger.debug {"#{self.class} encountered #{f.message} on cleanup of #{temp_output_file_path}" }
    end
    raise e
  end
end