daemons gem integration into Rails 3 (based on rails_generator)
To get it work just add dependency to this gem in your Gemfile.
Daemons.run
to Daemons::Rails.run
in your *_ctl
filesscript/daemons
file to lib/daemons
directory.rails generate daemon <name>
Then insert your code in the lib/daemons/<name>.rb stub. All pids and logs will live in the normal log/ folder. This helps to make things Capistrano friendly.
Individual control script:
./lib/daemons/<name>_ctl [start|stop|restart|status]
rake daemon:<name>[:(start|stop|restart|status)]
Examples:
rake daemon:test - runs lib/daemons/test.rb not daemonized
rake daemon:test:start - start daemon using lib/daemons/test_ctl start
rake daemon:test:stop - stop daemon using lib/daemons/test_ctl stop
rake daemon:test:restart - restart daemon using lib/daemons/test_ctl restart
rake daemon:test:status - show running status for daemon using lib/daemons/test_ctl status
App-wide control script:
./lib/daemons/daemons [start|stop|restart|status]
rake daemons:(start|stop|status)
Daemons::Rails::Monitoring.statuses - hash with all daemons and corresponding statuses
Daemons::Rails::Monitoring.start("test.rb") - start daemon using lib/daemons/test_ctl start
Daemons::Rails::Monitoring.stop("test.rb") - start daemon using lib/daemons/test_ctl stop
Daemons::Rails::Monitoring.controllers - list of controllers
Daemons::Rails::Monitoring.controller("test.rb") - controller for test.rb application
controller = Daemons::Rails::Monitoring.controller("test.rb")
controller.path # => lib/daemons/test_ctl
controller.app_name # => test.rb
controller.start # => starts daemon
controller.stop # => stops daemon
controller.status # => :not_exists or :running
You can set default settings for your daemons into config/daemons.yml file. Full list of options you can get from documentation to Daemons.daemonize method (http://daemons.rubyforge.org/classes/Daemons.html#M000007). Also it possible to set individual daemon options using file config/<daemon_name>-daemon.yml.
If you want to use directory other than default lib/daemons then you should add to application initialization block following lines:
class MyApp < Rails::Application
...
Daemons::Rails.configure do |c|
c.daemons_path = Rails.root.join("new_daemons_path")
end
...
end
If you change your mind, you can easily move content of this directory to other place and change config.
Notice: this feature available only from version 1.1 and old generated daemons can’t be free moved, because uses hard-coded path to lib/daemons. So, you can generate daemons with same names and then move client code to generated templates.
At this moment it is not supported at generators and rake tasks levels, but you can generate daemon to default location and move to different folder (you should also copy daemons script to same directory). Then you will be able to operate with them using following scripts:
other/daemons/location/daemons [start|stop|restart|status]
other/daemons/location/<daemon_name>_ctl [start|stop|restart|status]
To access the daemons with Monitoring API you can use configured instance of Daemons::Rails::Monitoring:
Daemons::Rails::Monitoring.new("other/daemons/location")
and same set of methods. Effectively, Daemons::Rails::Monitoring just delegates all method calls to Daemons::Rails::Monitoring.default initialized with configured daemons path.
rake daemon:<name>:restart
commandYou are free to choose kind of license to use:
MIT License or GPL-2 License