def with_lock(**opts)
      opts = opts.dup
      opts[:retries] ||= Float::INFINITY
      opts[:reload] = opts[:reload] != false

      acquire_lock(opts) if persisted? && (had_lock = !has_lock?)

      begin
        yield
      ensure
        unlock!(opts) if had_lock
      end
    end
def with_lock(**opts)
      opts = opts.dup
      opts[:retries] ||= Float::INFINITY
      opts[:reload] = opts[:reload] != false

      acquire_lock(opts) if persisted? && (had_lock = !has_lock?)

      begin
        yield
      ensure
        unlock!(opts) if had_lock
      end
    end
Executes the provided code once the document has been successfully locked. Otherwise, raises error after the number of retries to lock the document is exhausted or it is reached {ClassMethods#maximum_backoff} limit (depending what comes first).

 @example
   document.with_lock(reload: true, retries: 3) do
     document.quantity = 17
     document.save!
   end

 @param [Hash] opts for the locking mechanism
 @option opts [Fixnum] :retries (INFINITY) If the document is currently locked, the number of times to retry
 @option opts [Boolean] :reload (true) After acquiring the lock, reload the document
 @option opts [Integer] :attempt (0) Increment with each retry (not accepted by the method)
 @option opts [String] :locking_name Generate with each retry (not accepted by the method)