Class: SidekiqUniqueJobs::Script::Scripts
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::Script::Scripts
- Defined in:
- lib/sidekiq_unique_jobs/script/scripts.rb
Overview
Interface to dealing with .lua files
Constant Summary collapse
- SCRIPT_PATHS =
Returns a map with configured script paths.
Concurrent::Map.new
Instance Attribute Summary collapse
-
#root_path ⇒ Object
readonly
Returns the value of attribute root_path.
-
#scripts ⇒ Object
readonly
Returns the value of attribute scripts.
Class Method Summary collapse
-
.fetch(root_path) ⇒ Scripts
Fetch or create a scripts configuration for path.
Instance Method Summary collapse
- #count ⇒ Object
-
#delete(script) ⇒ Script?
Delete a script from the collection.
-
#execute(name, conn, keys: [], argv: []) ⇒ Object
Execute a lua script with given name.
-
#fetch(name, conn) ⇒ Script
Fetch or load a script by name.
-
#initialize(path) ⇒ Scripts
constructor
A new instance of Scripts.
-
#kill(conn) ⇒ String
Kill a running Redis script.
-
#load(name, conn) ⇒ Script
Load a script from disk, store in Redis, and cache in memory.
Constructor Details
#initialize(path) ⇒ Scripts
Returns a new instance of Scripts.
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_path ⇒ Object (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 |
#scripts ⇒ Object (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
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
#count ⇒ Object
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
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
this method is recursive if we need to load a lua script that wasn’t previously loaded.
Execute a lua script with given name
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
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
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 |