Class: MongoDBQueue::MongoDBQueue

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

Constant Summary collapse

DEFAULT_QUEUE =
:default_queue

Instance Method Summary collapse

Constructor Details

#initialize(settings, logger = nil) ⇒ MongoDBQueue

Initializer

Options Hash (settings):

  • :address (String)

    MongoDB address

  • :port (Integer)

    MongoDB port

  • :database (String)

    MongoDB database to use

  • :collection (String)

    MongoDB collection to use

  • :username (String)

    MongoDB username to use (optional)

  • :password (String)

    MongoDB password to use (optional)


18
19
20
21
22
23
24
# File 'lib/mongodb_queue.rb', line 18

def initialize(settings, logger=nil)
  @logger = logger || Logger.new(STDOUT)
  check_settings(settings)
  @settings = settings
  @unique_fields = []
  connect_mongo
end

Instance Method Details

#dequeue(queue_name, opts = {}) ⇒ Hash

Gets an object from a queue

Options Hash (opts):

  • :status (String)

    The status to mark the document as after it's dequeued. Defaults to 'dequeue'

  • :delete (Boolean)

    Delete the object from ALL queues when dequeued.


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongodb_queue.rb', line 51

def dequeue(queue_name, opts={})
  connect_mongo
  @logger.info 'Checking queue'
  status = opts[:status] || 'dequeue'
  delete = opts[:delete]

  query = {queue: {'$elemMatch' => {name: queue_name, status: :queue}}}
  
  if delete
    @queue.find_and_modify(query: query, remove: true)
  else
    @queue.find_and_modify(query: query, update: {'$set' => {'queue.$.status' => status, "queue.$.#{status}_timestamp" => Time.now}})
  end
end

#destroyObject

Disconnects from MongoDB. Call before exiting.


27
28
29
30
31
# File 'lib/mongodb_queue.rb', line 27

def destroy
  begin
    disconnect_mongo
  rescue; end
end

#enqueue(queue_names, object, opts = {}) ⇒ Object

Add an object to a queue

Options Hash (opts):

  • :unique_field (String)

    Prevent duplicate documents being queued on the same queue by this unique field


38
39
40
41
42
43
# File 'lib/mongodb_queue.rb', line 38

def enqueue(queue_names, object, opts={})
  connect_mongo
  unique_field = opts[:unique_field]
  set_unique unique_field
  send_queues(queue_names, object, unique_field)
end

#simple_dequeueHash

A simple interface for dequeueing an object. This utilized one queue and deletes the document when done. Should be used with #simple_enqueue


74
75
76
# File 'lib/mongodb_queue.rb', line 74

def simple_dequeue
  dequeue(DEFAULT_QUEUE, {delete: true})
end

#simple_enqueue(object) ⇒ Object

A simple interface for queueing objects. This utilizes one queue. Should be used with #simple_dequeue


68
69
70
# File 'lib/mongodb_queue.rb', line 68

def simple_enqueue(object)
  enqueue(DEFAULT_QUEUE, object)
end