Class: Falqon::Queue
- Inherits:
-
Object
- Object
- Falqon::Queue
- Extended by:
- T::Sig
- Includes:
- Hooks
- Defined in:
- lib/falqon/queue.rb
Overview
Simple, efficient, and reliable messaging queue implementation
Defined Under Namespace
Classes: Metadata
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The identifier of the queue (with prefix).
-
#max_retries ⇒ Integer
readonly
The maximum number of retries before a message is considered failed.
-
#name ⇒ String
readonly
The name of the queue (without prefix).
-
#retry_delay ⇒ Integer
readonly
The delay in seconds before a message is eligible for a retry.
-
#retry_strategy ⇒ Strategy
readonly
The configured retry strategy of the queue.
Class Method Summary collapse
-
.all ⇒ Array<Queue>
Get a list of all registered queues.
-
.size ⇒ Integer
Get the number of active (registered) queues.
Instance Method Summary collapse
-
#clear ⇒ Array<Identifier>
Clear the queue, removing all messages.
-
#delete ⇒ void
Delete the queue, removing all messages and deregistering the queue.
-
#empty? ⇒ Boolean
Check if the queue is empty.
-
#initialize(name, retry_strategy: Falqon.configuration.retry_strategy, max_retries: Falqon.configuration.max_retries, retry_delay: Falqon.configuration.retry_delay, redis: Falqon.configuration.redis, logger: Falqon.configuration.logger, version: Falqon::PROTOCOL) ⇒ void
constructor
Create a new queue.
-
#metadata ⇒ Metadata
Metadata of the queue.
-
#peek(index: 0) ⇒ Data?
Peek at the next message in the queue.
-
#pop(&block) ⇒ Data?
Pop data from the head of the queue.
-
#push(*data) ⇒ Identifier+
Push data onto the tail of the queue.
-
#range(start: 0, stop: -1)) ⇒ Array<Data>
Peek at the next messages in the queue.
-
#refill ⇒ Array<Identifier>
Refill the queue with messages from the processing queue.
-
#revive ⇒ Array<Identifier>
Revive the queue with messages from the dead queue.
-
#schedule ⇒ Array<Identifier>
Schedule failed messages for retry.
-
#size ⇒ Integer
Size of the queue.
Constructor Details
#initialize(name, retry_strategy: Falqon.configuration.retry_strategy, max_retries: Falqon.configuration.max_retries, retry_delay: Falqon.configuration.retry_delay, redis: Falqon.configuration.redis, logger: Falqon.configuration.logger, version: Falqon::PROTOCOL) ⇒ void
Create a new queue
Create a new queue in Redis with the given name. If a queue with the same name already exists, it is reused. When registering a new queue, the following Metadata is stored:
-
created_at
: Timestamp of creation -
updated_at
: Timestamp of last update -
version
: Protocol version
Initializing a queue with a different protocol version than the existing queue will raise a VersionMismatchError. Currently queues are not compatible between different protocol versions, and must be deleted and recreated manually. In a future version, automatic migration between protocol versions may be supported.
Please note that retry strategy, maximum retries, and retry delay are configured per queue instance, and are not shared between queue instances.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/falqon/queue.rb', line 71 def initialize( name, retry_strategy: Falqon.configuration.retry_strategy, max_retries: Falqon.configuration.max_retries, retry_delay: Falqon.configuration.retry_delay, redis: Falqon.configuration.redis, logger: Falqon.configuration.logger, version: Falqon::PROTOCOL ) @name = name @id = [Falqon.configuration.prefix, name].compact.join("/") @retry_strategy = Strategies.const_get(retry_strategy.to_s.capitalize).new(self) @max_retries = max_retries @retry_delay = retry_delay @redis = redis @logger = logger @version = version redis.with do |r| queue_version = r.hget("#{id}:metadata", :version) raise Falqon::VersionMismatchError, "Queue #{name} is using protocol version #{queue_version}, but this client is using protocol version #{version}" if queue_version && queue_version.to_i != @version r.multi do |t| # Register the queue t.sadd([Falqon.configuration.prefix, "queues"].compact.join(":"), name) # Set creation and update timestamp (if not set) t.hsetnx("#{id}:metadata", :created_at, Time.now.to_i) t.hsetnx("#{id}:metadata", :updated_at, Time.now.to_i) # Set protocol version t.hsetnx("#{id}:metadata", :version, @version) end end run_hook :initialize, :after end |
Instance Attribute Details
#id ⇒ String (readonly)
The identifier of the queue (with prefix)
19 20 21 |
# File 'lib/falqon/queue.rb', line 19 def id @id end |
#max_retries ⇒ Integer (readonly)
The maximum number of retries before a message is considered failed
27 28 29 |
# File 'lib/falqon/queue.rb', line 27 def max_retries @max_retries end |
#name ⇒ String (readonly)
The name of the queue (without prefix)
15 16 17 |
# File 'lib/falqon/queue.rb', line 15 def name @name end |
#retry_delay ⇒ Integer (readonly)
The delay in seconds before a message is eligible for a retry
31 32 33 |
# File 'lib/falqon/queue.rb', line 31 def retry_delay @retry_delay end |
#retry_strategy ⇒ Strategy (readonly)
The configured retry strategy of the queue
23 24 25 |
# File 'lib/falqon/queue.rb', line 23 def retry_strategy @retry_strategy end |
Class Method Details
.all ⇒ Array<Queue>
Get a list of all registered queues
585 586 587 588 589 590 591 592 |
# File 'lib/falqon/queue.rb', line 585 def all Falqon.configuration.redis.with do |r| r .smembers([Falqon.configuration.prefix, "queues"].compact.join(":")) .sort .map { |id| new(id) } end end |
.size ⇒ Integer
Get the number of active (registered) queues
599 600 601 602 603 604 |
# File 'lib/falqon/queue.rb', line 599 def size Falqon.configuration.redis.with do |r| r .scard([Falqon.configuration.prefix, "queues"].compact.join(":")) end end |
Instance Method Details
#clear ⇒ Array<Identifier>
Clear the queue, removing all messages
This method clears all messages from the queue, including pending, processing, scheduled, and dead messages. It also resets the metadata counters for processed, failed, and retried messages, but does not deregister the queue.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/falqon/queue.rb', line 319 def clear logger.debug "Clearing queue #{name}" run_hook :clear, :before # Clear all sub-queues = pending.clear + processing.clear + scheduled.clear + dead.clear redis.with do |r| r.multi do |t| # Clear metadata t.hdel("#{id}:metadata", :processed, :failed, :retried) # Set update timestamp t.hset("#{id}:metadata", :updated_at, Time.now.to_i) end end run_hook :clear, :after # Return identifiers end |
#delete ⇒ void
This method returns an undefined value.
Delete the queue, removing all messages and deregistering the queue
This method deletes the queue, removing all messages, metadata, and deregisters the queue.
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/falqon/queue.rb', line 353 def delete logger.debug "Deleting queue #{name}" run_hook :delete, :before # Delete all sub-queues [pending, processing, scheduled, dead] .each(&:clear) redis.with do |r| r.multi do |t| # Delete metadata t.del("#{id}:metadata") # Deregister the queue t.srem([Falqon.configuration.prefix, "queues"].compact.join(":"), name) end end run_hook :delete, :after end |
#empty? ⇒ Boolean
Check if the queue is empty
Only the pending queue is checked for emptiness.
515 516 517 |
# File 'lib/falqon/queue.rb', line 515 def empty? size.zero? end |
#metadata ⇒ Metadata
Metadata of the queue
525 526 527 528 529 530 |
# File 'lib/falqon/queue.rb', line 525 def redis.with do |r| Metadata .parse(r.hgetall("#{id}:metadata")) end end |
#peek(index: 0) ⇒ Data?
Peek at the next message in the queue
Use #range to peek at a range of messages. This method does not block.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/falqon/queue.rb', line 257 def peek(index: 0) logger.debug "Peeking at next message in queue #{name}" run_hook :peek, :before # Get identifier from pending queue = pending.peek(index:) return unless run_hook :peek, :after # Retrieve data Message.new(self, id: ).data end |
#pop(&block) ⇒ Data?
Pop data from the head of the queue
This method blocks until a message is available.
Acknowledgement
If a block is given, the popped data is passed to the block. If the block raises a Error exception, the message is retried according to the configured retry strategy. If no exception is raised, the message is ackwnowledged and removed from the queue.
If no block is given, the popped data is returned. The message is immediately acknowledged and removed from the queue.
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/falqon/queue.rb', line 181 def pop(&block) logger.debug "Popping message from queue #{name}" run_hook :pop, :before = redis.with do |r| # Move identifier from pending queue to processing queue = r.blmove(pending.id, processing.id, :left, :right).to_i # Get retry count retries = r.hget("#{id}:metadata:#{}", :retries).to_i r.multi do |t| # Set message status t.hset("#{id}:metadata:#{}", :status, "processing") # Set update timestamp t.hset("#{id}:metadata", :updated_at, Time.now.to_i) t.hset("#{id}:metadata:#{}", :updated_at, Time.now.to_i) # Increment processing counter t.hincrby("#{id}:metadata", :processed, 1) # Increment retry counter if message is retried t.hincrby("#{id}:metadata", :retried, 1) if retries.positive? end Message.new(self, id: ) end data = .data yield data if block run_hook :pop, :after # Remove identifier from processing queue processing.remove(.id) # Delete message .delete data rescue Error => e logger.debug "Error processing message #{.id}: #{e.}" # Increment failure counter redis.with { |r| r.hincrby("#{id}:metadata", :failed, 1) } # Retry message according to configured strategy retry_strategy.retry(, e) nil end |
#push(*data) ⇒ Identifier+
Push data onto the tail of the queue
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/falqon/queue.rb', line 124 def push(*data) logger.debug "Pushing #{data.size} messages onto queue #{name}" run_hook :push, :before # Set update timestamp redis.with { |r| r.hset("#{id}:metadata", :updated_at, Time.now.to_i) } ids = data.map do |d| = Message .new(self, data: d) .create # Push identifier to queue pending.add(.id) # Set message status redis.with { |r| r.hset("#{id}:metadata:#{.id}", :status, "pending") } # Return identifier(s) data.size == 1 ? (return .id) : (next .id) end run_hook :push, :after # Return identifier(s) ids end |
#range(start: 0, stop: -1)) ⇒ Array<Data>
Peek at the next messages in the queue
Use #peek to peek at a single message. This method does not block.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/falqon/queue.rb', line 290 def range(start: 0, stop: -1) logger.debug "Peeking at next messages in queue #{name}" run_hook :range, :before # Get identifiers from pending queue = pending.range(start:, stop:) return [] unless .any? run_hook :range, :after # Retrieve data .map { |id| Message.new(self, id:).data } end |
#refill ⇒ Array<Identifier>
Refill the queue with messages from the processing queue
This method moves all messages from the processing queue back to the pending queue (in order). It is useful when a worker crashes or is stopped, and messages are left in the processing queue.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/falqon/queue.rb', line 390 def refill logger.debug "Refilling queue #{name}" run_hook :refill, :before = [] # Move all identifiers from tail of processing queue to head of pending queue redis.with do |r| while ( = r.lmove(processing.id, id, :right, :left)) # Set message status r.hset("#{id}:metadata:#{}", :status, "pending") << end end run_hook :refill, :after end |
#revive ⇒ Array<Identifier>
Revive the queue with messages from the dead queue
This method moves all messages from the dead queue back to the pending queue (in order). It is useful when messages are moved to the dead queue due to repeated failures, and need to be retried.
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/falqon/queue.rb', line 426 def revive logger.debug "Reviving queue #{name}" run_hook :revive, :before = [] # Move all identifiers from tail of dead queue to head of pending queue redis.with do |r| while ( = r.lmove(dead.id, id, :right, :left)) # Set message status r.hset("#{id}:metadata:#{}", :status, "pending") << end end run_hook :revive, :after end |
#schedule ⇒ Array<Identifier>
Schedule failed messages for retry
This method moves all eligible messages from the scheduled queue back to the head of the pending queue (in order). Messages are eligible for a retry according to the configured retry strategy.
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/falqon/queue.rb', line 464 def schedule logger.debug "Scheduling failed messages on queue #{name}" run_hook :schedule, :before = T.let([], T::Array[Identifier]) # Move all due identifiers from scheduled queue to head of pending queue redis.with do |r| # Select all identifiers that are due (score <= current timestamp) # FIXME: replace with zrange(by_score: true) when https://github.com/sds/mock_redis/issues/307 is resolved # TODO: work in batches = r.zrangebyscore(scheduled.id, 0, Time.now.to_i).map(&:to_i) logger.debug "Scheduling messages #{.join(', ')} on queue #{name}" r.multi do |t| .each do || # Set message status t.hset("#{id}:metadata:#{}", :status, "pending") # Add identifier to pending queue pending.add() # Remove identifier from scheduled queue scheduled.remove() end end end run_hook :schedule, :after end |
#size ⇒ Integer
Size of the queue
504 505 506 |
# File 'lib/falqon/queue.rb', line 504 def size pending.size end |