Created
August 29, 2008 11:07
-
-
Save saimonmoore/7954 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.after_initialize do | |
puts "[RAILS] Rails loaded successfully!" | |
unless Object.const_defined?(:IRB) || Object.const_defined?(:MY_DAEMON_CTL_STARTED) || Object.const_defined?(:MY_OTHER_DAEMON_CTL_STARTED) RAILS_ENV == 'test' | |
puts "[RAILS] Starting daemons..." | |
begin | |
MyDaemonController.instance.start | |
rescue ::DaemonController::AlreadyStarted => das | |
puts "MyDaemon already started..." | |
end | |
begin | |
MyOtherDaemonController.instance.start | |
rescue ::DaemonController::AlreadyStarted => das | |
puts "MyOtherDaemon already started..." | |
end | |
end | |
at_exit do | |
puts "[RAILS] Rails exiting...Trace: #{caller.join("\n")}" | |
# Don't stop daemons if in irb or test environment or daemon controller (based on daemons gem) exited | |
unless Object.const_defined?(:IRB) || RAILS_ENV == 'test' || (caller.first && caller.first.grep(/daemons\/cmdline\.rb/)) | |
puts "[RAILS] Stopping daemons..." | |
begin | |
puts "[RAILS] Stopping MyDaemon..." | |
MyDaemonController.instance.stop | |
rescue Exception => e | |
puts "Exception stopping MyDaemon: #{e.message} Trace: #{e.backtrace.join("\n")}" | |
end | |
begin | |
puts "[RAILS] Stopping MyOtherDaemon..." | |
MyOtherDaemonController.instance.stop | |
rescue Exception => e | |
puts "Exception stopping MyOtherDaemon: #{e.message} Trace: #{e.backtrace.join("\n")}" | |
end | |
end | |
end | |
trap("INT") { puts "[RAILS] INT signal received." } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'daemon_controller' | |
require 'singleton' | |
class MyDaemonController | |
include Singleton | |
MY_DAEMON_PORT = 40000 | |
attr_reader :controller | |
def initialize | |
@controller = DaemonController.new( | |
:identifier => 'MyDaemon server', | |
:start_command => "#{RAILS_ROOT}/script/my_daemon_ctl start", | |
:before_start => method(:before_start), | |
:ping_command => lambda { TCPSocket.new('localhost', MY_DAEMON_PORT) }, | |
:pid_file => "#{RAILS_ROOT}/tmp/pids/my_daemon_#{RAILS_ENV}.pid", | |
:log_file => "#{RAILS_ROOT}/log/my_daemon_#{RAILS_ENV}.log") | |
end | |
def start | |
controller.start | |
end | |
def stop | |
controller.stop | |
end | |
private | |
def before_start | |
puts "daemon_controller starting my_daemon..." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment