Sha256: e8d01019d615ee3a445c7d21373ec226a36280c1e2df77c9a4054c23fa529d45

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

require 'pathname'
require 'digest/sha1'

module SidekiqUniqueJobs
  ScriptError = Class.new(StandardError)

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

    module_function

    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
      raise ScriptError,
            "#{file_name}.lua\n\n" +
              ex.message + "\n\n" +
              script_source(file_name) +
              ex.backtrace.join("\n")
    end

    def connection(redis_pool, &_block)
      SidekiqUniqueJobs.connection(redis_pool) do |conn|
        yield conn
      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

3 entries across 3 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-4.0.8 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-4.0.7 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-4.0.0 lib/sidekiq_unique_jobs/scripts.rb