onStart method

  1. @override
Future<bool> onStart()
override

Callback when this executor is started. Returns true if successfully started, false otherwise.

Implementation

@override
Future<bool> onStart() async {
  if (stream == null) {
    warning(
        "Trying to start the stream probe '$runtimeType' which does not provide a measurement stream. "
        'Have you initialized this probe correctly?');
    return false;
  } else {
    Duration? interval = samplingConfiguration?.interval;
    Duration? duration = samplingConfiguration?.duration;
    if (interval != null && duration != null) {
      // create a recurrent timer that starts sampling
      _timer = Timer.periodic(interval, (timer) {
        _subscription =
            stream!.listen(_onData, onError: _onError, onDone: _onDone);
        // create a timer that stops the sampling after the specified duration.
        Timer(duration, () async => await _subscription?.cancel());
      });
    } else {
      warning(
          '$runtimeType - no valid interval and duration found in sampling configuration: $samplingConfiguration. '
          'Is a valid PeriodicSamplingConfiguration provided?');
    }
  }
  return true;
}