startReport static method

Future<void> startReport()

Implementation

static Future<void> startReport() async {
  if (_timer != null) {
    _timer?.cancel();
  }
  TCICLog.info(
    "start report timer $reportTimerInterval ms",
    actionModule: ActionModule.report.name,
    actionName: ActionName.report.name,
  );
  _timer = Timer.periodic(Duration(milliseconds: reportTimerInterval), (
    timer,
  ) async {
    await report();
    // 根据剩余未上报日志调试,动态调整上报间隔,日志太多,应减小上报间隔,确保及时上报,每多200条日志,上报间隔少500ms
    final remainingLogs = _cache.length;
    if (remainingLogs > 0) {
      int latestReportTimerInterval =
          reportTimerInterval - (remainingLogs ~/ 200 * 500);
      if (latestReportTimerInterval < minReportTimerInterval) {
        latestReportTimerInterval = minReportTimerInterval;
      }
      if (remainingLogs < batchSize) {
        latestReportTimerInterval = 2000; // 如果剩余日志量小于batchSize,则上报间隔为2000ms
      }
      // 如果reportTimerInterval 没有改变,则不重新启动定时器
      if (latestReportTimerInterval != reportTimerInterval) {
        reportTimerInterval = latestReportTimerInterval;
        await startReport();
      }
    }
  });
}