class ClusterStatusUpdater {

    public void updateClusterStatus(Stack stack, Cluster cluster) {
        if (isStackOrClusterStatusInvalid(stack, cluster)) {
            if ((stack.isStackInStopPhase() || stack.isDeleteCompleted()) && cluster != null) {
                updateClusterStatus(stack.getId(), cluster, stack.getStatus());
                cluster.setStatus(stack.getStatus());
            }
            List<String> eventMessageArgs = Arrays.asList(stack.getStatus().name(), cluster == null ? "" : cluster.getStatus().name());
            cloudbreakEventService.fireCloudbreakEvent(stack.getId(), stack.getStatus().name(), CLUSTER_AMBARI_CLUSTER_COULD_NOT_SYNC, eventMessageArgs);
        } else if (cluster != null && cluster.getClusterManagerIp() != null) {
            Long stackId = stack.getId();
            clusterService.updateClusterMetadata(stackId);
            String blueprintName = cluster.getBlueprint().getStackName();
            ClusterStatusResult clusterStatusResult =
                    clusterApiConnectors.getConnector(stack).clusterStatusService().getStatus(StringUtils.isNotBlank(blueprintName));
            LOGGER.debug("Cluster status: [{}] Status reason: [{}]", clusterStatusResult.getClusterStatus(), clusterStatusResult.getStatusReason());
            updateClusterStatus(stackId, stack.getStatus(), cluster, clusterStatusResult);
        }
    }

}