Ejemplo n.º 1
0
 /// <summary>
 /// It is called dynamically.
 /// </summary>
 public async Task Execute(CancellationToken cancellationToken, AutoBook autoBook)
 {
     try
     {
         try
         {
             _autoBooks.Create(autoBook);
             _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0,
                                                                                                  $"Creating AutoBook instance (AutoBookID={autoBook.Id}): Waiting for Idle notification"));
         }
         catch (Exception ex)
         {
             _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0,
                                                                                         $"Error creating AutoBook (AutoBookID={autoBook.Id})", ex));
             autoBook.Status = AutoBookStatuses.Fatal_Error;
             _autoBookRepository.Update(autoBook);
         }
     }
     finally
     {
         foreach (var dbContext in _tenantDbContexts)
         {
             try
             {
                 await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
             }
             catch //ignore exception
             {
             }
         }
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Contructor
 /// </summary>
 /// <param name="autoBook">AutoBool details</param>
 /// <param name="autoBookRepository">AutoBook repository</param>
 /// <param name="scenarioResultRepository">ScenarioResult repository</param>
 /// <param name="outputFileRepository"></param>
 /// <param name="autoBookApi"></param>
 /// <param name="mapper">Mapper</param>
 /// <param name="resultsFilterRepository"></param>
 public AWSAutoBook(AutoBook autoBook,
                    IAutoBookRepository autoBookRepository,
                    IAutoBookAPI autoBookApi)
 {
     _autoBook           = autoBook;
     _autoBookRepository = autoBookRepository;
     _autoBookApi        = autoBookApi;
 }
 private AutoBook GetAutoBookAbleToDelete(List <AutoBook> autobooksOfTooSmallType)
 {
     foreach (AutoBook autobook in autobooksOfTooSmallType)
     {
         if (AutoBook.IsOKForDelete(autobook.Status))
         {
             return(autobook);
         }
     }
     return(null);
 }
        public AutoBook CreateAutoBookOfType(int type)
        {
            var newAutoBook = new AutoBook()
            {
                TimeCreated             = DateTime.UtcNow,
                Locked                  = false,
                Status                  = AutoBookStatuses.Provisioning,
                InstanceConfigurationId = type
            };

            _autoBooks.Create(newAutoBook);
            return(newAutoBook);
        }
Ejemplo n.º 5
0
        public void Update(AutoBook autoBook)
        {
            var autoBookId = AutoBookProfile.AutoBookCollectionIdToEntityAutoBookId(autoBook.Id);

            var entity = _dbContext.Query <Entities.Tenant.AutoBookApi.AutoBook>()
                         .Include(x => x.Task)
                         .FirstOrDefault(x => x.AutoBookId == autoBookId);

            if (entity != null)
            {
                _mapper.Map(autoBook, entity);
                _dbContext.Update(entity, post => post.MapTo(autoBook), _mapper);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// It is called dynamically.
        /// </summary>
        public async Task Execute(CancellationToken cancellationToken, AutoBook autoBook)
        {
            try
            {
                _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForInformationMessage(0, 0,
                                                                                                     $"Deleting AutoBook (AutoBookID={autoBook.Id})"));
                var autobookDeleted = false;
                try
                {
                    _autoBooks.Delete(autoBook);
                    autobookDeleted = true;
                }
                catch (Exception ex)
                {
                    _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0,
                                                                                                $"Error deleting AutoBook (AutoBookID={autoBook.Id})", ex));
                }

                if (autobookDeleted)
                {
                    _auditEventRepository.Insert(
                        AuditEventFactory.CreateAuditEventForInformationMessage(0, 0, "Deleted AutoBook"));
                }
            }
            finally
            {
                foreach (var dbContext in _tenantDbContexts)
                {
                    try
                    {
                        await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                    }
                    catch //ignore exception
                    {
                    }
                }
            }
        }
 public void DeleteAutoBook(AutoBook autoBook)
 {
     _autoBooks.Delete(autoBook);
     return;
 }
