module Daemons
def call(options = {}, &block)
end
}
serve(conn)
conn = accept_conn()
loop {
# Server loop:
Daemons.call(options) begin
}
:ontop => true
:monitor => true,
:backtrace => true,
options = {
=== Example:
-----
pid-file directory if the application exits due to an uncaught exception
:backtrace:: Write a backtrace of the last exceptions to the file '[app_name].log' in the
:ontop:: When given, stay on top, i.e. do not daemonize the application
same time
:multiple:: Specifies whether multiple instances of the same script are allowed to run at the
=== Options:
+block+:: The block to call in the daemon.
+options+:: A hash that may contain one or more of the options listed below
after spawning the daemon with the new Application object as a return value.
Execute the block in a new daemon. Daemons.call will return immediately
def call(options = {}, &block) unless block_given? raise "Daemons.call: no block given" end options[:proc] = block options[:mode] = :proc @group ||= ApplicationGroup.new('proc', options) new_app = @group.new_application(options) new_app.start return new_app end
def controller; @controller; end
def controller; @controller; end
def daemonize(options = {})
}
serve(conn)
conn = accept_conn()
loop {
# Server loop:
Daemons.daemonize(options)
}
:ontop => true
:backtrace => true,
options = {
=== Example:
-----
pid-file directory if the application exits due to an uncaught exception
:backtrace:: Write a backtrace of the last exceptions to the file '[app_name].log' in the
:ontop:: When given, stay on top, i.e. do not daemonize the application
=== Options:
+options+:: A hash that may contain one or more of the options listed below
Daemonize the currently runnig process, i.e. the calling process will become a daemon.
def daemonize(options = {}) @group ||= ApplicationGroup.new(options[:app_name] || 'self', options) @group.new_application(:mode => :none).start end
def group; @group; end
def group; @group; end
def run(script, options = {})
Daemons.run(File.join(File.dirname(__FILE__), 'myscript.rb'), options)
}
:monitor => true
:backtrace => true,
:mode => :exec,
:ontop => true,
:multiple => true,
:dir => 'pids',
:dir_mode => :script,
:ARGV => ['start', '-f', '--', 'param_for_myscript']
:app_name => "my_app",
options = {
=== Example:
-----
:stop_proc:: A proc that will be called when the daemonized process receives a request to stop (works only for :load and :proc mode)
not call at_exit handlers).
:hard_exit:: When given use exit! to end a daemons instead of exit (this will for example
:keep_pid_files:: When given do not delete lingering pid-files (files for which the process is no longer running).
:log_output:: When given (i.e. set to true), redirect both STDOUT and STDERR to a logfile named '[app_name].output' in the pid-file directory
:monitor:: Monitor the programs and restart crashed instances
pid-file directory if the application exits due to an uncaught exception
:backtrace:: Write a backtrace of the last exceptions to the file '[app_name].log' in the
:exec Execute the script file with Kernel.exec
note that :stop_proc only works for the :load (and :proc) mode.
:mode:: :load Load the script with Kernel.load;
(but the pid-file and other things are written as usual)
:ontop:: When given (i.e. set to true), stay on top, i.e. do not daemonize the application
same time
:multiple:: Specifies whether multiple instances of the same script are allowed to run at the
:dir:: Used in combination with :dir_mode (description above)
(/var/run is used as the pid file directory)
:dir is interpreted as a (absolute or relative) path) or :system
to the script location given by +script+) or :normal (the directory given by
given by :dir is interpreted relative
:dir_mode:: Either :script (the directory for writing the pid files to
If not given, ARGV (the parameters given to the Ruby process) will be used.
['start', 'f', '--', 'param1_for_script', 'param2_for_script'].
These are assumed to be separated by an array element '--', .e.g.
This includes both parameters for Daemons itself and the controlled scripted.
:ARGV:: An array of strings containing parameters and switches for Daemons.
the script.
and log files. Defaults to the basename of
used to contruct the name of the pid files
:app_name:: The name of the application. This will be
=== Options:
+options+:: A hash that may contain one or more of the options listed below
the controlling script from the appropriate directory.
script resides, so this has to be either an absolute path or you have to run
Also note that Daemons cannot detect the directory in which the controlling
Please note that Daemons runs this script with load .
+script+:: This is the path to the script that should be run as a daemon.
to do anything useful.
Such wrapper script should be invoked with command line options like 'start' or 'stop'
external applications. Control is completely passed to the daemons library.
This is used in wrapper-scripts that are supposed to control other ruby scripts or
Passes control to Daemons.
def run(script, options = {}) options[:script] = script @controller = Controller.new(options, options[:ARGV] || ARGV) @controller.catch_exceptions { @controller.run } # I don't think anybody will ever use @group, as this location should not be reached under non-error conditions @group = @controller.group end
def run_proc(app_name, options = {}, &block)
end
end
close_connection()
send_response()
read_request()
accept_connection()
loop do
Daemons.run_proc('myproc.rb') do
=== Example:
-----
A block must be given to this function. The block will be used as the :proc entry in the options hash.
+options+:: A hash that may contain one or more of the options listed in the documentation for Daemons.run
the script.
and log files. Defaults to the basename of
used to contruct the name of the pid files
+app_name+:: The name of the application. This will be
and the whole pid-file management to control the proc.
will be run as a daemon while this script provides command line options like 'start' or 'stop'
This function does the same as Daemons.run except that not a script but a proc
Passes control to Daemons.
def run_proc(app_name, options = {}, &block) options[:app_name] = app_name options[:mode] = :proc options[:proc] = block # we do not have a script location so the the :script :dir_mode cannot be used, change it to :normal if [nil, :script].include? options[:dir_mode] options[:dir_mode] = :normal options[:dir] = File.expand_path('.') end @controller = Controller.new(options, options[:ARGV] || ARGV) @controller.catch_exceptions { @controller.run } # I don't think anybody will ever use @group, as this location should not be reached under non-error conditions @group = @controller.group end