Sha256: 25024de4d50032db12170c7fbcb0703eef8ea5fd0f1967b135850b7710f0cb84

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 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
      raise ScriptError,
            "#{file_name}.lua\n\n" +
            ex.message + "\n\n" +
            script_source(file_name) +
            ex.backtrace.join("\n")
    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.17 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-4.0.16 lib/sidekiq_unique_jobs/scripts.rb
sidekiq-unique-jobs-4.0.15 lib/sidekiq_unique_jobs/scripts.rb