Class: Magent::GenericChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/magent/generic_channel.rb

Direct Known Subclasses

Channel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ GenericChannel



5
6
7
# File 'lib/magent/generic_channel.rb', line 5

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/magent/generic_channel.rb', line 3

def name
  @name
end

Instance Method Details

#collectionObject



34
35
36
# File 'lib/magent/generic_channel.rb', line 34

def collection
  @collection ||= Magent.database.collection(@name)
end

#dequeueObject



21
22
23
24
25
# File 'lib/magent/generic_channel.rb', line 21

def dequeue
  if m = self.next_message
    m["message"]
  end
end

#enqueue(message) ⇒ Object



9
10
11
# File 'lib/magent/generic_channel.rb', line 9

def enqueue(message)
  collection.save({:_id => generate_uid, :message => message, :priority => 3, :created_at => Time.now.to_i})
end

#message_countObject



13
14
15
# File 'lib/magent/generic_channel.rb', line 13

def message_count
  collection.count # TODO: number of processed messages (create a collection for stats)
end

#next_messageObject



27
28
29
30
31
32
# File 'lib/magent/generic_channel.rb', line 27

def next_message
  Magent.database.command(BSON::OrderedHash[:findandmodify, @name,
                           :sort, [{:priority => -1}, {:created_at => 1}],
                           :remove, true
                          ])["value"]
end

#queue_countObject



17
18
19
# File 'lib/magent/generic_channel.rb', line 17

def queue_count
  collection.count
end