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:

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: []) ⇒ Object

Note:

this method is recursive if we need to load a lua script that wasn’t previously loaded.

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

Returns:

  • value from script



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sidekiq_unique_jobs/script/client.rb', line 43

def execute(script_name, conn, keys: [], argv: [])
  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
  handle_error(script_name, conn, ex) do
    execute(script_name, conn, keys: keys, argv: argv)
  end
end