Class: Tina4::QueueBackends::LiteBackend
- Inherits:
-
Object
- Object
- Tina4::QueueBackends::LiteBackend
- Defined in:
- lib/tina4/queue_backends/lite_backend.rb
Overview
File-based queue backend — JSON files on disk. Zero dependencies.
Each job is stored as a separate .json file under <dir>/<topic>/. Dead-lettered jobs (those that exhausted their retries) live under the shared <dir>/dead_letter/ directory, tagged with their topic.
Dequeue policy: highest priority first, ties broken oldest-first by the stored created_at (ISO-8601, so lexicographic == chronological). The file name is NOT the ordering key — the stored priority/created_at are.
Instance Attribute Summary collapse
-
#max_retries ⇒ Object
Retry policy — settable so a Queue can propagate its own max_retries / retry_backoff onto a backend instance passed directly (legacy path).
-
#retry_backoff ⇒ Object
Retry policy — settable so a Queue can propagate its own max_retries / retry_backoff onto a backend instance passed directly (legacy path).
Instance Method Summary collapse
- #acknowledge(message) ⇒ Object
-
#clear(topic) ⇒ Object
Remove all pending jobs from a topic.
- #complete(message) ⇒ Object
- #dead_letter(message) ⇒ Object
-
#dead_letter_count(topic) ⇒ Object
Count dead-letter / failed messages for a topic.
-
#dead_letters(topic, max_retries: 3) ⇒ Object
Get dead letter jobs for a topic — messages that exceeded max retries.
- #dequeue(topic) ⇒ Object
- #dequeue_batch(topic, count) ⇒ Object
- #enqueue(message) ⇒ Object
-
#fail(job, error = "") ⇒ Object
Record a failed attempt.
-
#failed(topic, max_retries: 3) ⇒ Object
Jobs that have failed at least once but are still being retried.
-
#find_by_id(topic, id) ⇒ Object
Find a specific pending job by id, claim it (delete the file) and return it.
-
#initialize(options = {}) ⇒ LiteBackend
constructor
A new instance of LiteBackend.
-
#purge(topic, status) ⇒ Object
Delete messages by status.
- #requeue(message) ⇒ Object
-
#retry(job, delay_seconds: 0) ⇒ Object
Explicit re-queue requested by the caller (job.retry()).
-
#retry_failed(topic, max_retries: 3) ⇒ Object
Revive dead-letter jobs (under max_retries) back to pending.
-
#retry_job(topic, job_id: nil, delay_seconds: 0) ⇒ Object
Revive a specific dead-letter job by id back to the pending queue.
- #size(topic) ⇒ Object
- #topics ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ LiteBackend
Returns a new instance of LiteBackend.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 22 def initialize( = {}) @dir = [:dir] || File.join(Dir.pwd, ".queue") @dead_letter_dir = File.join(@dir, "dead_letter") # Retry policy. Mirrors the Python lite backend: a failed job is # re-enqueued while attempts < max_retries, then dead-lettered. @max_retries = [:max_retries] || 3 # Seconds to delay a job's next attempt when fail() re-enqueues it. # 0 (default) = retry on the very next pop/consume iteration. @retry_backoff = [:retry_backoff] || 0 FileUtils.mkdir_p(@dir) FileUtils.mkdir_p(@dead_letter_dir) @mutex = Mutex.new end |
Instance Attribute Details
#max_retries ⇒ Object
Retry policy — settable so a Queue can propagate its own max_retries / retry_backoff onto a backend instance passed directly (legacy path).
20 21 22 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 20 def max_retries @max_retries end |
#retry_backoff ⇒ Object
Retry policy — settable so a Queue can propagate its own max_retries / retry_backoff onto a backend instance passed directly (legacy path).
20 21 22 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 20 def retry_backoff @retry_backoff end |
Instance Method Details
#acknowledge(message) ⇒ Object
86 87 88 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 86 def acknowledge() # File already deleted on dequeue — nothing to do. end |
#clear(topic) ⇒ Object
Remove all pending jobs from a topic. Returns count removed.
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 245 def clear(topic) dir = topic_path(topic) return 0 unless Dir.exist?(dir) count = 0 Dir.glob(File.join(dir, "*.json")).each do |file| File.delete(file) count += 1 end count end |
#complete(message) ⇒ Object
90 91 92 93 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 90 def complete() # Job file was already deleted on dequeue. complete() is terminal: # the job is done and gone. end |
#dead_letter(message) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 121 def dead_letter() path = File.join(@dead_letter_dir, "#{.id}.json") data = .to_hash data[:status] = "dead" File.write(path, JSON.generate(data)) end |
#dead_letter_count(topic) ⇒ Object
Count dead-letter / failed messages for a topic.
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 135 def dead_letter_count(topic) return 0 unless Dir.exist?(@dead_letter_dir) count = 0 Dir.glob(File.join(@dead_letter_dir, "*.json")).each do |file| data = JSON.parse(File.read(file)) count += 1 if data["topic"] == topic.to_s rescue JSON::ParserError next end count end |
#dead_letters(topic, max_retries: 3) ⇒ Object
Get dead letter jobs for a topic — messages that exceeded max retries. Returns Hashes (raw job data with status “dead”).
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 157 def dead_letters(topic, max_retries: 3) return [] unless Dir.exist?(@dead_letter_dir) files = Dir.glob(File.join(@dead_letter_dir, "*.json")).sort_by { |f| File.mtime(f) } jobs = [] files.each do |file| data = JSON.parse(File.read(file)) next unless data["topic"] == topic.to_s next if (data["attempts"] || 0) < max_retries data["status"] = "dead" jobs << data rescue JSON::ParserError next end jobs end |
#dequeue(topic) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 45 def dequeue(topic) @mutex.synchronize do candidate = available_candidates(topic).first return nil unless candidate File.delete(candidate[:file]) job_from_data(candidate[:data], topic) end end |
#dequeue_batch(topic, count) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 55 def dequeue_batch(topic, count) @mutex.synchronize do chosen = available_candidates(topic).first(count) chosen.map do |c| File.delete(c[:file]) job_from_data(c[:data], topic) end end end |
#enqueue(message) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 36 def enqueue() @mutex.synchronize do topic_dir = topic_path(.topic) FileUtils.mkdir_p(topic_dir) path = File.join(topic_dir, "#{.id}.json") File.write(path, .to_json) end end |
#fail(job, error = "") ⇒ Object
Record a failed attempt. Increments attempts and stores the error. While attempts < max_retries the job is re-enqueued to pending (after the configured retry_backoff). Once attempts >= max_retries it is moved to the dead-letter store.
103 104 105 106 107 108 109 110 111 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 103 def fail(job, error = "") job.attempts += 1 job.error = error if job.attempts < @max_retries requeue_job(job, delay_seconds: @retry_backoff, error: error) else move_to_dead_letter(job, error) end end |
#failed(topic, max_retries: 3) ⇒ Object
Jobs that have failed at least once but are still being retried.
Under the auto-retry lifecycle a failed-but-retryable job lives in the pending queue (not the dead-letter dir), so this scans the topic dir for jobs with 0 < attempts < max_retries. Dead-lettered jobs are returned by dead_letters(). Returns Hashes.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 262 def failed(topic, max_retries: 3) dir = topic_path(topic) return [] unless Dir.exist?(dir) jobs = [] Dir.glob(File.join(dir, "*.json")).sort_by { |f| File.mtime(f) }.each do |file| data = JSON.parse(File.read(file)) attempts = data["attempts"] || 0 next unless attempts > 0 && attempts < max_retries jobs << data rescue JSON::ParserError next end jobs end |
#find_by_id(topic, id) ⇒ Object
Find a specific pending job by id, claim it (delete the file) and return it. Returns nil when no pending job with that id exists.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 67 def find_by_id(topic, id) @mutex.synchronize do dir = topic_path(topic) return nil unless Dir.exist?(dir) target = id.to_s Dir.glob(File.join(dir, "*.json")).each do |f| data = JSON.parse(File.read(f)) next unless data["id"].to_s == target File.delete(f) return job_from_data(data, topic) rescue JSON::ParserError next end nil end end |
#purge(topic, status) ⇒ Object
Delete messages by status. For “failed”/“dead”/“dead_letter”, removes from the dead_letter directory. For “completed”/“pending”, removes matching jobs from the topic (pending) directory. Returns count purged.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 179 def purge(topic, status) count = 0 if dead_status?(status) return 0 unless Dir.exist?(@dead_letter_dir) Dir.glob(File.join(@dead_letter_dir, "*.json")).each do |file| data = JSON.parse(File.read(file)) if data["topic"] == topic.to_s File.delete(file) count += 1 end rescue JSON::ParserError next end else dir = topic_path(topic) return 0 unless Dir.exist?(dir) Dir.glob(File.join(dir, "*.json")).each do |file| data = JSON.parse(File.read(file)) if data["status"].to_s == status.to_s File.delete(file) count += 1 end rescue JSON::ParserError next end end count end |
#requeue(message) ⇒ Object
95 96 97 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 95 def requeue() enqueue() end |
#retry(job, delay_seconds: 0) ⇒ Object
Explicit re-queue requested by the caller (job.retry()). Always re-enqueues regardless of the retry limit — a manual override, distinct from the automatic fail() path. Increments attempts, clears the error.
116 117 118 119 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 116 def retry(job, delay_seconds: 0) job.attempts += 1 requeue_job(job, delay_seconds: delay_seconds, error: nil) end |
#retry_failed(topic, max_retries: 3) ⇒ Object
Revive dead-letter jobs (under max_retries) back to pending. Returns the number of jobs re-queued.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 214 def retry_failed(topic, max_retries: 3) return 0 unless Dir.exist?(@dead_letter_dir) dir = topic_path(topic) FileUtils.mkdir_p(dir) count = 0 Dir.glob(File.join(@dead_letter_dir, "*.json")).each do |file| data = JSON.parse(File.read(file)) next unless data["topic"] == topic.to_s next if (data["attempts"] || 0) >= max_retries msg = Tina4::Job.new( topic: data["topic"], payload: data["payload"], id: data["id"], priority: data["priority"] || 0, attempts: data["attempts"] || 0, error: data["error"] ) enqueue(msg) File.delete(file) count += 1 rescue JSON::ParserError next end count end |
#retry_job(topic, job_id: nil, delay_seconds: 0) ⇒ Object
Revive a specific dead-letter job by id back to the pending queue. When job_id is nil, revives every dead-letter for the topic.
This is a manual override (Queue#retry(job_id)) — it always revives a dead-letter regardless of attempt count, mirroring job.retry. Returns true if any job was re-queued.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 283 def retry_job(topic, job_id: nil, delay_seconds: 0) return false unless Dir.exist?(@dead_letter_dir) available_at = delay_seconds > 0 ? Time.now + delay_seconds : nil count = 0 Dir.glob(File.join(@dead_letter_dir, "*.json")).each do |file| data = JSON.parse(File.read(file)) next unless data["topic"] == topic.to_s next if job_id && data["id"] != job_id.to_s msg = Tina4::Job.new( topic: data["topic"], payload: data["payload"], id: data["id"], priority: data["priority"] || 0, attempts: (data["attempts"] || 0) + 1, available_at: available_at, error: nil ) enqueue(msg) File.delete(file) count += 1 break if job_id # found the specific job, stop scanning rescue JSON::ParserError next end count > 0 end |
#size(topic) ⇒ Object
128 129 130 131 132 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 128 def size(topic) dir = topic_path(topic) return 0 unless Dir.exist?(dir) Dir.glob(File.join(dir, "*.json")).length end |
#topics ⇒ Object
148 149 150 151 152 153 |
# File 'lib/tina4/queue_backends/lite_backend.rb', line 148 def topics return [] unless Dir.exist?(@dir) Dir.children(@dir) .reject { |d| d == "dead_letter" } .select { |d| File.directory?(File.join(@dir, d)) } end |