Class: TestRunner::GcRoot
- Inherits:
-
Object
- Object
- TestRunner::GcRoot
- Defined in:
- lib/test-runner/gc_root.rb
Overview
Invocation-owned indirect GC root, with flock-based stale-root recovery.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root_directory ⇒ Object
readonly
Returns the value of attribute root_directory.
Instance Method Summary collapse
- #cleanup_stale_roots ⇒ Object protected
-
#initialize(root_directory:) ⇒ GcRoot
constructor
A new instance of GcRoot.
- #open ⇒ Object
- #registry_lock_path ⇒ Object protected
- #remove_gc_root ⇒ Object protected
- #reserve_gc_root ⇒ Object protected
- #unlink_gc_root(root_path) ⇒ Object protected
- #with_registry_lock ⇒ Object protected
Constructor Details
#initialize(root_directory:) ⇒ GcRoot
Returns a new instance of GcRoot.
10 11 12 |
# File 'lib/test-runner/gc_root.rb', line 10 def initialize(root_directory:) @root_directory = File.(root_directory) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/test-runner/gc_root.rb', line 8 def path @path end |
#root_directory ⇒ Object (readonly)
Returns the value of attribute root_directory.
8 9 10 |
# File 'lib/test-runner/gc_root.rb', line 8 def root_directory @root_directory end |
Instance Method Details
#cleanup_stale_roots ⇒ Object (protected)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/test-runner/gc_root.rb', line 68 def cleanup_stale_roots Dir.glob(File.join(root_directory, '*.lock')).each do |lock_path| File.open(lock_path, File::RDWR) do |lock_file| next unless lock_file.flock(File::LOCK_EX | File::LOCK_NB) unlink_gc_root(lock_path.sub(/\.lock\z/, '.root')) File.unlink(lock_path) end rescue Errno::ENOENT next end Dir.glob(File.join(root_directory, '*.root')).each do |root_path| lock_path = root_path.sub(/\.root\z/, '.lock') unlink_gc_root(root_path) unless File.exist?(lock_path) end end |
#open ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/test-runner/gc_root.rb', line 14 def open FileUtils.mkdir_p(root_directory, mode: 0o700) reserve_gc_root yield(path) ensure remove_gc_root end |
#registry_lock_path ⇒ Object (protected)
104 105 106 |
# File 'lib/test-runner/gc_root.rb', line 104 def registry_lock_path "#{root_directory}.lock" end |
#remove_gc_root ⇒ Object (protected)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/test-runner/gc_root.rb', line 49 def remove_gc_root return if @lock_path.nil? && @path.nil? with_registry_lock do unlink_gc_root(@path) @lock_file&.flock(File::LOCK_UN) @lock_file&.close @lock_file = nil File.unlink(@lock_path) if @lock_path && File.file?(@lock_path) end rescue Errno::ENOENT nil ensure @lock_path = nil @path = nil end |
#reserve_gc_root ⇒ Object (protected)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/test-runner/gc_root.rb', line 24 def reserve_gc_root with_registry_lock do cleanup_stale_roots loop do token = "#{Process.pid}-#{SecureRandom.hex(8)}" @lock_path = File.join(root_directory, "#{token}.lock") @path = File.join(root_directory, "#{token}.root") begin @lock_file = File.open( @lock_path, File::RDWR | File::CREAT | File::EXCL, 0o600 ) break rescue Errno::EEXIST next end end @lock_file.flock(File::LOCK_EX) end end |
#unlink_gc_root(root_path) ⇒ Object (protected)
95 96 97 98 99 100 101 102 |
# File 'lib/test-runner/gc_root.rb', line 95 def unlink_gc_root(root_path) return if root_path.nil? return unless File.file?(root_path) || File.symlink?(root_path) File.unlink(root_path) rescue Errno::ENOENT nil end |
#with_registry_lock ⇒ Object (protected)
86 87 88 89 90 91 92 93 |
# File 'lib/test-runner/gc_root.rb', line 86 def with_registry_lock File.open(registry_lock_path, File::RDWR | File::CREAT, 0o600) do |lock_file| lock_file.flock(File::LOCK_EX) yield ensure lock_file.flock(File::LOCK_UN) end end |