Ejemplo n.º 8
0
        public List <SystemTestResult> Execute(SystemTestCategories systemTestCategory)
        {
            var results = new List <SystemTestResult>();

            try
            {
                using (var scope = _repositoryFactory.BeginRepositoryScope())
                {
                    // Get list of AutoBooks
                    var autoBookRepository = scope.CreateRepository <IAutoBookRepository>();
                    var autoBooks          = autoBookRepository.GetAll();

                    // Check AutoBook provisioning settings
                    results.AddRange(ExecuteProvisioningTests(autoBooks.ToList()));

                    // Warn if no AutoBooks exist
                    if (autoBookRepository.GetAll().ToList().Count == 0)
                    {
                        if (!_autoBooks.Settings.AutoProvisioning)
                        {
                            results.Add(new SystemTestResult(SystemTestResult.ResultTypes.Error, _category, "No AutoBook data exists and auto-provisioning is disabled. It will not be possible to execute any runs.", ""));
                        }
                    }
                    else
                    {
                        // Check connectivity to each AutoBook
                        int countWorkingAutoBooks = 0;
                        int countFreeAutoBooks    = 0;

                        foreach (var autoBook in autoBooks)
                        {
                            try
                            {
                                IAutoBook autoBookInterface = _autoBooks.GetInterface(autoBook);
                                var       autoBookStatus    = autoBookInterface.GetStatus();
                                if (AutoBook.IsWorkingStatus(autoBookStatus))
                                {
                                    countWorkingAutoBooks++;
                                    if (autoBookStatus == AutoBookStatuses.Idle)
                                    {
                                        countFreeAutoBooks++;
                                    }
                                }
                                else if (autoBookStatus == AutoBookStatuses.Fatal_Error)
                                {
                                    if (!_autoBooks.Settings.AutoProvisioning)
                                    {
                                        results.Add(new SystemTestResult(SystemTestResult.ResultTypes.Warning, _category, string.Format("AutoBook {0} has indicated that is has encountered a fatal error. It cannot be used for any runs while it is in this state. Please manually restart it.", autoBook.Id), ""));
                                    }
                                }
                            }
                            catch
                            {
                                if (autoBook.Status != AutoBookStatuses.Provisioning)    // Ignore error if Provisioning
                                {
                                    results.Add(new SystemTestResult(SystemTestResult.ResultTypes.Warning, _category, string.Format("Error testing connection to AutoBook {0} ({1})", autoBook.Id, autoBook.Api), ""));
                                }
                            }
                        }

                        if (countWorkingAutoBooks == 0)
                        {
                            if (!_autoBooks.Settings.AutoProvisioning)
                            {
                                results.Add(new SystemTestResult(SystemTestResult.ResultTypes.Error, _category, "There are no working AutoBooks. It will not be possible to start any runs.", ""));
                            }
                        }
                        else if (countFreeAutoBooks == 0)
                        {
                            results.Add(new SystemTestResult(SystemTestResult.ResultTypes.Warning, _category, string.Format("All {0} working AutoBooks are currently executing a run. It will not be possible to start any runs until existing runs have completed.", countWorkingAutoBooks), ""));
                        }
                        else if (results.Count == 0)
                        {
                            results.Add(new SystemTestResult(SystemTestResult.ResultTypes.Information, _category, string.Format("AutoBook test OK ({0} working AutoBooks)", countWorkingAutoBooks), ""));
                        }
                    }
                }
            }
            catch (System.Exception exception)
            {
                results.Add(new SystemTestResult(SystemTestResult.ResultTypes.Error, _category, string.Format("Error checking AutoBooks: {0}", exception.Message), ""));
            }
            return(results);
        }
