Daemon Objects is designed to simplify using daemons in your ruby applications. Under the hood, it uses the daemons gem, which is a mature and tested solution. But, it adds support for managing via rake tasks, error handling and instrumentation.
The daemons gem also is intended to be used to daemonize a ruby script. DaemonObjects provides an object-oriented framework for developing daemons. This allows the application developer to focus on the specific behavior of the daemon instead of the infrastructure of daemon management.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install daemon_objects
DaemonObjects will create daemons based on a simple convention. It will search a directory for files name *Daemon.rb. These typically will just inherit from the base Daemon class.
class MyDaemon < DaemonObjects::Base; end
This provides the basic daemon control methods (start, stop, run and restart) to your daemon.
To add behavior to your daemon, you will need a consumer. DaemonObjects will load the consumer using the name of the daemon and will search in the same directory for it. For example, if your daemon is name MyDaemon, the consumer should be named MyConsumer.
A consumer needs to inherit from the consumer base and implement run. For example,
class MyConsumer < DaemonObjects::ConsumerBase def run loop do "I'm looping" sleep 5 end end end
Once you have defined the daemon, you can control it with rake tasks. To access the rake tasks, you will need to include the daemon_objects railtie in config/application.rb.
Rake tasks are created using the daemon file name. The rake syntax is:
For example, to start the MyDaemon daemon:
Four commands are supported
- start - Starts the daemon
- stop - Stops the daemon
- restart - Stops and then starts the daemon
- run - Runs the daemon synchronously
DaemonObjects also has support for monitoring an amqp queue. This is done with the amqp gem. To support this
with your daemon, add
supports_amqp to your daemon class.
class MyQueueProcessingDaemon < Daemon::Base supports_amqp :endpoint => "http://localhost:5672", :queue_name => "my_awesome_queue" end
This will add the code to monitor the queue, so all you need now is code to handle the messages.
class MyQueueProcessingConsumer < Daemon::ConsumerBase handle_messages_with do |payload| puts "payload" end end
DaemonObjects will create a new log file for your daemon using the pattern daemon_file_name_daemon.log. In a rails project, this will be created in the log directory of your application.
Support for third-party libraries
DaemonObjects supports the following third-party libraries. If they are required in your application, your daemon will use them.
- Airbrake - any errors that occur in the daemon will be reported to Airbrake.
- NewRelic - amqp daemons will have instrument the handle_message method and report to New Relic.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request