Exemplo n.º 1
0
        private void SendBeam()
        {
            if (!_poolSettings.Value.SendBeamPackages ||
                _poolSettings.Value.BeamPackagesInterval <= TimeSpan.Zero)
            {
                return;
            }

            var browserToBeam = browsers
                                .FirstOrDefault(b => b.LastBeamTime == browsers
                                                .Where(x => !x._isInWork && !x.CanBeStopped)
                                                .Where(x => _dateTimeProvider.UtcNow - x.LastBeamTime > _poolSettings.Value.BeamPackagesInterval)
                                                .NullIfEmpty()
                                                ?.Min(x => x.LastBeamTime));

            if (browserToBeam == null)
            {
                return;
            }

            var beam = new BeamCommand();

            _browserPool.DoJob(browserToBeam, new BeamCommand())
            .ContinueWith(t =>
                          _logger.LogWarning(
                              t.Exception,
                              "Error on beam {CommandId} on {BrowserId}",
                              beam.Id,
                              browserToBeam._id),
                          TaskContinuationOptions.OnlyOnFaulted);
        }
        private async Task DoBeamJob(BrowserWrapper wrapper, BeamCommand command, CancellationToken deactivateToken)
        {
            using var _ = _logger.BeginScope("{JobType} {CommandId} on {BrowserId}", command.GetType().Name, command.Id, wrapper._id);
            _logger.LogDebug("Start job");

            if (wrapper._isInWork || deactivateToken.IsCancellationRequested)
            {
                return;
            }

            wrapper._isInWork = true;
            if (wrapper.CanBeStopped)
            {
                wrapper._isInWork = false;
                return;
            }

            try
            {
                await(command as IBrowserCommand).Execute(wrapper._driver, deactivateToken, _serviceProvider);
            }
            catch (Exception ex)
            {
                ++wrapper.Fails;
                _logger.LogError(ex, "Job fail", command.GetType().Name, wrapper._id);
            }
            wrapper._isInWork    = false;
            wrapper.LastBeamTime = _dateTimeProvider.UtcNow;

            if (wrapper.Fails >= _poolSettings.Value.BrowserMaxFail)
            {
                wrapper.CanBeStopped = true;
            }
        }
 Task IBrowserPoolAdvanced.DoJob(BrowserWrapper wrapper, BeamCommand command)
 => DoBeamJob(wrapper, command, _loopCancelTokenSource.Token);