Class: Henitai::ReportsDirectoryLock

Inherits:
Object
  • Object
show all
Defined in:
lib/henitai/reports_directory_lock.rb,
sig/henitai.rbs

Overview

Coordinates exclusive access to a reports directory across processes.

Constant Summary collapse

LOCK_FILENAME =

Returns:

  • (String)
".henitai-run.lock"

Instance Method Summary collapse

Constructor Details

#initialize(reports_dir:) ⇒ ReportsDirectoryLock

Returns a new instance of ReportsDirectoryLock.

Parameters:

  • reports_dir: (String)


12
13
14
# File 'lib/henitai/reports_directory_lock.rb', line 12

def initialize(reports_dir:)
  @reports_dir = File.expand_path(reports_dir)
end

Instance Method Details

#acquire(file) ⇒ void

This method returns an undefined value.

Parameters:

  • (File)

Raises:



41
42
43
44
45
# File 'lib/henitai/reports_directory_lock.rb', line 41

def acquire(file)
  return if file.flock(File::LOCK_EX | File::LOCK_NB)

  raise ConcurrentRunError, contention_message(owner_pid(file))
end

#contention_message(pid) ⇒ String

Parameters:

  • (Integer, String)

Returns:

  • (String)


56
57
58
59
60
61
62
63
64
# File 'lib/henitai/reports_directory_lock.rb', line 56

def contention_message(pid)
  message = "another henitai run is active in #{@reports_dir} " \
            "(pid #{pid}); use a separate reports_dir or wait"
  return message unless dead_owner?(pid)

  "#{message}. The recorded owner pid #{pid} is not running — an " \
    "orphaned child may still hold the lock " \
    "(check: lsof #{lock_path})"
end

#lock_pathString

Returns:

  • (String)


37
38
39
# File 'lib/henitai/reports_directory_lock.rb', line 37

def lock_path
  File.join(@reports_dir, LOCK_FILENAME)
end

#owner_pid(file) ⇒ Integer, String

Parameters:

  • (File)

Returns:

  • (Integer, String)


47
48
49
50
51
52
53
54
# File 'lib/henitai/reports_directory_lock.rb', line 47

def owner_pid(file)
  file.rewind
   = JSON.parse(file.read)
  pid = ["pid"] if .is_a?(Hash)
  pid.is_a?(Integer) ? pid : "unknown"
rescue JSON::ParserError, IOError, SystemCallError
  "unknown"
end

#synchronize { ... } ⇒ Object

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/henitai/reports_directory_lock.rb', line 16

def synchronize
  FileUtils.mkdir_p(@reports_dir)
  File.open(lock_path, File::RDWR | File::CREAT, 0o644) do |file|
    acquire(file)
    write_owner(file)
    # Registered so a forked mutant child can close its inherited copy: the
    # flock lives on the shared open file description, so a child that
    # outlives its parent would otherwise keep this lock held. Unregistered
    # inside the File.open block, keeping the registration's lifetime a
    # subset of the handle's.
    InheritedFdRegistry.register(file)
    begin
      yield
    ensure
      InheritedFdRegistry.unregister(file)
    end
  end
end

#write_owner(file) ⇒ Object

Parameters:

  • (File)

Returns:

  • (Object)


70
71
72
73
74
75
# File 'lib/henitai/reports_directory_lock.rb', line 70

def write_owner(file)
  file.rewind
  file.truncate(0)
  file.write(JSON.generate(pid: Process.pid, started_at: Time.now.iso8601))
  file.flush
end