README.md in larva-0.1.0 vs README.md in larva-0.3.0

- old
+ new

@@ -16,20 +16,79 @@ $ bundle install ## Usage +Larva provides you with listeners, processors and a worker pool to build an application that listens and responds to Propono messages. + +Here is a sample application. This forms the basis of a rake task for most Meducation daemons. + +```ruby +require 'larva' + +# Setup Config for Filum and Propono + +class MyProcessor < Larva::Processor + def process(message) + if entity == "comment" && action == "created" + # Do something... + else + false + end + end +end + +processors = {my_topic: MyProcessor} +Larva::WorkerPool.start(processors, "queue-suffix") + +``` + ### Listeners Larva Listeners provide an easy way of listening to a Propono topic and processing the message, complete with lots of logging through Filum. ```ruby +Larva::Listener.listen(:my_topic, processor, "queue_suffix") +``` + +This will listen for messages on :my_topic and pass them to `processor.process`. It will log what is happening via Filum. + +### Processors + +Processors are used by listeners to handle the messages that are received. You are expected to subclass `Lavar::Processor` and implement `process`. + +Processors expect you to have an `entity` and `action` fields in your messages. + +For example: + +```ruby class MyProcessor - def self.process(message) + def process(message) + if entity == "comment" && action == "created" + # Do something... + else + false + end end end +Larva::Listener.listen(:my_topic, MyProcessor, "") +Propono.publish(:my_topic, {entity: "comment", action: "created", id: 8} +``` -Larva::Listener.listen(:my_topic, MyProcessor, "queue_suffix") +With this code `MyProcessor#process` will get called for each message, with extra logging around the call. Within the class you have access to `message`, `action`, `entity` and `id` methods. + +### Worker Pool + +The worker pool creates a listener for each topic, and proxies messages to the associated processors. If any processors die, the application will die. + +Creating a worker pool is trivial: + +```ruby +processors = { + my_topic_1: MyProcessor1 + my_topic_2: MyProcessor2 +} +Larva::WorkerPool.start(processors, "queue-suffix") ``` ### Is it any good? [Yes.](http://news.ycombinator.com/item?id=3067434)