Exemplo n.º 1
0
        public async Task DataflowExecutionEngineAsync(Func <ReceivedData, Task <bool> > workBodyAsync, int maxDoP = 4, CancellationToken token = default)
        {
            await dataFlowExecLock.WaitAsync(2000).ConfigureAwait(false);

            try
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                var dataflowEngine = new DataflowEngine(workBodyAsync, maxDoP);

                while (await DataBuffer.Reader.WaitToReadAsync().ConfigureAwait(false))
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }

                    try
                    {
                        while (DataBuffer.Reader.TryRead(out var receivedData))
                        {
                            if (receivedData != null)
                            {
                                _logger.LogDebug(
                                    LogMessages.Consumer.ConsumerDataflowQueueing,
                                    ConsumerSettings.ConsumerName,
                                    receivedData.DeliveryTag);

                                await dataflowEngine
                                .EnqueueWorkAsync(receivedData)
                                .ConfigureAwait(false);
                            }
                        }

                        await Task
                        .Delay(ConsumerSettings.SleepOnIdleInterval.Value)
                        .ConfigureAwait(false);
                    }
                    catch { }
                }
            }
            catch { }
            finally { dataFlowExecLock.Release(); }
        }
Exemplo n.º 2
0
        public async Task DataflowExecutionEngineAsync(Func <ReceivedData, Task <bool> > workBodyAsync, int maxDoP = 4, bool ensureOrdered = true, CancellationToken token = default)
        {
            await _dataFlowExecLock.WaitAsync(2000).ConfigureAwait(false);

            try
            {
                var dataflowEngine = new DataflowEngine <ReceivedData, bool>(workBodyAsync, maxDoP, ensureOrdered);

                while (await _dataBuffer.Reader.WaitToReadAsync(token).ConfigureAwait(false))
                {
                    while (_dataBuffer.Reader.TryRead(out var receivedData))
                    {
                        if (receivedData != null)
                        {
                            _logger.LogDebug(
                                LogMessages.Consumers.ConsumerDataflowQueueing,
                                ConsumerOptions.ConsumerName,
                                receivedData.DeliveryTag);

                            await dataflowEngine
                            .EnqueueWorkAsync(receivedData)
                            .ConfigureAwait(false);
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                _logger.LogWarning(
                    LogMessages.Consumers.ConsumerDataflowActionCancelled,
                    ConsumerOptions.ConsumerName);
            }
            catch (Exception ex)
            {
                _logger.LogError(
                    LogMessages.Consumers.ConsumerDataflowError,
                    ConsumerOptions.ConsumerName,
                    ex.Message);
            }
            finally { _dataFlowExecLock.Release(); }
        }