Class: SidekiqUniqueJobs::Script::Scripts

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_unique_jobs/script/scripts.rb

Overview

Interface to dealing with .lua files

Author:

Constant Summary collapse

SCRIPT_PATHS =

Returns a map with configured script paths.

Returns:

  • (Concurrent::Map)

    a map with configured script paths

Concurrent::Map.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Scripts

Returns a new instance of Scripts.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 36

def initialize(path)
  raise ArgumentError, "path needs to be a Pathname" unless path.is_a?(Pathname)

  @scripts   = Concurrent::Map.new
  @root_path = path
end

Instance Attribute Details

#root_pathObject (readonly)

Returns the value of attribute root_path.



34
35
36
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 34

def root_path
  @root_path
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



29
30
31
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 29

def scripts
  @scripts
end

Class Method Details

.fetch(root_path) ⇒ Scripts

Fetch or create a scripts configuration for path

Uses Concurrent::Map#fetch_or_store for thread-safe lazy initialization

Parameters:

  • root_path (Pathname)

    the path to scripts

Returns:

  • (Scripts)

    a collection of scripts



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

def self.fetch(root_path)
  SCRIPT_PATHS.fetch_or_store(root_path) { new(root_path) }
end

Instance Method Details

#countObject



114
115
116
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 114

def count
  scripts.keys.size
end

#delete(script) ⇒ Script?

Delete a script from the collection

Parameters:

  • script (Script, Symbol, String)

    the script or script name to delete

Returns:

  • (Script, nil)

    the deleted script



78
79
80
81
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 78

def delete(script)
  key = script.is_a?(Script) ? script.name : script.to_sym
  scripts.delete(key)
end

#execute(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 given name

Parameters:

  • 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



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

def execute(name, conn, keys: [], argv: [])
  script = fetch(name, conn)
  conn.evalsha(script.sha, keys, argv)
end

#fetch(name, conn) ⇒ Script

Fetch or load a script by name

Uses Concurrent::Map#fetch_or_store for thread-safe lazy loading

Parameters:

  • name (Symbol, String)

    the script name

  • conn (Redis)

    the redis connection

Returns:

  • (Script)

    the loaded script



53
54
55
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 53

def fetch(name, conn)
  scripts.fetch_or_store(name.to_sym) { load(name, conn) }
end

#kill(conn) ⇒ String

Kill a running Redis script

Parameters:

  • conn (Redis)

    the redis connection

Returns:

  • (String)

    Redis response



90
91
92
93
94
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 90

def kill(conn)
  # Handle both namespaced and non-namespaced Redis connections
  redis = conn.respond_to?(:namespace) ? conn.redis : conn
  redis.script(:kill)
end

#load(name, conn) ⇒ Script

Load a script from disk, store in Redis, and cache in memory

Parameters:

  • name (Symbol, String)

    the script name

  • conn (Redis)

    the redis connection

Returns:

  • (Script)

    the loaded script



65
66
67
68
69
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 65

def load(name, conn)
  script = Script.load(name, root_path, conn)
  scripts.put(name.to_sym, script)
  script
end