Ejemplo n.º 9
0
 public AWSAutoBookAPI(AutoBook autoBook, AutoBookSettings autoBookSettings, string accessToken)
 {
     _autoBook         = autoBook;
     _autoBookSettings = autoBookSettings;
     _accessToken      = accessToken;
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Starts run. Uploads input data and then instructs AutoBook instance to start
        /// </summary>
        /// <param name="autoBookInterface"></param>
        /// <param name="autoBook"></param>
        public void UploadInputFilesStartAutoBookRun(IAutoBook autoBookInterface, AutoBook autoBook)
        {
            using var scope = _repositoryFactory.BeginRepositoryScope();
            var runRepository      = scope.CreateRepository <IRunRepository>();
            var autoBookRepository = scope.CreateRepository <IAutoBookRepository>();

            bool startedRun = false;

            try
            {
                // Update scenario status to Starting, Scenario.StartedDateTime has already been set
                RunManager.UpdateScenarioStatuses(_repositoryFactory, _auditEventRepository, RunId,
                                                  new List <Guid>()
                {
                    ScenarioId
                }, new List <ScenarioStatuses>()
                {
                    ScenarioStatuses.Starting
                });

                // Get run
                var run = runRepository.Find(RunId);

                // Record which run/scenario is being processed
                //IAutoBookRepository autoBookRepository = (IAutoBookRepository)repositories[typeof(IAutoBookRepository)];
                //AutoBook localAutoBook = autoBookRepository.Find(autoBook.Id);
                lock (autoBook)
                {
                    autoBook.Status         = AutoBookStatuses.In_Progress;
                    autoBook.LastRunStarted = DateTime.UtcNow;
                    autoBook.Locked         = true;
                    autoBook.Task           = new AutoBookTask()
                    {
                        RunId      = RunId,
                        ScenarioId = ScenarioId
                    };
                }

                autoBookRepository.Update(autoBook);
                autoBookRepository.SaveChanges(); // Force save

                // Upload input data
                _autoBookInputHandler.Handle(run, ScenarioId);

                // Instruct AutoBook to start processing
                bool loggedNotifyFinished = false;
                try
                {
                    _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineStart(0, 0,
                                                                                                            PipelineEventIDs.STARTED_NOTIFYING_AUTOBOOK_API, RunId, ScenarioId, autoBook.Id, null));

                    GetAutoBookStatusModel autoBookStatusModel = autoBookInterface.StartAutoBookRun(RunId, ScenarioId);

                    _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun,
                                                                                                   PipelineEventIDs.STARTED_NOTIFYING_AUTOBOOK_API, RunId, ScenarioId, null));

                    if (autoBookStatusModel.Status == AutoBookStatuses.In_Progress) // Task_Error or Fatal_Error
                    {
                        _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineEnd(0, 0,
                                                                                                              PipelineEventIDs.FINISHED_NOTIFYING_AUTOBOOK_API, RunId, ScenarioId, autoBook.Id,
                                                                                                              null, null, null));

                        _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun,
                                                                                                       PipelineEventIDs.FINISHED_NOTIFYING_AUTOBOOK_API, RunId, ScenarioId, null));

                        loggedNotifyFinished = true;
                    }
                    else
                    {
                        _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineEnd(0, 0,
                                                                                                              PipelineEventIDs.FINISHED_NOTIFYING_AUTOBOOK_API, RunId, ScenarioId, autoBook.Id,
                                                                                                              null,
                                                                                                              String.Format(
                                                                                                                  "AutoBook API unexpectedly returned status {0} when instructing it to start run",
                                                                                                                  autoBookStatusModel.ToString()), null));

                        _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun,
                                                                                                       PipelineEventIDs.FINISHED_NOTIFYING_AUTOBOOK_API, RunId, ScenarioId,
                                                                                                       $"AutoBook API unexpectedly returned status {autoBookStatusModel.ToString()} " +
                                                                                                       "when instructing it to start run"));

                        loggedNotifyFinished = true;
                        throw new Exception(String.Format(
                                                "AutoBook returned status {0} when starting run (AutoBookID={1})",
                                                autoBookStatusModel.Status, autoBook.Id));
                    }
                }
                catch (System.Exception exception)
                {
                    if (!loggedNotifyFinished)
                    {
                        _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForGameplanPipelineEnd(0, 0,
                                                                                                              PipelineEventIDs.FINISHED_NOTIFYING_AUTOBOOK_API, RunId, ScenarioId, autoBook.Id,
                                                                                                              null, exception.Message, exception));

                        _pipelineAuditEventRepository.Add(PipelineEventHelper.CreatePipelineAuditEvent(AuditEventTypes.GamePlanRun,
                                                                                                       PipelineEventIDs.FINISHED_NOTIFYING_AUTOBOOK_API, RunId, ScenarioId, exception.Message));
                    }

                    throw;
                }
                finally
                {
                    _pipelineAuditEventRepository.SaveChanges();
                }

                // Flag as InProgress
                RunManager.UpdateScenarioStatuses(_repositoryFactory, _auditEventRepository, RunId,
                                                  new List <Guid>()
                {
                    ScenarioId
                }, new List <ScenarioStatuses>()
                {
                    ScenarioStatuses.InProgress
                });
                startedRun = true;
            }
            catch
            {
                throw;
            }
            finally
            {
                // Clean up failure
                if (!startedRun)
                {
                    // Reset to free, unlocks AutoBook instance so that it can be re-used
                    autoBookInterface.ResetFree();
                    autoBookRepository.SaveChanges();
                    _pipelineAuditEventRepository.SaveChanges();
                }
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Handles completed with error, no results
        /// </summary>
        public void HandleCompletedFatalError(AutoBook autoBook)
        {
            IRunRepository runRepository = null;

            try
            {
                using var scope = _repositoryFactory.BeginRepositoryScope();
                // Get scenario snapshot for diagnostics
                //_scenarioSnapshotGenerator.Generate(autoBook, _scenarioId);

                // Flag scenario as CompletedError, no results available
                DateTime?completedDateTime = DateTime.UtcNow;
                RunManager.UpdateScenarioStatuses(_repositoryFactory, _auditEventRepository, RunId, new List <Guid>()
                {
                    ScenarioId
                }, new List <ScenarioStatuses>()
                {
                    ScenarioStatuses.CompletedError
                }, null, new List <DateTime?>()
                {
                    completedDateTime
                });

                // Get run details
                runRepository = scope.CreateRepository <IRunRepository>();
                var run      = runRepository.Find(RunId);
                var scenario = run.Scenarios.Find(currentScenario => currentScenario.Id == ScenarioId);

                // Notification run scenario completed with error
                try
                {
                    _runCompletionNotifier.Notify(run, scenario, false);
                }
                catch (System.Exception exception)
                {
                    // Log failure, don't throw
                    _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, string.Format("Error generating notification for run completed failure (RunID={0}, ScenarioID={1})", RunId, ScenarioId), exception));
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                using var scope = _repositoryFactory.BeginRepositoryScope();
                // Load run so that we can check number of completed scenarios, use fresh repo instance in case run changed elsewhere
                runRepository = scope.CreateRepository <IRunRepository>();
                var run = runRepository.Find(RunId);

                // Generate notification if all scenarios completed, success if at least one scenario generated results
                if (run.Scenarios.Count == run.CompletedScenarios.Count)
                {
                    _synchronizationService.Release(run.Id);

                    int countSucccess = run.Scenarios.Where(currentScenario => currentScenario.Status == ScenarioStatuses.CompletedSuccess).ToList().Count;
                    try
                    {
                        _runCompletionNotifier.Notify(run, (countSucccess > 0));
                    }
                    catch (System.Exception exception)
                    {
                        // Log failure, don't throw
                        _auditEventRepository.Insert(AuditEventFactory.CreateAuditEventForException(0, 0, string.Format("Error generating notification for run completed failure (RunID={0})", run.Id), exception));
                    }
                }
            }
        }
Ejemplo n.º 12
0
 public void Add(AutoBook autoBook) =>
 _dbContext.Add(_mapper.Map <Entities.Tenant.AutoBookApi.AutoBook>(autoBook), post => post.MapTo(autoBook), _mapper);