Class: SidekiqUniqueJobs::Script::Client

Inherits:
Object
  • Object
show all
Includes:
Timing
Defined in:
lib/sidekiq_unique_jobs/script/client.rb

Overview

Interface to dealing with .lua files

Author:

Constant Summary collapse

MAX_RETRIES =

Maximum number of retries for script execution errors

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Timing

now, timed

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
# File 'lib/sidekiq_unique_jobs/script/client.rb', line 24

def initialize(config)
  @config  = config
  @logger  = config.logger
  @scripts = Scripts.fetch(config.scripts_path)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/sidekiq_unique_jobs/script/client.rb', line 18

def config
  @config
end

#file_nameString (readonly)

Returns The name of the file to execute.

Returns:

  • (String)

    The name of the file to execute



18
# File 'lib/sidekiq_unique_jobs/script/client.rb', line 18

attr_reader :config

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/sidekiq_unique_jobs/script/client.rb', line 14

def logger
  @logger
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



22
23
24
# File 'lib/sidekiq_unique_jobs/script/client.rb', line 22

def scripts
  @scripts
end

Instance Method Details

#execute(script_name, conn, keys: [], argv: [], retries: MAX_RETRIES) ⇒ Object

Note:

this method is recursive if we need to load a lua script that wasn’t previously loaded. Limited to MAX_RETRIES to prevent stack overflow.

Execute a lua script with the provided script_name

Parameters:

  • script_name (Symbol)

    the name of the script to execute

  • conn (Redis)

    the redis connection to use for execution

  • keys (Array<String>) (defaults to: [])

    script keys

  • argv (Array<Object>) (defaults to: [])

    script arguments

  • retries (Integer) (defaults to: MAX_RETRIES)

    number of retries remaining (internal use)

Returns:

  • value from script



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sidekiq_unique_jobs/script/client.rb', line 49

def execute(script_name, conn, keys: [], argv: [], retries: MAX_RETRIES)
  result, elapsed = timed do
    scripts.execute(script_name, conn, keys: keys, argv: argv)
  end

  logger.debug("Executed #{script_name}.lua in #{elapsed}ms")
  result
rescue ::RedisClient::CommandError => ex
  raise if retries <= 0

  handle_error(script_name, conn, ex) do
    execute(script_name, conn, keys: keys, argv: argv, retries: retries - 1)
  end
end