lib/pigeon/support.rb
def daemonize(logger = nil)
    delay = 10
    rfd, wfd = IO.pipe
    
    forked_pid = fork do
      rfd.close

      supervisor_pid = fork do
        relaunch = true
        
        while (relaunch)
          daemon_pid = fork do
            begin
              yield
            rescue SystemExit
              # Forced exit from supervisor process
            rescue Object => e
              if (logger)
                logger.error("Terminated with Exception: [#{e.class}] #{e}")
                logger.error(e.backtrace.join("\n"))

                Thread.list.each do |thread|
                  logger.error("Stack trace of current threads")
                  logger.error(thread.inspect)
                  
                  if (thread.backtrace)
                    logger.error("\t" + thread.backtrace.join("\n\t"))
                  end
                end
              end

              exit(-1)
            end
          end

          begin
            interrupted = false

            Signal.trap('INT') do
              interrupted = true
              Process.kill('INT', daemon_pid)

              relaunch = false
            end

            _, status = Process.wait2(daemon_pid)

            if (interrupted)
              logger.info("Supervisor #{Process.pid} received termination signal, shut down child #{daemon_pid}.")
            end

            # A non-zero exit status indicates some sort of error, so the
            # process will be relaunched after a short delay.
            relaunch = (status != 0)

          ensure
            # Reset Signal handler before forking again
            Signal.trap('INT') do
            end
          end
          
          if (relaunch)
            begin
              logger.info("Supervisor #{Process.pid} will relaunch in %d seconds" % delay)
              sleep(delay)

            rescue Interrupt
              logger.info("Supervisor #{Process.pid} abandoing restart because of termination")

              relaunch = false
            end
          else
            logger.info("Terminated normally.")
          end
        end
      end

      wfd.puts(supervisor_pid)
      wfd.flush
      wfd.close
    end

    Process.wait2(forked_pid)

    daemon_pid = rfd.readline
    rfd.close
    
    daemon_pid.to_i
  end
# Uses the double-fork method to create a fully detached background
# process. Returns the process ID of the created process. May throw an
# exception if these processes could not be created.
Uses the double-fork method to create a fully detached background
process. Returns the process ID of the created process. May throw an
exception if these processes could not be created.
Uses the double - fork method to create a fully detached background process . Returns the process ID of the created process . May throw an exception if these processes could not be created .
def daemonize(logger = nil)
    delay = 10
    rfd, wfd = IO.pipe
    forked_pid = fork do
      rfd.close
      supervisor_pid = fork do
        relaunch = true
        while (relaunch)
          daemon_pid = fork do
            begin
              yield
            rescue SystemExit
            rescue Object => e
              if (logger)
                logger.error("Terminated with Exception: [#{e.class}] #{e}")
                logger.error(e.backtrace.join("\n"))
                Thread.list.each do |thread|
                  logger.error("Stack trace of current threads")
                  logger.error(thread.inspect)
                  if (thread.backtrace)
                    logger.error("\t" + thread.backtrace.join("\n\t"))
                  end
                end
              end
              exit(-1)
            end
          end
          begin
            interrupted = false
            Signal.trap('INT') do
              interrupted = true
              Process.kill('INT', daemon_pid)
              relaunch = false
            end
            _, status = Process.wait2(daemon_pid)
            if (interrupted)
              logger.info("Supervisor #{Process.pid} received termination signal, shut down child #{daemon_pid}.")
            end
            relaunch = (status != 0)
          ensure
            Signal.trap('INT') do
            end
          end
          if (relaunch)
            begin
              logger.info("Supervisor #{Process.pid} will relaunch in %d seconds" % delay)
              sleep(delay)
            rescue Interrupt
              logger.info("Supervisor #{Process.pid} abandoing restart because of termination")
              relaunch = false
            end
          else
            logger.info("Terminated normally.")
          end
        end
      end
      wfd.puts(supervisor_pid)
      wfd.flush
      wfd.close
    end
    Process.wait2(forked_pid)
    daemon_pid = rfd.readline
    rfd.close
    daemon_pid.to_i
  end
def daemonize ( logger = nil ) delay = 10 rfd , wfd = IO . pipe forked_pid = fork do rfd . close supervisor_pid = fork do relaunch = true while ( relaunch ) daemon_pid = fork do begin yield rescue SystemExit rescue Object => e if ( logger ) logger . error ( "Terminated with Exception: [#{e.class}] #{e}" ) logger . error ( e . backtrace . join ( "\n" ) ) Thread . list . each do | thread | logger . error ( "Stack trace of current threads" ) logger . error ( thread . inspect ) if ( thread . backtrace ) logger . error ( "\t" + thread . backtrace . join ( "\n\t" ) ) end end end exit ( - 1 ) end end begin interrupted = false Signal . trap ( 'INT' ) do interrupted = true Process . kill ( 'INT' , daemon_pid ) relaunch = false end _ , status = Process . wait2 ( daemon_pid ) if ( interrupted ) logger . info ( "Supervisor #{Process.pid} received termination signal, shut down child #{daemon_pid}." ) end relaunch = ( status != 0 ) ensure Signal . trap ( 'INT' ) do end end if ( relaunch ) begin logger . info ( "Supervisor #{Process.pid} will relaunch in %d seconds" % delay ) sleep ( delay ) rescue Interrupt logger . info ( "Supervisor #{Process.pid} abandoing restart because of termination" ) relaunch = false end else logger . info ( "Terminated normally." ) end end end wfd . puts ( supervisor_pid ) wfd . flush wfd . close end Process . wait2 ( forked_pid ) daemon_pid = rfd . readline rfd . close daemon_pid . to_i end
Uses the double-fork method to create a fully detached background
process.
Uses the double - fork method to create a fully detached background process .
# Forced exit from supervisor process # A non-zero exit status indicates some sort of error, so the # process will be relaunched after a short delay. # Reset Signal handler before forking again
logger