Uses capped collection, tailable cursors and streams.
- init.js recreates collection capped collection 'queue' on mongodb.
- writer.js spams queue with new messages
- worker.js processes all messages saved to queue,
- onceWorker.js processes only unprocessed messages, so you can spawn several of them and each of your messages will be processed by only one worker.
- Create database named
captest
on your locally installed mongodb, or change connection strings in all *.js files - Start
node ./init.js
— it will recreate capped collection for messages. Note: it drops collection named 'queue' so don't run it on your database with valuable data. - Start
node ./writer.js
— it will start to push data to your collection
- Don't use in production — it's just example — it does not handle any errors.
- You can support your workers with queries during stream creation — it will allow you to pick only subset of all messages.
- Try to stop onceWorker while writer is still working, wait for some time, then start it again — you'll see how onceWorker will process all messages that were added in the time of it's "downtime"
- Try to start several instances of onceWorker.js to see how they balance workload
- You can implement worker.js as a Stream itself, and re-implement onceWorker as a Stream that takes event objects from worker.js Stream. Do it as a homework :)