Class: Faye::Redis::MessageQueue
- Inherits:
-
Object
- Object
- Faye::Redis::MessageQueue
- Defined in:
- lib/faye/redis/message_queue.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#clear(client_id, &callback) ⇒ Object
Clear a client’s message queue.
-
#dequeue_all(client_id, &callback) ⇒ Object
Dequeue all messages for a client.
-
#enqueue(client_id, message, &callback) ⇒ Object
Enqueue a message for a client.
-
#initialize(connection, options = {}) ⇒ MessageQueue
constructor
A new instance of MessageQueue.
-
#peek(client_id, limit = 10, &callback) ⇒ Object
Peek at messages without removing them.
-
#size(client_id, &callback) ⇒ Object
Get queue size for a client.
Constructor Details
#initialize(connection, options = {}) ⇒ MessageQueue
Returns a new instance of MessageQueue.
9 10 11 12 |
# File 'lib/faye/redis/message_queue.rb', line 9 def initialize(connection, = {}) @connection = connection @options = end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/faye/redis/message_queue.rb', line 7 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/faye/redis/message_queue.rb', line 7 def @options end |
Instance Method Details
#clear(client_id, &callback) ⇒ Object
Clear a client’s message queue
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/faye/redis/message_queue.rb', line 110 def clear(client_id, &callback) # Simply delete the queue @connection.with_redis do |redis| redis.del(queue_key(client_id)) end EventMachine.next_tick { callback.call(true) } if callback rescue => e log_error("Failed to clear queue for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |
#dequeue_all(client_id, &callback) ⇒ Object
Dequeue all messages for a client
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/faye/redis/message_queue.rb', line 40 def dequeue_all(client_id, &callback) # Get all messages and delete queue in a single atomic operation key = queue_key(client_id) = @connection.with_redis do |redis| # Use MULTI/EXEC to atomically get and delete redis.multi do |multi| multi.lrange(key, 0, -1) multi.del(key) end end # Parse messages from JSON = [] if && [0] [0].each do |json| begin << JSON.parse(json) rescue JSON::ParserError => e log_error("Failed to parse message JSON: #{e.}") end end end EventMachine.next_tick { callback.call() } if callback rescue => e log_error("Failed to dequeue messages for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call([]) } if callback [] end |
#enqueue(client_id, message, &callback) ⇒ Object
Enqueue a message for a client
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/faye/redis/message_queue.rb', line 15 def enqueue(client_id, , &callback) # Add unique ID if not present (for message deduplication) = .dup ['id'] ||= # Store message directly as JSON = .to_json @connection.with_redis do |redis| # Use RPUSH with EXPIRE in a single pipeline redis.pipelined do |pipeline| # Add message to client's queue pipeline.rpush(queue_key(client_id), ) # Set TTL on queue (only if it doesn't already have one) pipeline.expire(queue_key(client_id), ) end end EventMachine.next_tick { callback.call(true) } if callback rescue => e log_error("Failed to enqueue message for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(false) } if callback end |
#peek(client_id, limit = 10, &callback) ⇒ Object
Peek at messages without removing them
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/faye/redis/message_queue.rb', line 73 def peek(client_id, limit = 10, &callback) = @connection.with_redis do |redis| redis.lrange(queue_key(client_id), 0, limit - 1) end = .map do |json| begin JSON.parse(json) rescue JSON::ParserError => e log_error("Failed to parse message JSON: #{e.}") nil end end.compact EventMachine.next_tick { callback.call() } if callback rescue => e log_error("Failed to peek messages for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call([]) } if callback [] end |
#size(client_id, &callback) ⇒ Object
Get queue size for a client
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/faye/redis/message_queue.rb', line 96 def size(client_id, &callback) queue_size = @connection.with_redis do |redis| redis.llen(queue_key(client_id)) end EventMachine.next_tick { callback.call(queue_size) } if callback queue_size rescue => e log_error("Failed to get queue size for client #{client_id}: #{e.}") EventMachine.next_tick { callback.call(0) } if callback 0 end |