def lock
      retries = @max_retries
      while retries > 0
        begin
          @file = File.open(@file_name, File::RDWR | File::CREAT, 0644)
          @file.sync = true

          if @file.flock(File::LOCK_EX | File::LOCK_NB)
            # We have taken the lock. Write the PID into the file and leave it
            # open.
            @file.write($$)
            @file.flush
            @file.fsync
            @file.truncate(@file.pos)
            PEROBS.log.debug "Lock file #{@file_name} has been taken for " +
              "process #{$$}"

            return true
          else
            # We did not manage to take the lock file.
            if @file.mtime <= Time.now - @timeout_secs
              pid = @file.read.to_i
              PEROBS.log.info "Old lock file found for PID #{pid}. " +
                "Removing lock."
              if is_running?(pid)
                send_signal('TERM', pid)
                # Give the process 3 seconds to terminate gracefully.
                sleep 3
                # Then send a SIGKILL to ensure it's gone.
                send_signal('KILL', pid) if is_running?(pid)
              end
              @file.close
              File.delete(@file_name) if File.exist?(@file_name)
            else
              PEROBS.log.debug "Lock file #{@file_name} is taken. Trying " +
                "to get it #{retries} more times."
            end
          end
        rescue => e
          PEROBS.log.error "Cannot take lock file #{@file_name}: #{e.message}"
          return false
        end

        retries -= 1
        sleep(@pause_secs)
      end

      PEROBS.log.info "Failed to get lock file #{@file_name} due to timeout"
      false
    end
def lock
      retries = @max_retries
      while retries > 0
        begin
          @file = File.open(@file_name, File::RDWR | File::CREAT, 0644)
          @file.sync = true

          if @file.flock(File::LOCK_EX | File::LOCK_NB)
            # We have taken the lock. Write the PID into the file and leave it
            # open.
            @file.write($$)
            @file.flush
            @file.fsync
            @file.truncate(@file.pos)
            PEROBS.log.debug "Lock file #{@file_name} has been taken for " +
              "process #{$$}"

            return true
          else
            # We did not manage to take the lock file.
            if @file.mtime <= Time.now - @timeout_secs
              pid = @file.read.to_i
              PEROBS.log.info "Old lock file found for PID #{pid}. " +
                "Removing lock."
              if is_running?(pid)
                send_signal('TERM', pid)
                # Give the process 3 seconds to terminate gracefully.
                sleep 3
                # Then send a SIGKILL to ensure it's gone.
                send_signal('KILL', pid) if is_running?(pid)
              end
              @file.close
              File.delete(@file_name) if File.exist?(@file_name)
            else
              PEROBS.log.debug "Lock file #{@file_name} is taken. Trying " +
                "to get it #{retries} more times."
            end
          end
        rescue => e
          PEROBS.log.error "Cannot take lock file #{@file_name}: #{e.message}"
          return false
        end

        retries -= 1
        sleep(@pause_secs)
      end

      PEROBS.log.info "Failed to get lock file #{@file_name} due to timeout"
      false
    end
Create a new lock for the given file.
 @param file_name [String] file name of the lock file
 @param options [Hash] See case statement
 Attempt to take the lock.
 @return [Boolean] true if lock was taken, false otherwise