class SidekiqUniqueJobs::Script::Scripts
Interface to dealing with .lua files
@author Mikael Henriksson <mikael@mhenrixon.com>
Constants
- SCRIPT_PATHS
-
@return [Concurrent::Map] a map with configured script paths
Attributes
@!attribute [r] root_path
@return [Pathname] the path to the directory with lua scripts
@!attribute [r] scripts
@return [Concurrent::Map] a collection of loaded scripts
Public Class Methods
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 35 def self.create(root_path) scripts = new(root_path) store(scripts) end
Create a new scripts collection based on path
@param [Pathname] root_path
the path to scripts
@return [Scripts] a collection of scripts
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 20 def self.fetch(root_path) if (scripts = SCRIPT_PATHS.get(root_path)) return scripts end create(root_path) end
Fetch a scripts configuration for path
@param [Pathname] root_path
the path to scripts
@return [Scripts] a collection of scripts
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 62 def initialize(path) raise ArgumentError, "path needs to be a Pathname" unless path.is_a?(Pathname) @scripts = Concurrent::Map.new @root_path = path end
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 47 def self.store(scripts) SCRIPT_PATHS.put(scripts.root_path, scripts) scripts end
Store the scripts collection in memory
@param [Scripts] scripts the path to scripts
@return [Scripts] the scripts instance that was stored
Public Instance Methods
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 118 def count scripts.keys.size end
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 84 def delete(script) if script.is_a?(Script) scripts.delete(script.name) else scripts.delete(script.to_sym) end end
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 113 def execute(name, conn, keys: [], argv: []) script = fetch(name, conn) conn.evalsha(script.sha, keys, argv) end
Execute a lua script with given name
@note this method is recursive if we need to load a lua script
that wasn't previously loaded.
@param [Symbol] name the name of the script to execute @param [Redis] conn the redis connection to use for execution @param [Array<String>] keys script keys @param [Array<Object>] argv script arguments
@return value from script
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 69 def fetch(name, conn) if (script = scripts.get(name.to_sym)) return script end load(name, conn) end
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 92 def kill(conn) if conn.respond_to?(:namespace) conn.redis.script(:kill) else conn.script(:kill) end end
Source
# File lib/sidekiq_unique_jobs/script/scripts.rb, line 77 def load(name, conn) script = Script.load(name, root_path, conn) scripts.put(name.to_sym, script) script end