module SidekiqUniqueJobs::Script::Caller

Module Caller provides the convenience method call_script

@author Mikael Henriksson <mikael@mhenrixon.com>

Public Instance Methods

call_script(file_name, *args) { |result| ... } click to toggle source

Convenience method to reduce typing,

calls redis lua scripts.

@overload call_script(file_name, keys, argv, conn)

Call script without options hash
@param [Symbol] file_name the name of the file
@param [Array<String>] keys to pass to the the script
@param [Array<String>] argv arguments to pass to the script
@param [Redis] conn a redis connection

@overload call_script(file_name, conn, keys:, argv:)

Call script with options hash
@param [Symbol] file_name the name of the file
@param [Redis] conn a redis connection
@param [Hash] options arguments to pass to the script file
@option options [Array] :keys to pass to the script
@option options [Array] :argv arguments to pass to the script

@return [true,false,String,Integer,Float,nil] returns the return value of the lua script

# File lib/sidekiq_unique_jobs/script/caller.rb, line 41
def call_script(file_name, *args)
  conn, keys, argv = extract_args(*args)
  return do_call(file_name, conn, keys, argv) if conn

  pool = defined?(redis_pool) ? redis_pool : nil

  redis(pool) do |new_conn|
    result = do_call(file_name, new_conn, keys, argv)
    yield result if block_given?
    result
  end
end
debug_lua() click to toggle source

@see SidekiqUniqueJobs::Config#debug_lua

# File lib/sidekiq_unique_jobs/script/caller.rb, line 108
def debug_lua
  SidekiqUniqueJobs.config.debug_lua
end
do_call(file_name, conn, keys, argv) click to toggle source

Only used to reduce a little bit of duplication @see call_script

# File lib/sidekiq_unique_jobs/script/caller.rb, line 56
def do_call(file_name, conn, keys, argv)
  argv = argv.dup.concat([
                           now_f,
                           debug_lua,
                           max_history,
                           file_name,
                           redis_version,
                         ])
  Script.execute(file_name, conn, keys: keys, argv: argv)
end
extract_args(*args) click to toggle source

Utility method to allow both symbol keys and arguments

@overload call_script(file_name, keys, argv, conn)

Call script without options hash
@param [Symbol] file_name the name of the file
@param [Array<String>] keys to pass to the the script
@param [Array<String>] argv arguments to pass to the script
@param [Redis] conn a redis connection

@overload call_script(file_name, conn, keys:, argv:)

Call script with options hash
@param [Symbol] file_name the name of the file
@param [Redis] conn a redis connection
@param [Hash] options arguments to pass to the script file
@option options [Array] :keys to pass to the script
@option options [Array] :argv arguments to pass to the script

@return [Array<Redis, Array, Array>] <description>

# File lib/sidekiq_unique_jobs/script/caller.rb, line 86
def extract_args(*args)
  options = args.extract_options!
  if options.length.positive?
    [args.pop, options.fetch(:keys) { [] }, options.fetch(:argv) { [] }]
  else
    keys, argv = args.shift(2)
    keys ||= []
    argv ||= []
    [args.pop, keys, argv]
  end
end
max_history() click to toggle source

@see SidekiqUniqueJobs::Config#max_history

# File lib/sidekiq_unique_jobs/script/caller.rb, line 115
def max_history
  SidekiqUniqueJobs.config.max_history
end
now_f() click to toggle source

@see SidekiqUniqueJobs#now_f

# File lib/sidekiq_unique_jobs/script/caller.rb, line 101
def now_f
  SidekiqUniqueJobs.now_f
end
redis_version() click to toggle source

@see SidekiqUniqueJobs::Config#max_history

# File lib/sidekiq_unique_jobs/script/caller.rb, line 122
def redis_version
  SidekiqUniqueJobs.config.redis_version
end