def finish_terminating(timeout)
      if @sender
        request_count, request_age = @sender.terminate

        finish = lambda do
          request_count, request_age = @sender.terminate
          Log.info("[stop] The following #{request_count} requests initiated as recently as #{request_age} " +
                   "seconds ago are being dropped:\n  " + @sender.dump_requests.join("\n  ")) if request_age
          if @mode == :http
            @terminate_callback.call
          else
            @client.close { @terminate_callback.call }
          end
        end

        if (wait_time = [timeout - (request_age || timeout), 0].max) > 0
          Log.info("[stop] Termination waiting #{wait_time} seconds for completion of #{request_count} " +
                   "requests initiated as recently as #{request_age} seconds ago")
          @termination_timer = EM::Timer.new(wait_time) do
            begin
              Log.info("[stop] Continuing with termination")
              finish.call
            rescue Exception => e
              ErrorTracker.log(self, "Failed while finishing termination", e)
              begin @terminate_callback.call; rescue Exception; end
            end
          end
        else
          finish.call
        end
      else
        @terminate_callback.call
      end
      @history.update("graceful exit")
      true
    end
def finish_terminating(timeout)
      if @sender
        request_count, request_age = @sender.terminate

        finish = lambda do
          request_count, request_age = @sender.terminate
          Log.info("[stop] The following #{request_count} requests initiated as recently as #{request_age} " +
                   "seconds ago are being dropped:\n  " + @sender.dump_requests.join("\n  ")) if request_age
          if @mode == :http
            @terminate_callback.call
          else
            @client.close { @terminate_callback.call }
          end
        end

        if (wait_time = [timeout - (request_age || timeout), 0].max) > 0
          Log.info("[stop] Termination waiting #{wait_time} seconds for completion of #{request_count} " +
                   "requests initiated as recently as #{request_age} seconds ago")
          @termination_timer = EM::Timer.new(wait_time) do
            begin
              Log.info("[stop] Continuing with termination")
              finish.call
            rescue Exception => e
              ErrorTracker.log(self, "Failed while finishing termination", e)
              begin @terminate_callback.call; rescue Exception; end
            end
          end
        else
          finish.call
        end
      else
        @terminate_callback.call
      end
      @history.update("graceful exit")
      true
    end
Finish termination after all requests have been processed

 === Parameters
 timeout(Integer):: Maximum number of seconds to wait after last request received before
   terminating regardless of whether there are still unfinished requests

 === Return
 true:: Always return true