|
@@ -0,0 +1,48 @@
|
|
|
|
|
+package cn.seecoder.web.infrastructure;
|
|
|
|
|
+
|
|
|
|
|
+import cn.seecoder.common.exceptions.AccessDeniedException;
|
|
|
|
|
+import cn.seecoder.web.dao.pipeline.DeploymentMapper;
|
|
|
|
|
+import cn.seecoder.web.dao.pipeline.PipelineMapper;
|
|
|
|
|
+import cn.seecoder.web.model.po.pipeline.DeploymentPO;
|
|
|
|
|
+import cn.seecoder.web.model.po.pipeline.PipelinePO;
|
|
|
|
|
+import cn.seecoder.web.service.pipeline.DeploymentService;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import lombok.extern.log4j.Log4j;
|
|
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import java.sql.Timestamp;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+@Log4j2
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+@Async
|
|
|
|
|
+@Component
|
|
|
|
|
+public class ScheduledTasks {
|
|
|
|
|
+ private final DeploymentService deploymentService;
|
|
|
|
|
+ private final DeploymentMapper deploymentMapper;
|
|
|
|
|
+ private final PipelineMapper pipelineMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Scheduled(cron = "0 0 1 * * ?")
|
|
|
|
|
+ public void purgeZombieDeployments() {
|
|
|
|
|
+ long ONE_DAY_MILLIS = 86400000L;
|
|
|
|
|
+ Timestamp expireLimit = new Timestamp(System.currentTimeMillis() - ONE_DAY_MILLIS);
|
|
|
|
|
+ List<DeploymentPO> deploymentPOList = deploymentMapper.selectAllDeployments();
|
|
|
|
|
+ for (DeploymentPO deploymentPO: deploymentPOList) {
|
|
|
|
|
+ Timestamp deployedTime = deploymentPO.getDeployedTime();
|
|
|
|
|
+ if (deployedTime.before(expireLimit)) {
|
|
|
|
|
+ PipelinePO pipelinePO = pipelineMapper.selectById(deploymentPO.getPipelineId());
|
|
|
|
|
+ try {
|
|
|
|
|
+ deploymentService.delete(pipelinePO.getProjectId(), deploymentPO.getPipelineId());
|
|
|
|
|
+ } catch (AccessDeniedException e) {
|
|
|
|
|
+ log.error(String.format("Scheduled Deployments Purge Failed for projectId: %s, pipelineId: %s, for %s",
|
|
|
|
|
+ pipelinePO.getProjectId(),
|
|
|
|
|
+ deploymentPO.getPipelineId(),
|
|
|
|
|
+ e.getMessage()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|