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
-
.create(root_path) ⇒ Scripts
Create a new scripts collection based on path.
-
.fetch(root_path) ⇒ Scripts
Fetch a scripts configuration for path.
-
.store(scripts) ⇒ Scripts
Store the scripts collection in memory.
Instance Method Summary collapse
- #count ⇒ Object
- #delete(script) ⇒ Object
-
#execute(name, conn, keys: [], argv: []) ⇒ Object
Execute a lua script with given name.
- #fetch(name, conn) ⇒ Object
-
#initialize(path) ⇒ Scripts
constructor
A new instance of Scripts.
- #kill(conn) ⇒ Object
- #load(name, conn) ⇒ Object
Constructor Details
#initialize(path) ⇒ Scripts
Returns a new instance of Scripts.
62 63 64 65 66 67 |
# 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 |
Instance Attribute Details
#root_path ⇒ Object (readonly)
Returns the value of attribute root_path.
60 61 62 |
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 60 def root_path @root_path end |
#scripts ⇒ Object (readonly)
Returns the value of attribute scripts.
55 56 57 |
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 55 def scripts @scripts end |
Class Method Details
.create(root_path) ⇒ Scripts
Create a new scripts collection based on path
35 36 37 38 |
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 35 def self.create(root_path) scripts = new(root_path) store(scripts) end |
.fetch(root_path) ⇒ Scripts
Fetch a scripts configuration for path
20 21 22 23 24 25 26 |
# 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 |
.store(scripts) ⇒ Scripts
Store the scripts collection in memory
47 48 49 50 |
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 47 def self.store(scripts) SCRIPT_PATHS.put(scripts.root_path, scripts) scripts end |
Instance Method Details
#count ⇒ Object
118 119 120 |
# File 'lib/sidekiq_unique_jobs/script/scripts.rb', line 118 def count scripts.keys.size end |
#delete(script) ⇒ Object
84 85 86 87 88 89 90 |
# 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 |
#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
113 114 115 116 |
# 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 |
#fetch(name, conn) ⇒ Object
69 70 71 72 73 74 75 |
# 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 |
#kill(conn) ⇒ Object
92 93 94 95 96 97 98 |
# 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 |