瀏覽代碼

定时任务起计算服务弹性伸缩

dengsixing 3 年之前
父節點
當前提交
8fc2752f3e
共有 1 個文件被更改,包括 45 次插入4 次删除
  1. 45 4
      4dkankan-center-scene/src/main/java/com/fdkankan/scene/schedule/ScheduleJob.java

+ 45 - 4
4dkankan-center-scene/src/main/java/com/fdkankan/scene/schedule/ScheduleJob.java

@@ -1,6 +1,10 @@
 package com.fdkankan.scene.schedule;
 
-import com.fdkankan.common.factory.LogFactory;
+import com.fdkankan.common.util.RubberSheetingUtil;
+import com.fdkankan.rabbitmq.util.RabbitMqProducer;
+import com.fdkankan.redis.constant.RedisKey;
+import com.fdkankan.redis.constant.RedisLockKey;
+import com.fdkankan.redis.util.RedisLockUtil;
 import com.fdkankan.scene.service.IScene3dNumService;
 import com.fdkankan.scene.service.ISceneService;
 import lombok.extern.slf4j.Slf4j;
@@ -13,14 +17,23 @@ import org.springframework.stereotype.Component;
 @Component
 public class ScheduleJob {
 
-    @Autowired
-    ISceneService sceneService;
-
     @Value("${environment}")
     private String environment;
+    @Value("${queue.modeling.modeling-call}")
+    private String queueModelingCall;
+    @Value("${scaling.mq.threshold.modeling-call}")
+    private String modelingCallMqThreshold;
 
     @Autowired
+    ISceneService sceneService;
+    @Autowired
     private IScene3dNumService scene3dNumService;
+    @Autowired
+    private RedisLockUtil redisLockUtil;
+    @Autowired
+    RubberSheetingUtil rubberSheetingUtil;
+    @Autowired
+    RabbitMqProducer rabbitMqProducer;
 
     /**
      * 更新浏览量
@@ -55,4 +68,32 @@ public class ScheduleJob {
         log.info("执行定时任务结束:批量生成场景码");
     }
 
+    /**
+     * 开启场景计算弹性伸缩
+     */
+    @Scheduled(cron = "0 0/5 8-21 * * ? ")
+    public void startupModelingServer() {
+        if("hq".equals(environment)){
+            return;
+        }
+        boolean lock = redisLockUtil.lock(
+            RedisLockKey.LOCK_STARTUP_MODELING_SERVER, RedisKey.EXPIRE_TIME_5_MINUTE);
+        if(!lock){
+            return;
+        }
+        try {
+            //当mq排队数大于指定数量时使用弹性升缩
+            int mqNum = rabbitMqProducer.getMessageCount(queueModelingCall);
+            log.info("每5分钟查询一次排队队列,mqNum:" + mqNum);
+            if(mqNum - Integer.parseInt(modelingCallMqThreshold) > 0){
+                String responce = rubberSheetingUtil.createEcs();
+                log.info("开启弹性伸缩:{}", responce);
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage());
+        }finally {
+            redisLockUtil.unlockLua(RedisLockKey.LOCK_STARTUP_MODELING_SERVER);
+        }
+    }
+
 }