Class: Faye::Redis::MessageQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/faye/redis/message_queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @connection = connection
  @options = options
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/faye/redis/message_queue.rb', line 7

def connection
  @connection
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/faye/redis/message_queue.rb', line 7

def options
  @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.message}")
  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)

  json_messages = @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
  messages = []
  if json_messages && json_messages[0]
    json_messages[0].each do |json|
      begin
        messages << JSON.parse(json)
      rescue JSON::ParserError => e
        log_error("Failed to parse message JSON: #{e.message}")
      end
    end
  end

  EventMachine.next_tick { callback.call(messages) } if callback
  messages
rescue => e
  log_error("Failed to dequeue messages for client #{client_id}: #{e.message}")
  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, message, &callback)
  # Add unique ID if not present (for message deduplication)
  message_with_id = message.dup
  message_with_id['id'] ||= generate_message_id

  # Store message directly as JSON
  message_json = message_with_id.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), message_json)
      # Set TTL on queue (only if it doesn't already have one)
      pipeline.expire(queue_key(client_id), message_ttl)
    end
  end

  EventMachine.next_tick { callback.call(true) } if callback
rescue => e
  log_error("Failed to enqueue message for client #{client_id}: #{e.message}")
  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)
  json_messages = @connection.with_redis do |redis|
    redis.lrange(queue_key(client_id), 0, limit - 1)
  end

  messages = json_messages.map do |json|
    begin
      JSON.parse(json)
    rescue JSON::ParserError => e
      log_error("Failed to parse message JSON: #{e.message}")
      nil
    end
  end.compact

  EventMachine.next_tick { callback.call(messages) } if callback
  messages
rescue => e
  log_error("Failed to peek messages for client #{client_id}: #{e.message}")
  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.message}")
  EventMachine.next_tick { callback.call(0) } if callback
  0
end