EcsJob.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //package com.fdkankan.modeling.job;
  2. //
  3. //import com.fdkankan.modeling.constants.SysConstants;
  4. //import com.fdkankan.rubbersheeting.ScalingService;
  5. //import org.slf4j.Logger;
  6. //import org.slf4j.LoggerFactory;
  7. //import org.springframework.beans.factory.annotation.Autowired;
  8. //import org.springframework.context.annotation.Lazy;
  9. //import org.springframework.stereotype.Component;
  10. //import org.springframework.util.ObjectUtils;
  11. //
  12. //import javax.annotation.PostConstruct;
  13. //import java.util.Random;
  14. //import java.util.concurrent.Executors;
  15. //import java.util.concurrent.TimeUnit;
  16. //
  17. //@Component
  18. //public class EcsJob {
  19. //
  20. // private static final Logger log = LoggerFactory.getLogger(EcsJob.class);
  21. //
  22. // @Autowired
  23. // @Lazy
  24. // private ScalingService scalingService;
  25. //
  26. // @PostConstruct
  27. // public void deleteEcs() {
  28. // if (SysConstants.isResidenceService) {
  29. // log.info("此服务是常驻服务,不启动关闭弹性伸缩定时任务!");
  30. // return;
  31. // }
  32. // log.info("此服务非常驻服务,开始启动关闭弹性伸缩定时任务");
  33. // Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
  34. // // 判断是否有任务执行
  35. // if (SysConstants.SYSTEM_BUILDING) {
  36. // log.error("{} 服务构建中,退出停止服务请求!", SysConstants.hostName);
  37. // return;
  38. // }
  39. // log.info("服务未构建,准备停止服务!");
  40. // if (!SysConstants.executorService.isShutdown()) {
  41. // SysConstants.SYSTEM_OFFING = true;
  42. // if (!SysConstants.SYSTEM_BUILDING) {
  43. // SysConstants.executorService.shutdown();
  44. // log.error("{} 线程池关闭,不接受新的构建请求!", SysConstants.hostName);
  45. // }else{
  46. // SysConstants.SYSTEM_OFFING = false;
  47. // log.error("{} 服务构建中,退出停止服务请求!", SysConstants.hostName);
  48. // }
  49. // }
  50. // // 因为 阿里云同一时间不能删除多台实例,因此做随机睡眠,错开时间。
  51. // try {
  52. // Thread.sleep((new Random().nextInt(30) + 10) * 1000);
  53. // } catch (InterruptedException e) {
  54. // e.printStackTrace();
  55. // }
  56. // // 没有任务执行时,则退出
  57. // if (SysConstants.executorService.isShutdown() && SysConstants.executorService.isTerminated()) {
  58. // log.error("{} 请求阿里云关闭弹性伸缩", SysConstants.hostName);
  59. // String result = null;
  60. // int tryTimes = -1;
  61. // do {
  62. // tryTimes++;
  63. // result = scalingService.deleteEcs(SysConstants.hostName);
  64. // if (ObjectUtils.isEmpty(result)) {
  65. // int time = new Random().nextInt(10) + 30;
  66. // log.error("{} 关闭弹性伸缩失败,{}秒后重试", SysConstants.hostName, time);
  67. // try {
  68. // Thread.sleep(time * 1000);
  69. // } catch (InterruptedException e) {
  70. // e.printStackTrace();
  71. // }
  72. // }
  73. // } while (ObjectUtils.isEmpty(result) && tryTimes < 5);
  74. // log.error("{} 关闭弹性伸缩成功!", SysConstants.hostName);
  75. // } else {
  76. // log.error("{} 服务构建中,退出删除程序失败!", SysConstants.hostName);
  77. // }
  78. // },
  79. // 50, 60, TimeUnit.MINUTES);
  80. // }
  81. //}