|
@@ -1,81 +1,81 @@
|
|
|
-package com.fdkankan.modeling.job;
|
|
|
-
|
|
|
-import com.fdkankan.modeling.constants.SysConstants;
|
|
|
-import com.fdkankan.rubbersheeting.ScalingService;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.context.annotation.Lazy;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.ObjectUtils;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import java.util.Random;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
-@Component
|
|
|
-public class EcsJob {
|
|
|
-
|
|
|
- private static final Logger log = LoggerFactory.getLogger(EcsJob.class);
|
|
|
-
|
|
|
- @Autowired
|
|
|
- @Lazy
|
|
|
- private ScalingService scalingService;
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void deleteEcs() {
|
|
|
- if (SysConstants.isResidenceService) {
|
|
|
- log.info("此服务是常驻服务,不启动关闭弹性伸缩定时任务!");
|
|
|
- return;
|
|
|
- }
|
|
|
- log.info("此服务非常驻服务,开始启动关闭弹性伸缩定时任务");
|
|
|
- Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
|
|
|
- // 判断是否有任务执行
|
|
|
- if (SysConstants.SYSTEM_BUILDING) {
|
|
|
- log.error("{} 服务构建中,退出停止服务请求!", SysConstants.hostName);
|
|
|
- return;
|
|
|
- }
|
|
|
- log.info("服务未构建,准备停止服务!");
|
|
|
- if (!SysConstants.executorService.isShutdown()) {
|
|
|
- SysConstants.SYSTEM_OFFING = true;
|
|
|
- if (!SysConstants.SYSTEM_BUILDING) {
|
|
|
- SysConstants.executorService.shutdown();
|
|
|
- log.error("{} 线程池关闭,不接受新的构建请求!", SysConstants.hostName);
|
|
|
- }else{
|
|
|
- SysConstants.SYSTEM_OFFING = false;
|
|
|
- log.error("{} 服务构建中,退出停止服务请求!", SysConstants.hostName);
|
|
|
- }
|
|
|
- }
|
|
|
- // 因为 阿里云同一时间不能删除多台实例,因此做随机睡眠,错开时间。
|
|
|
- try {
|
|
|
- Thread.sleep((new Random().nextInt(30) + 10) * 1000);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- // 没有任务执行时,则退出
|
|
|
- if (SysConstants.executorService.isShutdown() && SysConstants.executorService.isTerminated()) {
|
|
|
- log.error("{} 请求阿里云关闭弹性伸缩", SysConstants.hostName);
|
|
|
- String result = null;
|
|
|
- int tryTimes = -1;
|
|
|
- do {
|
|
|
- tryTimes++;
|
|
|
- result = scalingService.deleteEcs(SysConstants.hostName);
|
|
|
- if (ObjectUtils.isEmpty(result)) {
|
|
|
- int time = new Random().nextInt(10) + 30;
|
|
|
- log.error("{} 关闭弹性伸缩失败,{}秒后重试", SysConstants.hostName, time);
|
|
|
- try {
|
|
|
- Thread.sleep(time * 1000);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- } while (ObjectUtils.isEmpty(result) && tryTimes < 5);
|
|
|
- log.error("{} 关闭弹性伸缩成功!", SysConstants.hostName);
|
|
|
- } else {
|
|
|
- log.error("{} 服务构建中,退出删除程序失败!", SysConstants.hostName);
|
|
|
- }
|
|
|
- },
|
|
|
- 50, 60, TimeUnit.MINUTES);
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.fdkankan.modeling.job;
|
|
|
+//
|
|
|
+//import com.fdkankan.modeling.constants.SysConstants;
|
|
|
+//import com.fdkankan.rubbersheeting.ScalingService;
|
|
|
+//import org.slf4j.Logger;
|
|
|
+//import org.slf4j.LoggerFactory;
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//import org.springframework.context.annotation.Lazy;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//import org.springframework.util.ObjectUtils;
|
|
|
+//
|
|
|
+//import javax.annotation.PostConstruct;
|
|
|
+//import java.util.Random;
|
|
|
+//import java.util.concurrent.Executors;
|
|
|
+//import java.util.concurrent.TimeUnit;
|
|
|
+//
|
|
|
+//@Component
|
|
|
+//public class EcsJob {
|
|
|
+//
|
|
|
+// private static final Logger log = LoggerFactory.getLogger(EcsJob.class);
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// @Lazy
|
|
|
+// private ScalingService scalingService;
|
|
|
+//
|
|
|
+// @PostConstruct
|
|
|
+// public void deleteEcs() {
|
|
|
+// if (SysConstants.isResidenceService) {
|
|
|
+// log.info("此服务是常驻服务,不启动关闭弹性伸缩定时任务!");
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// log.info("此服务非常驻服务,开始启动关闭弹性伸缩定时任务");
|
|
|
+// Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
|
|
|
+// // 判断是否有任务执行
|
|
|
+// if (SysConstants.SYSTEM_BUILDING) {
|
|
|
+// log.error("{} 服务构建中,退出停止服务请求!", SysConstants.hostName);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// log.info("服务未构建,准备停止服务!");
|
|
|
+// if (!SysConstants.executorService.isShutdown()) {
|
|
|
+// SysConstants.SYSTEM_OFFING = true;
|
|
|
+// if (!SysConstants.SYSTEM_BUILDING) {
|
|
|
+// SysConstants.executorService.shutdown();
|
|
|
+// log.error("{} 线程池关闭,不接受新的构建请求!", SysConstants.hostName);
|
|
|
+// }else{
|
|
|
+// SysConstants.SYSTEM_OFFING = false;
|
|
|
+// log.error("{} 服务构建中,退出停止服务请求!", SysConstants.hostName);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// // 因为 阿里云同一时间不能删除多台实例,因此做随机睡眠,错开时间。
|
|
|
+// try {
|
|
|
+// Thread.sleep((new Random().nextInt(30) + 10) * 1000);
|
|
|
+// } catch (InterruptedException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// // 没有任务执行时,则退出
|
|
|
+// if (SysConstants.executorService.isShutdown() && SysConstants.executorService.isTerminated()) {
|
|
|
+// log.error("{} 请求阿里云关闭弹性伸缩", SysConstants.hostName);
|
|
|
+// String result = null;
|
|
|
+// int tryTimes = -1;
|
|
|
+// do {
|
|
|
+// tryTimes++;
|
|
|
+// result = scalingService.deleteEcs(SysConstants.hostName);
|
|
|
+// if (ObjectUtils.isEmpty(result)) {
|
|
|
+// int time = new Random().nextInt(10) + 30;
|
|
|
+// log.error("{} 关闭弹性伸缩失败,{}秒后重试", SysConstants.hostName, time);
|
|
|
+// try {
|
|
|
+// Thread.sleep(time * 1000);
|
|
|
+// } catch (InterruptedException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } while (ObjectUtils.isEmpty(result) && tryTimes < 5);
|
|
|
+// log.error("{} 关闭弹性伸缩成功!", SysConstants.hostName);
|
|
|
+// } else {
|
|
|
+// log.error("{} 服务构建中,退出删除程序失败!", SysConstants.hostName);
|
|
|
+// }
|
|
|
+// },
|
|
|
+// 50, 60, TimeUnit.MINUTES);
|
|
|
+// }
|
|
|
+//}
|