Class: CassandraQueue::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra-queue.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.retrieve(qid, opts = {}) ⇒ Object Also known as: get_queue, get

Entry point for using a queue. Class method which will return you a queue object for that UUID



27
28
29
30
31
32
# File 'lib/cassandra-queue.rb', line 27

def self.retrieve(qid, opts = {})
  string_queue = opts[:string_queue] || false
  keyspace = opts[:keyspace] || DEFAULT_KEYSPACE
  servers = opts[:servers] || DEFAULT_SERVERS
  QueueManager.queue(qid, string_queue, keyspace, servers)
end

Instance Method Details

#empty?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/cassandra-queue.rb', line 76

def empty?(options = {})
  list(true, options).empty?
end

#insert(payload, time = Time.now, options = {}) ⇒ Object Also known as: add

Takes a payload, throws it on the queue, and returns the TimeUUID that was created for it



39
40
41
42
43
# File 'lib/cassandra-queue.rb', line 39

def insert(payload, time = Time.now, options = {})
  timeUUID = UUID.new(time)
  @client.insert(@queue_cf, @key, { timeUUID => payload }, options)
  timeUUID
end

#list(get_times = false, options = {}) ⇒ Object Also known as: list_queue, queue

Show the first 100 elements of the queue by default, for things such as failure recovery



61
62
63
64
# File 'lib/cassandra-queue.rb', line 61

def list(get_times = false, options = {})
  list = @client.get(@queue_cf, @key, options)
  get_times ? list : list.values
end

#payloads(options = {}) ⇒ Object Also known as: messages, values



69
70
71
# File 'lib/cassandra-queue.rb', line 69

def payloads(options = {})
  list(false, options)
end

#peek(get_time = false, options = {}) ⇒ Object Also known as: front, get_first

Show the first (oldest) element in the queue Returns payload [TimeUUID, payload] as a two element array



82
83
84
85
86
# File 'lib/cassandra-queue.rb', line 82

def peek(get_time = false, options = {})
  options.merge(:count => 1)
  payload = @client.get(@queue_cf, @key, options).first
  payload && !get_time ? payload.last : payload
end

#pop(get_time = false, options = {}) ⇒ Object Also known as: dequeue



91
92
93
94
95
96
# File 'lib/cassandra-queue.rb', line 91

def pop(get_time = false, options = {})
  item = peek(true, options)
  return nil if item.nil?
  remove(item.first, options)
  get_time ? item : item.last
end

#push(payload, options = {}) ⇒ Object Also known as: enqueue



47
48
49
# File 'lib/cassandra-queue.rb', line 47

def push(payload, options = {})
  insert(payload, Time.now, options)
end

#remove(timeUUID, options = {}) ⇒ Object Also known as: delete

Removes a TimeUUID, and it’s payload, from the queue



54
55
56
# File 'lib/cassandra-queue.rb', line 54

def remove(timeUUID, options = {})
  @client.remove(@queue_cf, @key, timeUUID, options)
end