Sha256: a28826a0d13eb63478d13bf9b3d6c8305de8b30826370f3db1f3eb6e5845c29d

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'pathname'
require 'digest/sha1'
require 'concurrent/map'
require 'sidekiq_unique_jobs/scripts/acquire_lock'
require 'sidekiq_unique_jobs/scripts/release_lock'

module SidekiqUniqueJobs
  ScriptError         = Class.new(StandardError)
  UniqueKeyMissing    = Class.new(ArgumentError)
  JidMissing          = Class.new(ArgumentError)
  MaxLockTimeMissing  = Class.new(ArgumentError)
  UnexpectedValue     = Class.new(StandardError)

  module Scripts
    LUA_PATHNAME ||= Pathname.new(__FILE__).dirname.join('../../redis').freeze
    SOURCE_FILES ||= Dir[LUA_PATHNAME.join('**/*.lua')].compact.freeze
    DEFINED_METHODS ||= [].freeze
    SCRIPT_SHAS ||= Concurrent::Map.new

    module_function

    extend SingleForwardable
    def_delegators :SidekiqUniqueJobs, :connection, :logger

    def call(file_name, redis_pool, options = {})
      internal_call(file_name, redis_pool, options)
    rescue Redis::CommandError => ex
      handle_error(ex, file_name, redis_pool, options)
    end

    def internal_call(file_name, redis_pool, options = {})
      connection(redis_pool) do |redis|
        SCRIPT_SHAS[file_name] = redis.script(:load, script_source(file_name)) if SCRIPT_SHAS[file_name].nil?
        redis.evalsha(SCRIPT_SHAS[file_name], options)
      end
    end

    def handle_error(ex, file_name, redis_pool, options = {})
      if ex.message == 'NOSCRIPT No matching script. Please use EVAL.' # rubocop:disable Style/GuardClause
        SCRIPT_SHAS.delete(file_name)
        call(file_name, redis_pool, options)
      else
        raise ScriptError, "Problem compiling #{file_name}. Invalid LUA syntax?"
      end
    end

    def script_source(file_name)
      script_path(file_name).read
    end

    def script_path(file_name)
      LUA_PATHNAME.join("#{file_name}.lua")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-5.0.4 lib/sidekiq_unique_jobs/scripts.rb