Class: Magent::GenericChannel

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

Direct Known Subclasses

ActorChannel, AsyncChannel, WebSocketChannel

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Failure

#enqueue_error, #error_collection, #error_count, #errors, #failed, #remove_error, #retry_error

Constructor Details

#initialize(name) ⇒ GenericChannel

Returns a new instance of GenericChannel.



7
8
9
# File 'lib/magent/generic_channel.rb', line 7

def initialize(name)
  @name = "magent.#{name}"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

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



23
24
25
26
27
# File 'lib/magent/generic_channel.rb', line 23

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

#enqueue(message, priority = 3) ⇒ Object



11
12
13
# File 'lib/magent/generic_channel.rb', line 11

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

#message_countObject



15
16
17
# File 'lib/magent/generic_channel.rb', line 15

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

#next_messageObject



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

def next_message
  collection.find_and_modify(:sort => [[:priority, Mongo::ASCENDING], [:created_at, Mongo::DESCENDING]],
                             :remove => true) rescue {}
end

#queue_countObject



19
20
21
# File 'lib/magent/generic_channel.rb', line 19

def queue_count
  collection.count
end