I’m creating a sort of background job queue system with MongoDB as the data store. How can I "listen" for inserts to a MongoDB collection before spawning workers to process the job?
Do I need to poll every few seconds to see if there are any changes from last time, or is there a way my script can wait for inserts to occur?
This is a PHP project that I am working on, but feel free to answer in Ruby or language agnostic.
MongoDB has what is called
capped collectionsandtailable cursorsthat allows MongoDB to push data to the listeners.A
capped collectionis essentially a collection that is a fixed size and only allows insertions. Here’s what it would look like to create one:MongoDB Tailable cursors (original post by Jonathan H. Wage)
Ruby
PHP
Python (by Robert Stewart)
Perl (by Max)
Additional Resources:
Ruby/Node.js Tutorial which walks you through creating an application that listens to inserts in a MongoDB capped collection.
An article talking about tailable cursors in more detail.
PHP, Ruby, Python, and Perl examples of using tailable cursors.