 
 
 
+
+
+
+
+
+
+
+
+

class MongoQueueClient extends AbstractQueueClient
  constructor: ->
    @configure()

  registerQueue: (queueName, options, callback) ->
    channel = new MongoQueue queueName,options,this
    callback(null, channel)

  configure: ->
    @databaseAddress = db.generateDatabaseAddress()
    @mongoDatabaseName = config.mongoQueue.queueDatabaseName;
    @createMongoConnection()

  createMongoConnection: ->
    @mongooseConnection = mongoose.createConnection "mongodb://#{@databaseAddress}/#{@mongoDatabaseName}"

    @mongooseConnection.once 'open', ->
      winston.info "Successfully connected to MongoDB queue!"

  generateMessageModel: ->
    #do find something like: messages not processing, queue as current queue, visibility time before now, sort by insertion time, findOne
    schema = new mongoose.Schema
      messageBody : Object,
      processing: false,
      insertionTime: {type: Date, default: Date.now }
      queue: String
      scheduledVisibilityTime: Date
    @mongooseConnection.model 'Message',schema


class MongoQueue extends events.EventEmitter
  constructor: (@queueName, options, @Message) ->

  subscribe: (eventName, callback) ->
    @channel.subscribe eventName, callback

