Class: Hg::Queues::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/hg/queues/queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue_key) ⇒ Hg::Queues::Queue

Create a new interface to a queue living in redis at queue_key.

Parameters:

  • queue_key (String)

    The key at which the queue is stored in redis.



21
22
23
# File 'lib/hg/queues/queue.rb', line 21

def initialize(queue_key)
  @key = queue_key
end

Instance Method Details

#popHash

Pop the oldest message off of the queue.

Returns:

  • (Hash)

    The oldest message on the queue.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hg/queues/queue.rb', line 28

def pop
  message = Hg::Redis.execute do |conn|
    if message_json = conn.lpop(@key)
      JSON.parse(message_json)
    else
      {}
    end
  end

  return message
end

#push(message) ⇒ Object

Push a message onto the queue.

Parameters:

  • message (#to_json)

    The message to store on the queue.



43
44
45
46
47
# File 'lib/hg/queues/queue.rb', line 43

def push(message)
  Hg::Redis.execute do |conn|
    conn.rpush(@key, message.to_json)
  end
end