# File lib/spawn.rb, line 88
  def fork_it(options)
    # The problem with rails is that it only has one connection (per class),
    # so when we fork a new process, we need to reconnect.
    @@logger.debug "spawn> parent PID = #{Process.pid}"
    child = fork do
      begin
        start = Time.now
        @@logger.debug "spawn> child PID = #{Process.pid}"

        # set the nice priority if needed
        Process.setpriority(Process::PRIO_PROCESS, 0, options[:nice]) if options[:nice]

        # disconnect from the listening socket, et al
        Spawn.close_resources
        # get a new connection so the parent can keep the original one
        ActiveRecord::Base.spawn_reconnect

        # run the block of code that takes so long
        yield

      rescue => ex
        @@logger.error "spawn> Exception in child[#{Process.pid}] - #{ex.class}: #{ex.message}"
      ensure
        begin
          # to be safe, catch errors on closing the connnections too
          if RAILS_2_2
            ActiveRecord::Base.connection_handler.clear_all_connections!
          else
            ActiveRecord::Base.connection.disconnect!
            ActiveRecord::Base.remove_connection
          end
        ensure
          @@logger.info "spawn> child[#{Process.pid}] took #{Time.now - start} sec"
          # ensure log is flushed since we are using exit!
          @@logger.flush if @@logger.respond_to?(:flush)
          # this form of exit doesn't call at_exit handlers
          exit!(0)
        end
      end
    end

    # detach from child process (parent may still wait for detached process if they wish)
    Process.detach(child)

    return SpawnId.new(:fork, child)
  end