Module: SidekiqUniqueJobs::Script::Caller

Included in:
Locksmith, OnConflict::Strategy, Orphans::Reaper, Redis::Entity
Defined in:
lib/sidekiq_unique_jobs/script/caller.rb

Overview

Module Caller provides the convenience method #call_script

Author:

Class Method Summary collapse

Class Method Details

.call_script(file_name, keys, argv, conn) ⇒ true, ... .call_script(file_name, conn, keys: , argv: ) ⇒ true, ...

Convenience method to reduce typing,

calls redis lua scripts.

Overloads:

  • .call_script(file_name, keys, argv, conn) ⇒ true, ...

    Call script without options hash

    Parameters:

    • file_name (Symbol)

      the name of the file

    • keys (Array<String>)

      to pass to the the script

    • argv (Array<String>)

      arguments to pass to the script

    • conn (Redis)

      a redis connection

  • .call_script(file_name, conn, keys: , argv: ) ⇒ true, ...

    Call script with options hash

    Parameters:

    • file_name (Symbol)

      the name of the file

    • conn (Redis)

      a redis connection

    • options (Hash)

      arguments to pass to the script file

Returns:

  • (true, false, String, Integer, Float, nil)

    returns the return value of the lua script



41
42
43
44
45
46
47
48
49
50
51
52
# 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_luaObject

See Also:

  • Config#debug_lua


103
104
105
# File 'lib/sidekiq_unique_jobs/script/caller.rb', line 103

def debug_lua
  SidekiqUniqueJobs.config.debug_lua
end

.do_call(file_name, conn, keys, argv) ⇒ Object

Only used to reduce a little bit of duplication

See Also:



56
57
58
59
60
# File 'lib/sidekiq_unique_jobs/script/caller.rb', line 56

def do_call(file_name, conn, keys, argv)
  argv = argv.dup.push(now_f, debug_lua, max_history, file_name, redis_version)

  SidekiqUniqueJobs::Script.execute(file_name, conn, keys: keys, argv: normalize_argv(argv))
end

.call_script(file_name, keys, argv, conn) ⇒ Array<Redis, Array, Array> .call_script(file_name, conn, keys: , argv: ) ⇒ Array<Redis, Array, Array>

Utility method to allow both symbol keys and arguments

Overloads:

  • .call_script(file_name, keys, argv, conn) ⇒ Array<Redis, Array, Array>

    Call script without options hash

    Parameters:

    • file_name (Symbol)

      the name of the file

    • keys (Array<String>)

      to pass to the the script

    • argv (Array<String>)

      arguments to pass to the script

    • conn (Redis)

      a redis connection

  • .call_script(file_name, conn, keys: , argv: ) ⇒ Array<Redis, Array, Array>

    Call script with options hash

    Parameters:

    • file_name (Symbol)

      the name of the file

    • conn (Redis)

      a redis connection

    • options (Hash)

      arguments to pass to the script file

Returns:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sidekiq_unique_jobs/script/caller.rb', line 81

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_historyObject

See Also:

  • Config#max_history


110
111
112
# File 'lib/sidekiq_unique_jobs/script/caller.rb', line 110

def max_history
  SidekiqUniqueJobs.config.max_history
end

.normalize_argv(argv) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/sidekiq_unique_jobs/script/caller.rb', line 121

def normalize_argv(argv)
  argv.each_with_index do |item, index|
    case item
    when FalseClass, NilClass
      argv[index] = 0
    when TrueClass
      argv[index] = 1
    end
  end
end

.now_fObject

See Also:

  • SidekiqUniqueJobs#now_f


96
97
98
# File 'lib/sidekiq_unique_jobs/script/caller.rb', line 96

def now_f
  SidekiqUniqueJobs.now_f
end

.redis_versionObject

See Also:

  • Config#max_history


117
118
119
# File 'lib/sidekiq_unique_jobs/script/caller.rb', line 117

def redis_version
  SidekiqUniqueJobs.config.redis_version
end