require "redis"
require "redis-lock"

module Contacts
  class DistributedLock
    LIFETIME = (5 * 60) # seconds. The lock expires automatically after this time

    def initialize(lock_name)
      @lock_name = lock_name
    end

    def lock
      redis.lock("contacts-admin:#{Rails.env}:#{@lock_name}", life: LIFETIME) do
        Rails.logger.debug("Successfully got a lock. Running...")
        yield
      end
    rescue Redis::Lock::LockNotAcquired => e
      Rails.logger.debug("Failed to get lock for #{@lock_name} (#{e.message}). Another process probably got there first.")
    end

  private

    def redis
      @redis ||= begin
        redis_config = {
          host: ENV["REDIS_HOST"] || "127.0.0.1",
          port: ENV["REDIS_PORT"] || 6379,
        }

        Redis.new(redis_config)
      end
    end
  end
end

require "redis"
require "redis-lock"

module Contacts
  class DistributedLock
    LIFETIME = (5 * 60) # seconds. The lock expires automatically after this time

    def initialize(lock_name)
      @lock_name = lock_name
    end

    def lock
      Redis.current.lock("contacts-admin:#{Rails.env}:#{@lock_name}", life: LIFETIME) do
        Rails.logger.debug("Successfully got a lock. Running...")
        yield
      end
    rescue Redis::Lock::LockNotAcquired => e
      Rails.logger.debug("Failed to get lock for #{@lock_name} (#{e.message}). Another process probably got there first.")
    end
  end
end

Remove need for REDIS_HOST and REDIS_PORT env vars
Remove need for REDIS_HOST and REDIS_PORT env vars

This is one of the few GOV.UK projects that uses this old approach to
configuring Redis - whereas most of the other ones use the approach
supported natively by the Redis gem [1] of using REDIS_URL. Removing
this will allow us to remove the need for REDIS_HOST and REDIS_PORT env
vars in our architecture and also allows this code to be simplified.

[1]: https://github.com/redis/redis-rb
