require "redis-lock"

module LocalLinksManager
  class DistributedLock
    attr_accessor :lock_name, :redis_lock

    APP = "local-links-manager".freeze
    LIFETIME = (60 * 60) # seconds

    def initialize(lock_name)
      @lock_name = lock_name
      @redis_lock = Redis::Lock.new(Redis.current, "#{APP}:#{lock_name}", owner: APP, life: LIFETIME)
    end

    def lock(lock_obtained:, lock_not_obtained:)
      redis_lock.lock
      Rails.logger.debug("Successfully got a lock. Running...")
      lock_obtained.call
    rescue Redis::Lock::LockNotAcquired => e
      Rails.logger.debug("Failed to get lock for #{lock_name} (#{e.message}). Another process probably got there first.")
      lock_not_obtained.call
    end

    def unlock
      redis_lock.unlock
      Rails.logger.debug("Successfully unlocked #{lock_name}")
    rescue StandardError => e
      Rails.logger.error("Failed to unlock #{lock_name}\n#{e.message}")
    end

    delegate :locked?, to: :redis_lock
  end
end

require "redis-lock"

module LocalLinksManager
  class DistributedLock
    attr_accessor :lock_name, :redis_lock

    APP = "local-links-manager".freeze
    LIFETIME = (60 * 60) # seconds

    def initialize(lock_name)
      @lock_name = lock_name
      @redis_lock = Redis::Lock.new(Redis.new, "#{APP}:#{lock_name}", owner: APP, life: LIFETIME)
    end

    def lock(lock_obtained:, lock_not_obtained:)
      redis_lock.lock
      Rails.logger.debug("Successfully got a lock. Running...")
      lock_obtained.call
    rescue Redis::Lock::LockNotAcquired => e
      Rails.logger.debug("Failed to get lock for #{lock_name} (#{e.message}). Another process probably got there first.")
      lock_not_obtained.call
    end

    def unlock
      redis_lock.unlock
      Rails.logger.debug("Successfully unlocked #{lock_name}")
    rescue StandardError => e
      Rails.logger.error("Failed to unlock #{lock_name}\n#{e.message}")
    end

    delegate :locked?, to: :redis_lock
  end
end

Use Redis.new rather than Redis.current
Use Redis.new rather than Redis.current

From Redis changelog from [5.0][1]:

‘Removed Redis.current. You shouldn't assume there is a single global
Redis connection, use a connection pool instead, and libaries using
Redis should accept a Redis instance (or connection pool) as a config.
E.g. MyLibrary.redis = Redis.new(…).’

[Redis.current][2] just calls Redis.new so should just be able to swap
methods.

[1]: https://github.com/alphagov/local-links-manager/pull/956
[2]: https://www.rubydoc.info/github/redis/redis-rb/Redis.current
