Sha256: 56c822efc22c7ea6c09309a5b16c4ab96f63ecaea21d85e8684243110eefdb6f

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'pathname'
require 'digest/sha1'
require 'concurrent/map'

module SidekiqUniqueJobs
  module Scripts
    LUA_PATHNAME ||= Pathname.new(__FILE__).dirname.join('../../redis').freeze
    SCRIPT_SHAS ||= Concurrent::Map.new

    include SidekiqUniqueJobs::Connection

    module_function

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

    def execute_script(file_name, redis_pool, options = {})
      redis(redis_pool) do |conn|
        sha = script_sha(conn, file_name)
        conn.evalsha(sha, options)
      end
    end

    def script_sha(conn, file_name)
      if (sha = SCRIPT_SHAS.get(file_name))
        return sha
      end

      sha = conn.script(:load, script_source(file_name))
      SCRIPT_SHAS.put(file_name, sha)
      sha
    end

    def handle_error(ex, file_name)
      if ex.message == 'NOSCRIPT No matching script. Please use EVAL.'
        SCRIPT_SHAS.delete(file_name)
        return yield if block_given?
      end

      raise ScriptError, file_name: file_name, source_exception: ex
    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

6 entries across 6 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-6.0.0.rc6 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-6.0.0.rc5 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-6.0.0.rc4 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-6.0.0.rc3 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-6.0.0.rc2 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-6.0.0.rc1 lib/sidekiq_unique_jobs/scripts.rb