Пример #1
0
        public List <SystemTaskResult> Execute()
        {
            IReadOnlyCollection <Run> GetRunsToDeleteForLastNDays(DateTime minCreatedDate)
            {
                using (var scope = _repositoryFactory.BeginRepositoryScope())
                {
                    var runRepository = scope.CreateRepository <IRunRepository>();

                    return(runRepository.GetAll()
                           .Where(r => r.CreatedDateTime.Year > 2000 && r.CreatedDateTime < minCreatedDate)
                           .OrderBy(r => r.CreatedDateTime)
                           .ToList());
                }
            }

            DateTime minCreated            = DateTime.UtcNow.Subtract(_runRetention);
            IReadOnlyCollection <Run> runs = GetRunsToDeleteForLastNDays(minCreated);

            var results = new List <SystemTaskResult>();

            foreach (var run in runs)
            {
                _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0,
                                                                                                     $"Deleting run {run.Id} ({run.Description})"
                                                                                                     ));

                try
                {
                    Run.ValidateForDelete(run);

                    _runManager.DeleteRun(run.Id);

                    _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForRunDeleted(0, 0, run.Id, null));
                    _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0,
                                                                                                         $"Deleted run {run.Id} ({run.Description})"
                                                                                                         ));
                }
                catch (Exception exception)
                {
                    results.Add(
                        new SystemTaskResult(SystemTaskResult.ResultTypes.Error, Id,
                                             $"Error deleting run {run.Id} ({run.Description}): {exception.Message}"
                                             )
                        );
                }
            }

            return(results);
        }