class Object

Public Instance Methods

with_lock(lock_file) { || ... } click to toggle source
# File lib/generators/lab/install/templates/start_worker.rb, line 9
def with_lock(lock_file)
  File.open("log/#{lock_file}", File::RDWR | File::CREAT, 0o644) do |file|
    unless file.flock(File::LOCK_EX | File::LOCK_NB)
      Rails.logger.warn("Failed to start new process due to lock: #{lock_file}")
      exit 2
    end

    file.rewind
    file.puts("Process ##{Process.pid} started at #{Time.now}")

    yield
  end
end