Example #1
0
        public void OnError(Exception exception, CancellationToken cancellationToken)
        {
            _logger.Error(LogTag, exception);

            Stop();
            Listen(cancellationToken);
        }
Example #2
0
        public void Stop()
        {
            _tokenSource.Cancel();

            try
            {
                if (_preempter != null)
                {
                    _logger.Debug(LogTag, "Waiting for preempter to stop...");
                    _preempter.Stop();
                }

                _logger.Debug(LogTag, "Waiting for task(s) to end...");

                Task.WaitAll(_tasks.ToArray(), _tokenSource.Token);
            }
            catch (OperationCanceledException) { }
            catch (AggregateException e)
            {
                foreach (var exception in e.WithoutCancellations().InnerExceptions)
                {
                    _logger.Error(LogTag, exception);
                }
            }

            OnStopped();

            _logger.Debug(LogTag, "Stopped.");
        }
Example #3
0
        public async Task TryAction(Func <Task> action, IMonitoredActivity activity, int attempts = 0)
        {
            try
            {
                await action().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.Error(ex);

                var handler      = _handling.DetermineAction(ex, activity);
                var continuation = await handler.Handle(ex, attempts + 1, activity);

                switch (continuation)
                {
                case ExceptionAction.Retry:
                    await TryAction(action, activity, attempts + 1);

                    break;

                case ExceptionAction.Pause:
                    await pause(activity).ConfigureAwait(false);

                    break;

                case ExceptionAction.Stop:
                    await stop(activity).ConfigureAwait(false);

                    break;

                case ExceptionAction.StopAll:
                    await stopAll().ConfigureAwait(false);

                    break;
                }
            }
        }