Class: Archsight::Import::SharedFileWriter
- Inherits:
-
Object
- Object
- Archsight::Import::SharedFileWriter
- 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.
Instance Method Summary collapse
-
#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.
-
#close_all ⇒ Object
Close all files - sorts and writes buffered content.
-
#initialize ⇒ SharedFileWriter
constructor
A new instance of SharedFileWriter.
Constructor Details
#initialize ⇒ SharedFileWriter
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
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_all ⇒ Object
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 |