Module: INatChannel::Lock

Defined in:
lib/inat-channel/lock.rb

Class Method Summary collapse

Class Method Details

.acquire!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/inat-channel/lock.rb', line 14

def acquire!
  file = IC::CONFIG.dig(:lock_file, :path)
  FileUtils.mkdir_p File.dirname(file)

  if File.exist?(file)
    data = load_data file
    if stale?(data)
      IC::logger.info "Remove stale lock: #{file}"
      File.delete file
    else
      raise "Another instance is already running (PID: #{data[:pid]})"
    end
  end

  data = {
    pid: Process.pid,
    started_at: Time.now.utc.iso8601
  }
  File.write file, JSON.pretty_generate(data)
  IC::logger.info "Lock acquired: #{file}"
end

.release!Object



36
37
38
39
40
41
42
# File 'lib/inat-channel/lock.rb', line 36

def release!
  file = IC::CONFIG.dig(:lock_file, :path)
  return nil unless File.exist?(file)

  File.delete file
  IC::logger.info "Lock release: #{file}"
end