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,
:app_name => "myproc",
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
:monitor_interval:: Interval in sesconds at which to check whether the instances are still running
:monitor:: Monitor the programs and restart crashed instances
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
+app_name+:: The name of the application.
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) new_app = Daemons.new(options, &block) new_app.start new_app end
def controller
def controller @controller end
def daemonize(options = {})
}
serve(conn)
puts "some text which goes to the output logfile"
conn = accept_conn()
loop {
# Server loop:
Daemons.daemonize(options)
}
:log_output => true
:ontop => true,
:backtrace => true,
options = {
=== Example:
-----
:log_output_syslog:: When set to true, redirect output into SYSLOG instead of the file. This overrides log_output setting.
:log_output:: When given (i.e. set to true), redirect both $stdout and $stdout to a logfile named '[app_name].output' in the pid-file directory
location as derived from the :dir_mode and :dir options
:log_dir:: A specific directory to put the log files into (when not given, resort to the default
:dir:: Used in combination with :dir_mode (description above)
(/var/run is used as the file directory)
:dir is interpreted as a (absolute or relative) path) or :system
to the script location given by +script+, the default) or :normal (the directory given by
given by :dir is interpreted relative
:dir_mode:: Either :script (the directory for writing files to
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
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 = {}) options[:script] ||= File.basename(__FILE__) @group ||= ApplicationGroup.new(options[:app_name] || options[:script], options) @group.new_application(:mode => :none).start end
def group
def group @group end
def new(options = {}, &block)
def new(options = {}, &block) fail 'Daemons.call: no block given' unless block_given? options[:app_name] ||= 'proc' options[:proc] = Proc.new(&block) options[:mode] = :proc options[:dir_mode] = :normal @group ||= ApplicationGroup.new(options[:app_name], options) @group.new_application(options) end
def run(script, options = {})
Daemons.run(File.join(File.dirname(__FILE__), 'myscript.rb'), options)
}
:output_logfilename => 'custom_outputfile.txt'
:logfilename => 'custom_logfile.log',
:monitor => true,
:backtrace => true,
:mode => :exec,
:shush => false,
:ontop => true,
:pid_delimiter => '.n',
: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_syslog:: When set to true, redirect output into SYSLOG instead of the file. This overrides log_output setting.
:output_logfilename:: Specifiy a custom output redirection file name
:log_output:: When given (i.e. set to true), redirect both $stdout and $stderr to a logfile named '[app_name].output' (or as given in :output_logfilename) in the pid-file directory
:logfilename:: Specifiy a custom log file name
location as derived from the :dir_mode and :dir options
:log_dir:: A specific directory to put the log files into (when not given, resort to the default
:monitor_interval:: Interval in sesconds at which to check whether the instances are still running
: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;
:shush:: When given (i.e. set to true), turn on silent mode (no output to the terminal)
(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
:pid_delimiter:: Specifies the separator used when enumerating multiple process names/pid-files. Default is '_num'.
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+, the default) 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
there to restart your daemon if it crashes.
end with _monitor because that name is reserved for a monitor process which is
the controlling script from the appropriate directory. Your script name should not
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 do @controller.run end # 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 do @controller.run end # I don't think anybody will ever use @group, as this location should not be reached under non-error conditions @group = @controller.group end