private void ComponentDeploymentFailure(ComponentVertexDeploymentProgress value)
        {
            var name      = GetName(value);
            var flowId    = GetFlowId(value);
            var timeStamp = GetJavaTimeStamp();

            var progressMessage = _serviceMessageFormatter.FormatMessage("progressMessage", _logMessages.DeploymentFailed(value.Vertex, value.Text));

            var buildProblemMessage = _serviceMessageFormatter.FormatMessage("buildProblem", new
            {
                description = _logMessages.DeploymentFailed(value.Vertex, value.Text),
                identity    = name,
                flowId      = flowId,
                timeStamp   = timeStamp
            });

            var testFailedMessage = _serviceMessageFormatter.FormatMessage("testFailed", new
            {
                name      = name,
                message   = _logMessages.DeploymentFailed(value.Vertex, value.Text),
                details   = _logMessages.DeploymentFailed(value.Vertex, value.Text),
                flowId    = flowId,
                timeStamp = timeStamp
            });

            var failedDeploymentMessage = _serviceMessageFormatter.FormatMessage("message", new
            {
                text         = _logMessages.DeploymentFailed(value.Vertex, value.Text),
                errorDetails = _logMessages.DeploymentFailed(value.Vertex, value.Text),
                status       = "ERROR",
                flowId       = flowId,
                timeStamp    = timeStamp
            });

            var testFinishMessage = _serviceMessageFormatter.FormatMessage("testFinished", new
            {
                name      = name,
                flowId    = flowId,
                timeStamp = timeStamp
            });

            Console.Out.WriteLine(progressMessage);
            Console.Out.WriteLine(buildProblemMessage);
            Console.Out.WriteLine(testFailedMessage);
            Console.Out.WriteLine(failedDeploymentMessage);
            Console.Out.WriteLine(testFinishMessage);
        }
        /// <summary>
        /// Deploy component
        /// </summary>
        /// <param name="componentDeploymentVertex"></param>
        /// <param name="cancellationToken"></param>
        private ComponentVertexDeploymentResult DeployComponent(ComponentDeploymentVertex componentDeploymentVertex, CancellationToken cancellationToken)
        {
            try
            {
                _throttler.Wait(cancellationToken);
                try
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(new ComponentVertexDeploymentResult
                        {
                            Status = ComponentVertexDeploymentStatus.Cancelled,
                            Description = _logMessages.DeploymentCancelled(componentDeploymentVertex)
                        });
                    }

                    //Start progress
                    if (_progress != null)
                    {
                        var componentVertexDeploymentProgress = new ComponentVertexDeploymentProgress
                        {
                            Vertex       = componentDeploymentVertex,
                            Status       = ComponentVertexDeploymentStatus.Started,
                            MinimumValue = 0,
                            MaximumValue =
                                componentDeploymentVertex.DeploymentDuration.HasValue
                                    ? componentDeploymentVertex.DeploymentDuration.Value.Ticks
                                    : 0,
                            Value = 0,
                            Text  = _logMessages.DeploymentStarted(componentDeploymentVertex)
                        };
                        _progress.Report(componentVertexDeploymentProgress);
                    }

                    var result = new ComponentVertexDeploymentResult();

                    try
                    {
                        foreach (var deployer in _deployers)
                        {
                            result = deployer.Deploy(componentDeploymentVertex, cancellationToken, _logMessages, _progress);
                        }
                    }
                    catch (Exception ex)
                    {
                        var stringBuilder = new StringBuilder();
                        WriteExceptionDetails(ex, stringBuilder, 1);

                        result = new ComponentVertexDeploymentResult
                        {
                            Status      = ComponentVertexDeploymentStatus.Failure,
                            Description = _logMessages.DeploymentFailed(componentDeploymentVertex, stringBuilder.ToString())
                        };
                    }

                    //Finish progress
                    if (_progress != null)
                    {
                        var componentVertexDeploymentProgress = new ComponentVertexDeploymentProgress
                        {
                            Vertex       = componentDeploymentVertex,
                            Status       = result.Status,
                            MinimumValue = 0,
                            MaximumValue =
                                componentDeploymentVertex.DeploymentDuration.HasValue
                                    ? componentDeploymentVertex.DeploymentDuration.Value.Ticks
                                    : 0,
                            Value =
                                componentDeploymentVertex.DeploymentDuration.HasValue
                                    ? componentDeploymentVertex.DeploymentDuration.Value.Ticks
                                    : 0,
                            Text = result.Description
                        };
                        _progress.Report(componentVertexDeploymentProgress);
                    }

                    return(result);
                }
                finally
                {
                    _throttler.Release();
                }
            }
            catch (OperationCanceledException)
            {
                return(new ComponentVertexDeploymentResult
                {
                    Status = ComponentVertexDeploymentStatus.Cancelled,
                    Description = _logMessages.DeploymentCancelled(componentDeploymentVertex)
                });
            }
        }
 private void ComponentDeploymentFailure(ComponentVertexDeploymentProgress value)
 {
     Console.Out.WriteLine(_logMessages.DeploymentFailed(value.Vertex, value.Text));
 }
        public ComponentVertexDeploymentResult Deploy(ComponentDeploymentVertex componentDeploymentVertex, CancellationToken cancellationToken, ILogMessages logMessages, IProgress <ComponentVertexDeploymentProgress> progress)
        {
            if (!componentDeploymentVertex.Exists || componentDeploymentVertex.DeploymentAction == PlanAction.Skip)
            {
                return(new ComponentVertexDeploymentResult
                {
                    Status = ComponentVertexDeploymentStatus.Success,
                    Description = logMessages.DeploymentSkipped(componentDeploymentVertex)
                });
            }

            if (componentDeploymentVertex.Version == null)
            {
                throw new Exception("Version for release is null");
            }

            var environment = _repository.Environments.GetEnvironment(_environmentToDeployTo.Name);
            var project     = _repository.Projects.GetProjectByName(componentDeploymentVertex.Name);
            var release     = _repository.Projects.GetRelease(project.Id, componentDeploymentVertex.Version);

            var deployment = new DeploymentResource
            {
                ReleaseId                = release.Id,
                EnvironmentId            = environment.Id,
                Comments                 = _comments,
                ForcePackageDownload     = _forcePackageDownload,
                ForcePackageRedeployment = _forcePackageRedeployment,
            };

            var queuedDeployment = _repository.Deployments.Create(deployment);

            componentDeploymentVertex.DeploymentId = queuedDeployment.Id;
            var deploymentTask = _repository.Tasks.Get(queuedDeployment.TaskId);

            Action <TaskResource[]> interval = tasks =>
            {
                foreach (var task in tasks)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        _repository.Tasks.Cancel(task);
                    }

                    var duration = new TimeSpan(0);

                    if (task.StartTime.HasValue)
                    {
                        var now = new DateTimeOffset(DateTime.UtcNow);
                        duration = now.Subtract(task.StartTime.Value);
                    }

                    if (progress != null)
                    {
                        var componentVertexDeploymentProgress = new ComponentVertexDeploymentProgress
                        {
                            Vertex       = componentDeploymentVertex,
                            Status       = ComponentVertexDeploymentStatus.InProgress,
                            MinimumValue = 0,
                            MaximumValue =
                                componentDeploymentVertex.DeploymentDuration.HasValue
                                    ? componentDeploymentVertex.DeploymentDuration.Value.Ticks
                                    : 0,
                            Value = duration.Ticks,
                            Text  = logMessages.DeploymentProgress(componentDeploymentVertex, task.Description)
                        };
                        progress.Report(componentVertexDeploymentProgress);
                    }
                }
            };

            _repository.Tasks.WaitForCompletion(deploymentTask, _pollIntervalSeconds, _timeoutAfterMinutes, interval);

            deploymentTask = _repository.Tasks.Get(queuedDeployment.TaskId);

            var result = new ComponentVertexDeploymentResult();

            switch (deploymentTask.State)
            {
            case TaskState.Success:
                result.Status      = ComponentVertexDeploymentStatus.Success;
                result.Description = logMessages.DeploymentSuccess(componentDeploymentVertex);
                break;

            case TaskState.Canceled:
            case TaskState.Cancelling:
                result.Status      = ComponentVertexDeploymentStatus.Cancelled;
                result.Description = logMessages.DeploymentCancelled(componentDeploymentVertex);
                break;

            case TaskState.Failed:
            case TaskState.TimedOut:
                result.Status      = ComponentVertexDeploymentStatus.Failure;
                result.Description = logMessages.DeploymentFailed(componentDeploymentVertex, deploymentTask.ErrorMessage);
                break;

            default:
                result.Status      = ComponentVertexDeploymentStatus.Failure;
                result.Description = logMessages.DeploymentFailed(componentDeploymentVertex, deploymentTask.ErrorMessage);
                break;
            }

            return(result);
        }