Class: Process::Daemon::Controller
- Inherits:
-
Object
- Object
- Process::Daemon::Controller
- Defined in:
- lib/process/daemon/controller.rb
Overview
This module contains functionality related to starting and stopping the @daemon, and code for processing command line input.
Constant Summary collapse
- STOP_PERIOD =
How long to wait between checking the daemon process when shutting down:
0.1
Instance Method Summary collapse
-
#daemonize(argv = ARGV) ⇒ Object
This function is called from the daemon executable.
-
#initialize(daemon, options = {}) ⇒ Controller
constructor
options[:output]specifies where to write textual output describing what is going on. - #pid ⇒ Object
-
#spawn ⇒ Object
Fork a child process, detatch it and run the daemon code.
-
#start ⇒ Object
This function starts the daemon process in the background.
-
#status ⇒ Object
Prints out the status of the daemon.
-
#stop ⇒ Object
Stops the daemon process.
Constructor Details
#initialize(daemon, options = {}) ⇒ Controller
options[:output] specifies where to write textual output describing what is going on.
31 32 33 34 35 36 37 38 |
# File 'lib/process/daemon/controller.rb', line 31 def initialize(daemon, = {}) @daemon = daemon @output = [:output] || $stdout # How long to wait until sending SIGTERM and eventually SIGKILL to the daemon process group when asking it to stop: @stop_timeout = [:stop_timeout] || 10.0 end |
Instance Method Details
#daemonize(argv = ARGV) ⇒ Object
This function is called from the daemon executable. It processes ARGV and checks whether the user is asking for start, stop, restart, status.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/process/daemon/controller.rb', line 41 def daemonize(argv = ARGV) case argv.shift.to_sym when :start start status when :stop stop status ProcessFile.cleanup(@daemon) when :restart stop ProcessFile.cleanup(@daemon) start status when :status status else @stderr.puts Rainbow("Invalid command. Please specify start, restart, stop or status.").red end end |
#pid ⇒ Object
157 158 159 |
# File 'lib/process/daemon/controller.rb', line 157 def pid ProcessFile.recall(@daemon) end |
#spawn ⇒ Object
Fork a child process, detatch it and run the daemon code.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/process/daemon/controller.rb', line 63 def spawn @daemon.prefork @daemon.mark_log fork do Process.setsid exit if fork ProcessFile.store(@daemon, Process.pid) File.umask 0000 Dir.chdir @daemon.working_directory $stdin.reopen "/dev/null" $stdout.reopen @daemon.log_file_path, "a" $stdout.sync = true $stderr.reopen $stdout $stderr.sync = true begin @daemon.run rescue Exception => error $stderr.puts "=== Daemon Exception Backtrace @ #{Time.now.to_s} ===" $stderr.puts "#{error.class}: #{error.}" $!.backtrace.each { |at| $stderr.puts at } $stderr.puts "=== Daemon Crashed ===" $stderr.flush ensure $stderr.puts "=== Daemon Stopping @ #{Time.now.to_s} ===" $stderr.flush end end end |
#start ⇒ Object
This function starts the daemon process in the background.
99 100 101 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 |
# File 'lib/process/daemon/controller.rb', line 99 def start @output.puts Rainbow("Starting #{@daemon.name} daemon...").blue case ProcessFile.status(@daemon) when :running @output.puts Rainbow("Daemon already running!").blue return when :stopped # We are good to go... else @output.puts Rainbow("Daemon in unknown state! Will clear previous state and continue.").red ProcessFile.clear(@daemon) end spawn sleep 0.1 timer = TIMEOUT pid = ProcessFile.recall(@daemon) while pid == nil and timer > 0 # Wait a moment for the forking to finish... @output.puts Rainbow("Waiting for daemon to start (#{timer}/#{TIMEOUT})").blue sleep 1 # If the @daemon has crashed, it is never going to start... break if @daemon.crashed? pid = ProcessFile.recall(@daemon) timer -= 1 end end |
#status ⇒ Object
Prints out the status of the daemon
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/process/daemon/controller.rb', line 134 def status daemon_state = ProcessFile.status(@daemon) case daemon_state when :running @output.puts Rainbow("Daemon status: running pid=#{ProcessFile.recall(@daemon)}").green when :unknown if @daemon.crashed? @output.puts Rainbow("Daemon status: crashed").red @output.flush @output.puts Rainbow("Dumping daemon crash log:").red @daemon.tail_log(@output) else @output.puts Rainbow("Daemon status: unknown").red end when :stopped @output.puts Rainbow("Daemon status: stopped").blue end return daemon_state end |
#stop ⇒ Object
Stops the daemon process.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/process/daemon/controller.rb', line 165 def stop @output.puts Rainbow("Stopping #{@daemon.name} daemon...").blue # Check if the pid file exists... unless File.file?(@daemon.process_file_path) @output.puts Rainbow("Pid file not found. Is the daemon running?").red return end pid = ProcessFile.recall(@daemon) # Check if the @daemon is already stopped... unless ProcessFile.running(@daemon) @output.puts Rainbow("Pid #{pid} is not running. Has daemon crashed?").red @daemon.tail_log($stderr) return end # Interrupt the process group: pgid = -Process.getpgid(pid) Process.kill("INT", pgid) (@stop_timeout / STOP_PERIOD).to_i.times do sleep STOP_PERIOD if ProcessFile.running(@daemon) end # Kill/Term loop - if the @daemon didn't die easily, shoot # it a few more times. attempts = 5 while ProcessFile.running(@daemon) and attempts > 0 sig = (attempts <= 2) ? "KILL" : "TERM" @output.puts Rainbow("Sending #{sig} to process group #{pgid}...").red Process.kill(sig, pgid) attempts -= 1 sleep 1 end # If after doing our best the @daemon is still running (pretty odd)... if ProcessFile.running(@daemon) @output.puts Rainbow("Daemon appears to be still running!").red return end # Otherwise the @daemon has been stopped. ProcessFile.clear(@daemon) end |