def terminate(reason = nil, exception = nil)
      begin
        @history.update("stop") if @history
        ErrorTracker.log(self, "[stop] Terminating because #{reason}", exception) if reason
        if exception.is_a?(Exception)
          h = @history.analyze_service
          if h[:last_crashed]
            delay = [(Time.now.to_i - h[:last_crash_time]) * 2, MAX_ABNORMAL_TERMINATE_DELAY].min
            Log.info("[stop] Delaying termination for #{RightSupport::Stats.elapsed(delay)} to slow crash cycling")
            sleep(delay)
          end
        end
        if @terminating || @client.nil?
          @terminating = true
          @termination_timer.cancel if @termination_timer
          @termination_timer = nil
          Log.info("[stop] Terminating immediately")
          @terminate_callback.call
          @history.update("graceful exit") if @history && @client.nil?
        else
          @terminating = true
          @check_status_timer.cancel if @check_status_timer
          @check_status_timer = nil
          Log.info("[stop] Agent #{@identity} terminating")
          stop_gracefully(@options[:grace_timeout])
        end
      rescue StandardError => e
        ErrorTracker.log(self, "Failed to terminate gracefully", e)
        begin @terminate_callback.call; rescue Exception; end
      end
      true
    end
def terminate(reason = nil, exception = nil)
      begin
        @history.update("stop") if @history
        ErrorTracker.log(self, "[stop] Terminating because #{reason}", exception) if reason
        if exception.is_a?(Exception)
          h = @history.analyze_service
          if h[:last_crashed]
            delay = [(Time.now.to_i - h[:last_crash_time]) * 2, MAX_ABNORMAL_TERMINATE_DELAY].min
            Log.info("[stop] Delaying termination for #{RightSupport::Stats.elapsed(delay)} to slow crash cycling")
            sleep(delay)
          end
        end
        if @terminating || @client.nil?
          @terminating = true
          @termination_timer.cancel if @termination_timer
          @termination_timer = nil
          Log.info("[stop] Terminating immediately")
          @terminate_callback.call
          @history.update("graceful exit") if @history && @client.nil?
        else
          @terminating = true
          @check_status_timer.cancel if @check_status_timer
          @check_status_timer = nil
          Log.info("[stop] Agent #{@identity} terminating")
          stop_gracefully(@options[:grace_timeout])
        end
      rescue StandardError => e
        ErrorTracker.log(self, "Failed to terminate gracefully", e)
        begin @terminate_callback.call; rescue Exception; end
      end
      true
    end
Gracefully terminate execution by allowing unfinished tasks to complete
 Immediately terminate if called a second time
 Report reason for termination if it is abnormal

 === Parameters
 reason(String):: Reason for abnormal termination, if any
 exception(Exception|String):: Exception or other parenthetical error information, if any

 === Return
 true:: Always return true