Class: Kaal::Registry
- Inherits:
-
Object
- Object
- Kaal::Registry
- Includes:
- Enumerable
- Defined in:
- lib/kaal/registry.rb
Overview
Thread-safe registry for storing and managing registered cron jobs. Each entry consists of a unique key, cron expression, and enqueue callback.
Defined Under Namespace
Classes: Entry
Instance Method Summary collapse
-
#add(key:, cron:, enqueue:) ⇒ Entry
Register a new cron job.
-
#all ⇒ Array<Entry>
Get all registered entries.
-
#clear ⇒ Integer
Clear all registered entries.
-
#each {|entry| ... } ⇒ void
Iterate over all entries with thread-safe access.
-
#find(key) ⇒ Entry?
Find a registered entry by key.
-
#initialize ⇒ Registry
constructor
Initialize a new Registry instance.
-
#inspect ⇒ String
Get a string representation of the registry.
-
#registered?(key) ⇒ Boolean
Check if a key is registered.
-
#remove(key) ⇒ Entry?
Unregister (remove) a cron job by key.
-
#size ⇒ Integer
(also: #count)
Get the number of registered entries.
-
#to_a ⇒ Array<Hash>
Convert registry to an array of hashes.
-
#upsert(key:, cron:, enqueue:) ⇒ Entry
Insert or replace a cron job entry atomically.
Constructor Details
#initialize ⇒ Registry
Initialize a new Registry instance.
27 28 29 30 |
# File 'lib/kaal/registry.rb', line 27 def initialize @entries = {} @mutex = Mutex.new end |
Instance Method Details
#add(key:, cron:, enqueue:) ⇒ Entry
Register a new cron job.
Entries are frozen after creation to prevent external mutation that could corrupt the internal key->entry mapping.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kaal/registry.rb', line 52 def add(key:, cron:, enqueue:) validate_entry(key, cron, enqueue) @mutex.synchronize do raise RegistryError, "Key '#{key}' is already registered" if @entries.key?(key) entry = Entry.new(key: key, cron: cron, enqueue: enqueue).freeze @entries[key] = entry entry end end |
#all ⇒ Array<Entry>
Get all registered entries.
121 122 123 124 125 |
# File 'lib/kaal/registry.rb', line 121 def all @mutex.synchronize do @entries.values.dup end end |
#clear ⇒ Integer
Clear all registered entries.
167 168 169 170 171 172 173 |
# File 'lib/kaal/registry.rb', line 167 def clear @mutex.synchronize do count = @entries.size @entries.clear count end end |
#each {|entry| ... } ⇒ void
This method returns an undefined value.
Iterate over all entries with thread-safe access.
Copies entries inside the lock and yields outside to avoid deadlocks if the block calls back into the registry.
187 188 189 190 191 192 193 194 195 |
# File 'lib/kaal/registry.rb', line 187 def each(&) return enum_for(:each) unless block_given? entries_snapshot = @mutex.synchronize do @entries.values.dup end entries_snapshot.each(&) end |
#find(key) ⇒ Entry?
Find a registered entry by key.
107 108 109 110 111 |
# File 'lib/kaal/registry.rb', line 107 def find(key) @mutex.synchronize do @entries[key] end end |
#inspect ⇒ String
Get a string representation of the registry.
214 215 216 217 218 |
# File 'lib/kaal/registry.rb', line 214 def inspect @mutex.synchronize do "#<Kaal::Registry size=#{@entries.size} keys=[#{@entries.keys.map(&:inspect).join(', ')}]>" end end |
#registered?(key) ⇒ Boolean
Check if a key is registered.
154 155 156 157 158 |
# File 'lib/kaal/registry.rb', line 154 def registered?(key) @mutex.synchronize do @entries.key?(key) end end |
#remove(key) ⇒ Entry?
Unregister (remove) a cron job by key.
92 93 94 95 96 |
# File 'lib/kaal/registry.rb', line 92 def remove(key) @mutex.synchronize do @entries.delete(key) end end |
#size ⇒ Integer Also known as: count
Get the number of registered entries.
134 135 136 137 138 |
# File 'lib/kaal/registry.rb', line 134 def size @mutex.synchronize do @entries.size end end |
#to_a ⇒ Array<Hash>
Convert registry to an array of hashes.
204 205 206 207 208 |
# File 'lib/kaal/registry.rb', line 204 def to_a @mutex.synchronize do @entries.values.map(&:to_h) end end |
#upsert(key:, cron:, enqueue:) ⇒ Entry
Insert or replace a cron job entry atomically.
Unlike #add, this does not raise when the key already exists.
75 76 77 78 79 80 81 82 |
# File 'lib/kaal/registry.rb', line 75 def upsert(key:, cron:, enqueue:) validate_entry(key, cron, enqueue) @mutex.synchronize do entry = Entry.new(key: key, cron: cron, enqueue: enqueue).freeze @entries[key] = entry end end |