Class: AlchemyServer::ProcessHelper
- Inherits:
-
Object
- Object
- AlchemyServer::ProcessHelper
- Defined in:
- lib/alchemy/runner.rb
Instance Method Summary collapse
- #close_io_handles ⇒ Object
- #daemonize ⇒ Object
- #detach_from_terminal ⇒ Object
-
#initialize(log_file = nil, pid_file = nil, user = nil, group = nil) ⇒ ProcessHelper
constructor
A new instance of ProcessHelper.
- #redirect_io ⇒ Object
- #remove_pid_file ⇒ Object
- #rescue_exception ⇒ Object
- #running? ⇒ Boolean
- #safefork ⇒ Object
- #write_pid_file ⇒ Object
Constructor Details
#initialize(log_file = nil, pid_file = nil, user = nil, group = nil) ⇒ ProcessHelper
Returns a new instance of ProcessHelper.
148 149 150 151 152 153 |
# File 'lib/alchemy/runner.rb', line 148 def initialize(log_file = nil, pid_file = nil, user = nil, group = nil) @log_file = log_file @pid_file = pid_file @user = user @group = group end |
Instance Method Details
#close_io_handles ⇒ Object
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/alchemy/runner.rb', line 192 def close_io_handles ObjectSpace.each_object(IO) do |io| unless [STDIN, STDOUT, STDERR].include?(io) begin io.close unless io.closed? rescue Exception end end end end |
#daemonize ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/alchemy/runner.rb', line 166 def daemonize sess_id = detach_from_terminal exit if pid = safefork Dir.chdir("/") File.umask 0000 close_io_handles redirect_io return sess_id end |
#detach_from_terminal ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/alchemy/runner.rb', line 179 def detach_from_terminal srand safefork and exit unless sess_id = Process.setsid raise "Couldn't detache from controlling terminal." end trap 'SIGHUP', 'IGNORE' sess_id end |
#redirect_io ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/alchemy/runner.rb', line 203 def redirect_io begin; STDIN.reopen('/dev/null'); rescue Exception; end if @log_file begin STDOUT.reopen(@log_file, "a") STDOUT.sync = true rescue Exception begin; STDOUT.reopen('/dev/null'); rescue Exception; end end else begin; STDOUT.reopen('/dev/null'); rescue Exception; end end begin; STDERR.reopen(STDOUT); rescue Exception; end STDERR.sync = true end |
#remove_pid_file ⇒ Object
234 235 236 237 |
# File 'lib/alchemy/runner.rb', line 234 def remove_pid_file return unless @pid_file File.unlink(@pid_file) if File.exists?(@pid_file) end |
#rescue_exception ⇒ Object
221 222 223 224 225 226 |
# File 'lib/alchemy/runner.rb', line 221 def rescue_exception begin yield rescue Exception end end |
#running? ⇒ Boolean
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/alchemy/runner.rb', line 239 def running? return false unless @pid_file pid = File.read(@pid_file).chomp.to_i rescue nil pid = nil if pid == 0 return false unless pid begin Process.kill(0, pid) return pid rescue Errno::ESRCH return nil rescue Errno::EPERM return pid end end |
#safefork ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/alchemy/runner.rb', line 155 def safefork begin if pid = fork return pid end rescue Errno::EWOULDBLOCK sleep 5 retry end end |
#write_pid_file ⇒ Object
228 229 230 231 232 |
# File 'lib/alchemy/runner.rb', line 228 def write_pid_file return unless @pid_file File.open(@pid_file, "w") { |f| f.write(Process.pid) } File.chmod(0644, @pid_file) end |