Class: Archsight::Import::SharedFileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/archsight/import/shared_file_writer.rb

Overview

Thread-safe file writer for concurrent import handlers

Manages shared output files that multiple handlers can write to. Content is buffered in memory and sorted by key when close_all is called.

Examples:

writer = SharedFileWriter.new
writer.append_yaml("/path/to/output.yaml", yaml_content, sort_key: "Repo:name")
writer.close_all  # Sorts and writes buffered content

Instance Method Summary collapse

Constructor Details

#initializeSharedFileWriter

Returns a new instance of SharedFileWriter.



15
16
17
18
# File 'lib/archsight/import/shared_file_writer.rb', line 15

def initialize
  @mutex = Mutex.new
  @files = {}
end

Instance Method Details

#append_yaml(path, content, sort_key: nil) ⇒ Object

Append YAML content to a file (thread-safe, buffered) Content is sorted by sort_key when close_all is called

Parameters:

  • path (String)

    Full path to the output file

  • content (String)

    YAML content to append

  • sort_key (String, nil) (defaults to: nil)

    Key for sorting (nil keys go last)



26
27
28
29
30
31
32
33
34
35
# File 'lib/archsight/import/shared_file_writer.rb', line 26

def append_yaml(path, content, sort_key: nil)
  @mutex.synchronize do
    @files[path] ||= { entries: [], lock: Mutex.new }
  end

  entry = @files[path]
  entry[:lock].synchronize do
    entry[:entries] << { key: sort_key, content: content }
  end
end

#close_allObject

Close all files - sorts and writes buffered content



38
39
40
41
42
43
44
45
# File 'lib/archsight/import/shared_file_writer.rb', line 38

def close_all
  @mutex.synchronize do
    @files.each do |path, entry|
      write_sorted_file(path, entry[:entries])
    end
    @files.clear
  end
end