Ejemplo n.º 1
0
        public async Task Handle(ApplyAdded notification, CancellationToken cancellationToken)
        {
            _apply = await _db.Applies
                     .Include(a => a.Run)
                     .ThenInclude(r => r.Workspace)
                     .SingleOrDefaultAsync(x => x.Id == notification.ApplyId);

            try
            {
                _output = _outputService.GetOrAddOutput(_apply.Id);

                // Update status
                _apply.Status     = Domain.Models.ApplyStatus.Applying;
                _apply.Run.Status = Domain.Models.RunStatus.Applying;
                await this.UpdateApply();

                var workingDir = _apply.Run.Workspace.GetPath(_options.RootWorkingDirectory);

                _timer          = new System.Timers.Timer(_options.OutputSaveInterval);
                _timer.Elapsed += OnTimedEvent;
                _timer.Start();

                var  result  = _terraformService.Apply(workingDir, OutputHandler);
                bool isError = result.IsError;

                lock (_apply)
                {
                    _timerComplete = true;
                    _timer.Stop();
                }

                _apply.Output = _outputBuilder.ToString();
                await _apply.Run.Workspace.RetrieveState(workingDir);

                _apply.Status     = !isError ? ApplyStatus.Applied : ApplyStatus.Failed;
                _apply.Run.Status = !isError ? RunStatus.Applied : RunStatus.Failed;

                await this.UpdateApply();

                await _mediator.Publish(new ApplyCompleted(_apply.Run.Workspace));

                _output.SetCompleted();
                _outputService.RemoveOutput(_apply.Id);

                _apply.Run.Workspace.CleanupFileSystem(_options.RootWorkingDirectory);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error in {nameof(ApplyAddedHandler)}.Handle");
                _apply.Status     = Domain.Models.ApplyStatus.Failed;
                _apply.Run.Status = Domain.Models.RunStatus.Failed;
                await this.UpdateApply();
            }
        }
Ejemplo n.º 2
0
        private async Task <Domain.Models.Plan> CreatePlan(Domain.Models.Run run)
        {
            var plan = new Domain.Models.Plan
            {
                RunId  = run.Id,
                Status = Domain.Models.PlanStatus.Planning
            };

            run.Plan   = plan;
            run.Status = Domain.Models.RunStatus.Planning;

            await _db.AddAsync(plan);

            await _db.SaveChangesAsync();

            _output = _outputService.GetOrAddOutput(plan.Id);
            await _mediator.Publish(new RunUpdated(run.Id));

            return(plan);
        }