Beispiel #1
0
        protected override int Process()
        {
            _logger.Trace("Trying to receive next task to scan...");

            CheckVersion();

            var task = GetNextTask();

            if (task == null)
            {
                _logger.Trace("There are no task to scan in queue");

                return(0);
            }

            _logger.Trace($"Task received. TaskId='{task.Id}'");

            var results = new List <ScanTaskResultDto>();

            var localParameters = ConfigurationManager
                                  .AppSettings
                                  .AllKeys.ToDictionary(
                appSettingKey => appSettingKey,
                appSettingKey => ConfigurationManager.AppSettings[appSettingKey]);

            foreach (var core in task.Cores)
            {
                var manager = _pluginProvider.GetByKey(core.Core);

                if (manager == null)
                {
                    results.Add(
                        new ScanTaskResultDto
                    {
                        Id     = task.Id,
                        Status = ScanStatus.Failed
                    });
                    _logger.Error(Resources.CoreManagerIsNotFound.FormatWith(core.Core, task.Id));

                    continue;
                }

                CancellationTokenSource source;

                var executionTask = manager.Run(task.Path, core.CodeLocation, core.CoreParameters, localParameters, out source);

                while (!executionTask.Wait(Settings.Default.WaitCheckTaskCancelledTimeout))
                {
                    // ReSharper disable once InvertIf
                    if (IsTaskCancelled(task.Id))
                    {
                        source.Cancel();

                        _logger.Info($"Task execution cancelled. TaskId='{task.Id}'");
                    }
                }

                results.Add(executionTask.Result);
            }

            if (!results.Any())
            {
                SendResults(
                    new ScanTaskResultDto
                {
                    Id     = task.Id,
                    Status = ScanStatus.Failed
                });

                return(1);
            }

            var result = new ScanTaskResultDto
            {
                Id                  = task.Id,
                AnalyzedFiles       = string.Join("\n", results.Select(_ => _.AnalyzedFiles)),
                Status              = ScanStatus.Success,
                AnalyzedLinesCount  = results.Sum(_ => _.AnalyzedLinesCount),
                AnalyzedSizeInBytes = results.Sum(_ => _.AnalyzedSizeInBytes),
                CoreRunTime         = new TimeSpan(results.Sum(_ => _.CoreRunTime.Ticks)),
                LogPath             = results.First().LogPath,
                ResultPath          = results.First().ResultPath
            };

            SendResults(result);

            _logger.Info($"Task successfully processed. TaskId='{task.Id}', ResultPath='{result.ResultPath}'");

            return(1);
        }