Example #1
0
        /// <summary>
        /// Add a command to the command table
        /// </summary>
        /// <param name="command">unique ID of command</param>
        /// <param name="command">command implementation</param>
        private void AddCommand(BrowserCommand commandID, IBrowserCommand command)
        {
            // verify we haven't already added this command
            Debug.Assert(!m_commands.Contains(commandID),
                         "Added a command that is already part of the command table");

            // insert the command into the table
            m_commands[commandID] = command;
        }
Example #2
0
        /// <summary>
        /// Execute a command
        /// </summary>
        /// <param name="command">unique ID of command</param>
        public void Execute(BrowserCommand command)
        {
            // verify that the command exists
            Debug.Assert(m_commands.Contains(command),
                         "Attempted to Execute a command that doesn't exist");

            // execute
            IBrowserCommand cmdExecute = (IBrowserCommand)m_commands[command];

            if (cmdExecute != null)
            {
                cmdExecute.Execute();
            }
        }
        public Task DoJob(IBrowserCommand command)
        {
            if (!isNeedSaveState)
            {
                _logger.LogTrace("BrowserPool stoped");
                _stateProvider.SaveAction(command);
                return(Task.CompletedTask);
            }
            else if ((_poolSettings.Value.QueueLimit - _actions.Count ?? 1) <= 0)
            {
                _logger.LogTrace("Queue overflowed");
                _stateProvider.SaveAction(command);
                return(Task.CompletedTask);
            }

            _actions.Enqueue(command);
            return(Task.CompletedTask);
        }
Example #4
0
        /// <summary>
        /// Determine if a command is enabled
        /// </summary>
        /// <param name="command">unique ID of command</param>
        /// <returns>true if the command is enabled, otherwise false</returns>
        public bool IsEnabled(BrowserCommand command)
        {
            // verify that the command exists
            Debug.Assert(m_commands.Contains(command),
                         "Attempted to QueryStatus on a command that doesn't exist");

            // query status
            IBrowserCommand cmdStatus = (IBrowserCommand)m_commands[command];

            if (cmdStatus != null)
            {
                return(cmdStatus.Enabled);
            }
            else
            {
                return(false);
            }
        }
        private async Task <bool> SaveIfNotValidCommand(IBrowserCommand command)
        {
            var result = true;

            if (command is BeamCommand)
            {
                _logger.LogCritical(new InvalidCommandException(command), "Can't handle command in current method");
                result = false;
            }
            else if (command.RunNumber >= _poolSettings.Value.CommandMaxRuns)
            {
                await _stateProvider.SaveProblemAction(command, CommandProblem.TooManyRuns);

                result = false;
            }
            else if (command.CancellationToken.IsCancellationRequested)
            {
                await _stateProvider.SaveProblemAction(command, CommandProblem.OperationCancelled);

                result = false;
            }

            return(result);
        }
        /// <summary>
        /// Add a command to the command table
        /// </summary>
        /// <param name="command">unique ID of command</param>
        /// <param name="command">command implementation</param>
        private void AddCommand(BrowserCommand commandID, IBrowserCommand command)
        {
            // verify we haven't already added this command
            Debug.Assert(!m_commands.Contains(commandID),
                "Added a command that is already part of the command table");

            // insert the command into the table
            m_commands[commandID] = command;
        }
 public InvalidCommandException(IBrowserCommand command) : base(GetMessage(command))
 {
 }
 private static string GetMessage(IBrowserCommand command)
 => $"Invocation of {command.GetType().Name} was illegal in current context.";
        private async Task DoJob(BrowserWrapper wrapper, IBrowserCommand command, CancellationToken deactivateToken)
        {
            using var _ = _logger.BeginScope("{JobType} {CommandId} on {BrowserId}", command.GetType().Name, command.Id, wrapper._id);
            _logger.LogDebug("Start job");

            if (!await SaveIfNotValidCommand(command))
            {
                _browsers.Push(wrapper);
                return;
            }

            if (wrapper._isInWork)
            {
                _logger.LogError(new InvalidOperationException("Browser wrapper is in inconsistent state. Field name _isInWork."), "Illegal state occurred");
                _actions.Enqueue(command);
                return;
            }

            wrapper._isInWork = true;
            if (wrapper.CanBeStopped)
            {
                _logger.LogDebug("Current browser will be stopped. Reenqueue action.");
                _actions.Enqueue(command);
                wrapper._isInWork = false;
                return;
            }

            _logger.LogDebug("Validation completed");

            var localLoopCancel      = new CancellationTokenSource();
            var localLoopCancelToken = localLoopCancel.Token;
            var t = Task.Run(async() =>
            {
                while (true)
                {
                    if (localLoopCancelToken.IsCancellationRequested)
                    {
                        break;
                    }
                    else if (command.CancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                    else if (deactivateToken.IsCancellationRequested)
                    {
                        await _stateProvider.SaveAction(command);
                        break;
                    }

                    Thread.Sleep(200);
                }
            });

            try
            {
                await command.Execute(wrapper._driver, deactivateToken, _serviceProvider);
            }
            catch (Exception ex)
            {
                ++wrapper.Fails;
                _logger.LogError(ex, "Job fail", command.GetType().Name, wrapper._id);
                if (!deactivateToken.IsCancellationRequested)
                {
                    _actions.Enqueue(command);
                }
            }
            localLoopCancel.Cancel();
            wrapper._isInWork   = false;
            wrapper.LastJobTime = _dateTimeProvider.UtcNow;
            _logger.LogDebug("Finish job", command.GetType().Name, wrapper._id);

            if (wrapper.Fails < _poolSettings.Value.BrowserMaxFail)
            {
                _browsers.Push(wrapper);
            }
            else
            {
                wrapper.CanBeStopped = true;
            }
        }