示例#1
0
        protected override async Task ExecuteAsync(CancellationToken stopCancellationToken)
        {
            while (!stopCancellationToken.IsCancellationRequested)
            {
                try
                {
                    var currentBlock = await BlockHeightTracker.GetCurrentBlockHeightAsync(stopCancellationToken).ConfigureAwait(false);

                    if (currentBlock == null || !long.TryParse(currentBlock.Height, out var height) || height <= 0)
                    {
                        continue;
                    }
                    height = height - 1;
                    long nextBlockHeight = 0;
                    using (var scope = ServiceScopeFactory.CreateScope())
                    {
                        var db        = scope.ServiceProvider.GetRequiredService <PoolContext>();
                        var nextBlock = await db.Blocks.Where(i => !db.BlockStates.Any(x => x.Height == i.Height) && i.Height < height).OrderBy(i => i.Height).FirstOrDefaultAsync(stopCancellationToken);

                        if (nextBlock != null)
                        {
                            nextBlockHeight = nextBlock.Height;
                        }
                    }
                    if (nextBlockHeight > 0)
                    {
                        await ProcessBlockStateAsync(nextBlockHeight, stopCancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        lock (_resetEvent)
                            if (_resetEvent.IsSet)
                            {
                                _resetEvent.Reset();
                            }
                        while (!_resetEvent.IsSet)
                        {
                            stopCancellationToken.ThrowIfCancellationRequested();
                            if (_resetEvent.Wait(10000))
                            {
                                break;
                            }
                        }
                    }
                }
                catch (OperationCanceledException) when(stopCancellationToken.IsCancellationRequested)
                {
                    return;
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "An unhandled exception occured while trying to update block state, will retry in 30 seconds.");
                    await Task.Delay(TimeSpan.FromSeconds(30), stopCancellationToken).ConfigureAwait(false);
                }
            }
        }
        protected override async Task ExecuteAsync(CancellationToken stopCancellationToken)
        {
            long lastPassHeight = 0;

            while (!stopCancellationToken.IsCancellationRequested)
            {
                try
                {
                    if (!_resetEvent.IsSet)
                    {
                        _resetEvent.Wait(TimeSpan.FromMinutes(1), stopCancellationToken);
                    }
                    var currentBlock = await BlockHeightTracker.GetCurrentBlockHeightAsync(stopCancellationToken);

                    var currentPassHeight = long.Parse(currentBlock.Height);
                    if (!await HandleCurrentPassAsync(currentPassHeight - 1, stopCancellationToken) && currentPassHeight == lastPassHeight)
                    {
                        var blockAfterPass = await BlockHeightTracker.GetCurrentBlockHeightAsync(stopCancellationToken);

                        if (blockAfterPass.Height == currentBlock.Height)
                        {
                            lock (_resetEvent)
                                if (_resetEvent.IsSet)
                                {
                                    _resetEvent.Reset();
                                }
                        }
                    }
                    lastPassHeight = currentPassHeight;
                }
                catch (OperationCanceledException) when(stopCancellationToken.IsCancellationRequested)
                {
                    return;
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "An unhandled exception occured processing account history, will try again");
                }
            }
        }