|
|
@@ -30,9 +30,12 @@ import org.eclipse.jgit.api.ResetCommand;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.transaction.support.TransactionSynchronizationAdapter;
|
|
|
+import org.springframework.transaction.support.TransactionSynchronizationManager;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import javax.transaction.Transactional;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
@@ -203,7 +206,13 @@ public class EnvironmentServiceImpl implements EnvironmentService {
|
|
|
result.setBuildStatus(BuildStatus.BUILDING);
|
|
|
result.setBuildStartTime(LocalDateTime.now());
|
|
|
result.setDeployStatus(DeployStatus.NOT_STARTED);
|
|
|
- asyncWrapper.asyncInvoke(() -> buildAsync(id));
|
|
|
+ TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
|
|
|
+ @Override
|
|
|
+ public void afterCommit() {
|
|
|
+ super.afterCommit();
|
|
|
+ asyncWrapper.asyncInvoke(() -> buildAsync(id));
|
|
|
+ }
|
|
|
+ });
|
|
|
return EnvironmentConverter.convertToVO(environmentDAO.save(result));
|
|
|
}
|
|
|
|
|
|
@@ -228,6 +237,7 @@ public class EnvironmentServiceImpl implements EnvironmentService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional
|
|
|
public EnvironmentVO restart(Integer id, String podName) throws ServiceException {
|
|
|
Environment result = environmentDAO.findById(id).orElse(null);
|
|
|
if (result == null) {
|
|
|
@@ -241,6 +251,64 @@ public class EnvironmentServiceImpl implements EnvironmentService {
|
|
|
return EnvironmentConverter.convertToVO(environmentDAO.save(result));
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void checkDeployStatus() {
|
|
|
+ LocalDateTime startTime = LocalDateTime.now().minusMinutes(10);
|
|
|
+ List<Environment> environments = environmentDAO.findAllByBuildStatusAndBuildStartTimeBefore(BuildStatus.BUILDING, startTime);
|
|
|
+ if (!CollectionUtils.isEmpty(environments)) {
|
|
|
+ for (Environment environment : environments) {
|
|
|
+ environment.setBuildStatus(BuildStatus.FAIL);
|
|
|
+ environment.setBuildOutput("Timeout interrupt.");
|
|
|
+ logger.info("Interrupt buildTask: " + environment.getId());
|
|
|
+ environmentDAO.save(environment);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ environments = environmentDAO.findAllByDeployStatus(DeployStatus.DEPLOYING);
|
|
|
+ environments.addAll(environmentDAO.findAllByDeployStatus(DeployStatus.RESTARTING));
|
|
|
+ if (!CollectionUtils.isEmpty(environments)) {
|
|
|
+ for (Environment environment : environments) {
|
|
|
+ if (environment.getDeployStartTime().isBefore(startTime)) {
|
|
|
+ environment.setDeployStatus(DeployStatus.FAIL);
|
|
|
+ environment.setBuildOutput("Timeout interrupt.");
|
|
|
+ logger.info("Interrupt deployTask: " + environment.getId());
|
|
|
+ environmentDAO.save(environment);
|
|
|
+ } else {
|
|
|
+ String labelValue = ResourceLabel.RESOURCE.getGenerator().gen("environment", String.valueOf(environment.getAppId()), String.valueOf(environment.getId()));
|
|
|
+ List<Deployment> deployments = deploymentApi.getByCondition(K8sObjectRequest
|
|
|
+ .builder()
|
|
|
+ .name(labelValue)
|
|
|
+ .namespace(applicationProperties.getDeploymentNamespace())
|
|
|
+ .build());
|
|
|
+ if (!CollectionUtils.isEmpty(deployments)) {
|
|
|
+ V1DeploymentStatus status = deployments.get(0).getStatus();
|
|
|
+ List<V1DeploymentCondition> conditions = status.getConditions();
|
|
|
+ if (!CollectionUtils.isEmpty(conditions)) {
|
|
|
+ Collections.sort(conditions, Comparator.comparingLong(c -> ((V1DeploymentCondition)c).getLastUpdateTime().getMillis()).reversed());
|
|
|
+ V1DeploymentCondition condition = conditions.get(0);
|
|
|
+ switch (condition.getType()) {
|
|
|
+ case "Available":
|
|
|
+ case "Complete":
|
|
|
+ environment.setDeployStatus(DeployStatus.SUCCESS);
|
|
|
+ environment.setDeployOutput(condition.getMessage());
|
|
|
+ break;
|
|
|
+ case "Progressing":
|
|
|
+ environment.setDeployStatus(DeployStatus.DEPLOYING);
|
|
|
+ environment.setDeployOutput(condition.getMessage());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ environment.setDeployStatus(DeployStatus.FAIL);
|
|
|
+ environment.setDeployOutput(condition.getMessage());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ environmentDAO.save(environment);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
void buildAsync(Integer id) {
|
|
|
Environment result = environmentDAO.findById(id).get();
|
|
|
try {
|