Class: LogicalConstruct::Target::FlightDeck::DaemonizedResolutionServerTask

Inherits:
ResolutionServerTask
  • Object
show all
Defined in:
lib/logical-construct/target/flight-deck.rb

Instance Method Summary collapse

Methods inherited from ResolutionServerTask

#needed?, #resolve_configuration

Instance Method Details

#action(args) ⇒ Object



103
104
105
# File 'lib/logical-construct/target/flight-deck.rb', line 103

def action(args)
  daemonize{ super }
end

#daemonizeObject



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
97
98
99
100
101
# File 'lib/logical-construct/target/flight-deck.rb', line 69

def daemonize
  fail "Can't daemonize without a block" unless block_given?

  pid = fork do
    begin
      Process::setsid
      ObjectSpace::each_object(IO) do |io|
        begin
          if (0..2).cover?(io.fileno)
            begin
              io.reopen("/dev/null")
            rescue IOError => ioe
              raise "#{ioe.inspect} while trying to reopen #{io}"
            end
          else
            io.close
          end
        rescue IOError
          #io errors when closing or fileno aren't a problem
        end
      end

      yield
    rescue Object => ex
      File::open("daemonize-crash-log", "w") do |log|
        log.write("#{([ex.class.name, ex.message, ex.to_s] + ex.backtrace).join("\n")}")
      end
    end

    Kernel.exit!
  end
  Process.detach(pid)
end