Exemplo n.º 1
0
        public virtual async Task Start()
        {
            await WorkStatusSemaphore.WaitAsync();

            try
            {
                if (Status == WorkerStatus.Running)
                {
                    return;
                }

                Logger?.LogDebug($"{GetType().Name}: Started");

                WorkerCancellationTokenSource = new CancellationTokenSource();
                using (ExecutionContext.SuppressFlow())
                {
                    WorkerThread = new Thread(async() =>
                    {
                        var cbPolicy = RetryManager.CircuitBreakerWithRetryAsync <Exception>(
                            WaitStrategy,
                            (exception, span) => Logger?.LogError($"{GetType().Name}: Unhandled exception occured", exception),
                            (exception, span) => Logger?.LogCritical($"{GetType().Name}: Circuit breaker tripped.", exception),
                            () => Logger?.LogInformation($"{GetType().Name}: Circuit breaker restored."));
                        await cbPolicy.ExecuteAsync(async() =>
                                                    await StartWorkItemProcessingAsync(WorkerCancellationTokenSource.Token)
                                                    );
                    });

                    WorkerThread.Start();
                    Status = WorkerStatus.Running;
                }
            }
            finally
            {
                WorkStatusSemaphore.Release();
            }
        }