Sha256: c7730ab628e5feb2c204d762ac562bc5c55e8a4d7757dbb5c715607302d50d7b

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'pathname'
require 'digest/sha1'

module SidekiqUniqueJobs
  ScriptError = 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

    module_function

    extend SingleForwardable
    def_delegator :SidekiqUniqueJobs, :connection

    def script_shas
      @script_shas ||= {}
    end

    def logger
      Sidekiq.logger
    end

    def call(file_name, redis_pool, options = {})
      connection(redis_pool) do |redis|
        script_shas[file_name] ||= redis.script(:load, script_source(file_name))
        redis.evalsha(script_shas[file_name], options)
      end
    rescue Redis::CommandError => ex
      if ex.message == 'NOSCRIPT No matching script. Please use EVAL.'
        script_shas[file_name] = nil
        call(file_name, redis_pool, options)
      else
        raise ScriptError,
              "#{file_name}.lua\n\n#{ex.message}\n\n#{script_source(file_name)}" \
              "#{ex.backtrace.join("\n")}"
      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-4.0.18 lib/sidekiq_unique_jobs/scripts.rb