Class: Daemons::Application
- Inherits:
-
Object
- Object
- Daemons::Application
- Defined in:
- lib/feed_updater/vendor/daemons/application.rb
Instance Attribute Summary collapse
-
#app_argv ⇒ Object
Returns the value of attribute app_argv.
-
#controller_argv ⇒ Object
Returns the value of attribute controller_argv.
-
#group ⇒ Object
readonly
the ApplicationGroup the application belongs to.
-
#options ⇒ Object
readonly
my private options.
-
#pid ⇒ Object
readonly
the Pid instance belonging to this application.
Instance Method Summary collapse
-
#exception_log ⇒ Object
This is a nice little function for debugging purposes: In case a multi-threaded ruby script exits due to an uncaught exception it may be difficult to find out where the exception came from because one cannot catch exceptions that are thrown in threads other than the main thread.
-
#initialize(group, add_options = {}, pid = nil) ⇒ Application
constructor
A new instance of Application.
- #logfile ⇒ Object
- #pidfile_dir ⇒ Object
-
#running? ⇒ Boolean
This function implements a (probably too simle) method to detect whether the program with the pid found in the pid-file is still running.
- #script ⇒ Object
- #show_status ⇒ Object
- #start ⇒ Object
- #start_exec ⇒ Object
- #start_load ⇒ Object
- #start_none ⇒ Object
- #start_proc ⇒ Object
- #stop ⇒ Object
- #zap ⇒ Object
- #zap! ⇒ Object
Constructor Details
#initialize(group, add_options = {}, pid = nil) ⇒ Application
Returns a new instance of Application.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 22 def initialize(group, = {}, pid = nil) @group = group @options = group..dup @options.update() unless @pid = pid if dir = pidfile_dir @pid = PidFile.new(pidfile_dir(), @group.app_name, @group.multiple) else @pid = PidMem.new end end end |
Instance Attribute Details
#app_argv ⇒ Object
Returns the value of attribute app_argv.
9 10 11 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 9 def app_argv @app_argv end |
#controller_argv ⇒ Object
Returns the value of attribute controller_argv.
10 11 12 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 10 def controller_argv @controller_argv end |
#group ⇒ Object (readonly)
the ApplicationGroup the application belongs to
16 17 18 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 16 def group @group end |
#options ⇒ Object (readonly)
my private options
19 20 21 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 19 def @options end |
#pid ⇒ Object (readonly)
the Pid instance belonging to this application
13 14 15 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 13 def pid @pid end |
Instance Method Details
#exception_log ⇒ Object
This is a nice little function for debugging purposes: In case a multi-threaded ruby script exits due to an uncaught exception it may be difficult to find out where the exception came from because one cannot catch exceptions that are thrown in threads other than the main thread.
This function searches for all exceptions in memory and outputs them to STDERR (if it is connected) and to a log file in the pid-file directory.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 216 def exception_log require 'logger' l_file = Logger.new(File.join(pidfile_dir(), @group.app_name + '.log')) # the code below only logs the last exception # e = nil # # ObjectSpace.each_object {|o| # if ::Exception === o # e = o # end # } # # l_file.error e # l_file.close # this code logs every exception found in memory ObjectSpace.each_object {|o| if ::Exception === o l_file.error o end } l_file.close end |
#logfile ⇒ Object
44 45 46 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 44 def logfile ([:log_output] && pidfile_dir()) ? File.join(pidfile_dir(), @group.app_name + '.output') : nil end |
#pidfile_dir ⇒ Object
40 41 42 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 40 def pidfile_dir Pid.dir(@dir_mode || @group.dir_mode, @dir || @group.dir, @script || @group.script) end |
#running? ⇒ Boolean
This function implements a (probably too simle) method to detect whether the program with the pid found in the pid-file is still running. It just searches for the pid in the output of ps ax
, which is probably not a good idea in some cases. Alternatives would be to use a direct access method the unix process control system.
289 290 291 292 293 294 295 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 289 def running? if @pid.exists? return Pid.running?(@pid.pid) end return false end |
#script ⇒ Object
36 37 38 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 36 def script @script || @group.script end |
#show_status ⇒ Object
276 277 278 279 280 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 276 def show_status running = self.running? puts "#{self.group.app_name}: #{running ? '' : 'not '}running#{(running and @pid.exists?) ? ' [pid ' + @pid.pid.to_s + ']' : ''}#{(@pid.exists? and not running) ? ' (but pid-file exists: ' + @pid.pid.to_s + ')' : ''}" end |
#start ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 173 def start @group.create_monitor(@group.applications[0] || self) case [:mode] when :none start_none when :exec start_exec when :load start_load when :proc start_proc else start_load end end |
#start_exec ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 87 def start_exec unless [:ontop] Daemonize.daemonize(logfile) else Daemonize.simulate(logfile) end @pid.pid = Process.pid ENV['DAEMONS_ARGV'] = @controller_argv.join(' ') # haven't tested yet if this is really passed to the exec'd process... Kernel.exec(script(), *ARGV) end |
#start_load ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 102 def start_load unless [:ontop] Daemonize.daemonize(logfile) else Daemonize.simulate(logfile) end @pid.pid = Process.pid # We need this to remove the pid-file if the applications exits by itself. # Note that <tt>at_text</tt> will only be run if the applications exits by calling # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt> # in your application! # at_exit { @pid.cleanup rescue nil # If the option <tt>:backtrace</tt> is used and the application did exit by itself # create a exception log. if [:backtrace] and not [:ontop] and not $daemons_sigterm exception_log() rescue nil end } # This part is needed to remove the pid-file if the application is killed by # daemons or manually by the user. # Note that the applications is not supposed to overwrite the signal handler for # 'TERM'. # trap('TERM') { @pid.cleanup rescue nil $daemons_sigterm = true exit } # Know we really start the script... $DAEMONS_ARGV = @controller_argv ENV['DAEMONS_ARGV'] = @controller_argv.join(' ') ARGV.clear ARGV.concat @app_argv if @app_argv # TODO: begin - rescue - end around this and exception logging load script() end |
#start_none ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 48 def start_none unless [:ontop] Daemonize.daemonize #(logfile) else Daemonize.simulate end @pid.pid = Process.pid # We need this to remove the pid-file if the applications exits by itself. # Note that <tt>at_text</tt> will only be run if the applications exits by calling # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt> # in your application! # at_exit { @pid.cleanup rescue nil # If the option <tt>:backtrace</tt> is used and the application did exit by itself # create a exception log. if [:backtrace] and not [:ontop] and not $daemons_sigterm exception_log() rescue nil end } # This part is needed to remove the pid-file if the application is killed by # daemons or manually by the user. # Note that the applications is not supposed to overwrite the signal handler for # 'TERM'. # trap('TERM') { @pid.cleanup rescue nil $daemons_sigterm = true exit } end |
#start_proc ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 151 def start_proc return unless [:proc] unless [:ontop] @pid.pid = Daemonize.call_as_daemon([:proc], logfile) else # Daemonize.simulate(logfile) # # @pid.pid = Process.pid # # Thread.new(&options[:proc]) unless @pid.pid = Process.fork Daemonize.simulate(logfile) [:proc].call exit else Process.detach(@pid.pid) end end end |
#stop ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 245 def stop if [:force] and not running? self.zap return end # Catch errors when trying to kill a process that doesn't # exist. This happens when the process quits and hasn't been # restarted by the monitor yet. By catching the error, we allow the # pid file clean-up to occur. begin Process.kill('TERM', @pid.pid) rescue Errno::ESRCH => e puts "#{e} #{@pid.pid}" puts "deleting pid-file." end # We try to remove the pid-files by ourselves, in case the application # didn't clean it up. @pid.cleanup rescue nil end |
#zap ⇒ Object
268 269 270 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 268 def zap @pid.cleanup end |
#zap! ⇒ Object
272 273 274 |
# File 'lib/feed_updater/vendor/daemons/application.rb', line 272 def zap! @pid.cleanup rescue nil end |