1

I'm creating a ruby based tool of callback listeners for Restful applications that emit callbacks, so that I can access the callback emitted out, something like RequestBin. For the backend I have MongoDB where I have 1 main document which creates a reusable bucket per session to listen to requests and I have an embedded document which is populated per request.

class Bin
    include Mongoid::Document
    include Mongoid::Timestamps
    embeds_many :callbacks
    field :bin_id, type: String
end

class Callback
    include Mongoid::Document
    include Mongoid::Timestamps
    embedded_in :bin, :inverse_of => :bins
    field :callback_id, type: Integer
    field :http_method, type: String
    field :params, type: String
    field :headers, type: String
    field :raw_post, type: String
end

My question is there a way of to listen for insertion of callback document in MongoDB? I have looked around on internet and found that MongoDB has what is called capped collections and tailable cursors that allows MongoDB to push data to the listeners. But for me it will not work as I have the main document already created and I have to listen to the creation of embedded document.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
harshs08
  • 700
  • 10
  • 29
  • 1
    It sounds like you are looking for MongoDB triggers. They do not exist in any official way - http://stackoverflow.com/questions/9691316/how-to-listen-for-changes-to-a-mongodb-collection – maerics Feb 17 '15 at 19:48

2 Answers2

1

Nope. There is no way to listen for document changes in MongoDB.

Search for "mongodb triggers" to learn more.

How to listen for changes to a MongoDB collection?

Community
  • 1
  • 1
maerics
  • 151,642
  • 46
  • 269
  • 291
  • Thanks maerics for reply.I looked into link you have provided, but answer given there is using capped collection and in my case I don't have a capped collection element and moreover I want to listen to the changes of the embedded document. – harshs08 Feb 17 '15 at 20:24
  • @harshs08: correct - the answer is "no, you cannot do what you want". – maerics Feb 17 '15 at 20:27
  • Thank you. I wish there would have some way to do that as querying again and again to check for some document existence is kind of expensive. – harshs08 Feb 17 '15 at 20:30
0

So I ended up doing the following,

  def self.wait_for_callback(bin_id)
    host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
    port = ENV['MONGO_RUBY_DRIVER_PORT'] || MongoClient::DEFAULT_PORT

    db = MongoClient.new(host, port).db('mongoid')
    coll = db.collection('bins')
    retries = 0

    res = coll.find("bin_id" => bin_id).to_a.first
    loop do
      if res["callbacks"].nil? && retries < 45
        sleep 5
        puts "Retry: #{retries}"
        retries += 1
        res = coll.find("bin_id" => bin_id).to_a.first
      else
        return res["callbacks"].to_a.first       
      end
    end
    return nil
  rescue Exception => e
    @@success =false
    @@errors << "Error:\n#{e.inspect}"
    if $debug_flag == "debug"
      puts @@errors 
   end
    return nil
  end

But here the problem is have do repeated querying. So is there a better way to do this?

harshs08
  • 700
  • 10
  • 29