Exemplo n.º 1
0
 private void OnExecutionException(Exception exception)
 {
     ExecutionException?.Invoke(this, new EventArgs <Exception>(exception));
 }
        internal async Task StartSubscribe()
        {
            var reader = TaskQueueReader;

            SubscriberStarted?.Invoke(this);
            while (true)
            {
                T newPayload;
                try
                {
                    newPayload = await reader.ReadAsync(TaskManagerCancellationToken.Token).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    break;
                }
                catch (Exception e)
                {
                    TaskPayloadFetchException?.Invoke(this, e);
                    continue;
                }

                var cts = TaskManagerCancellationToken;
                CancellationTokenSource localCts = null;
                var timeout = ExecutionTimeout;
                if (timeout.HasValue)
                {
                    localCts = new CancellationTokenSource(timeout.Value);
                    cts      = CancellationTokenSource.CreateLinkedTokenSource(
                        TaskManagerCancellationToken.Token,
                        localCts.Token);
                }
                try
                {
                    ExecutionStarting?.Invoke(this);
                    await Execution(newPayload, cts).ConfigureAwait(false);

                    ExecutionFinished?.Invoke(this);
                }
                catch (OperationCanceledException ex)
                {
                    if (localCts?.IsCancellationRequested ?? false)
                    {
                        ExecutionTimeoutEvent?.Invoke(this);
                    }
                    else
                    {
                        SubscriberCancelled?.Invoke(this, ex);
                        break;
                    }
                }
                catch (ObjectDisposedException ed)
                {
                    SubscriberCancelled?.Invoke(this, ed);
                    break;
                }
                catch (Exception e)
                {
                    ExecutionException?.Invoke(this, e);
                }

                if (TaskManagerCancellationToken.IsCancellationRequested)
                {
                    break;
                }
            }
            TaskSubscriberFinalized?.Invoke(this);
        }