Pārlūkot izejas kodu

修复钉钉token失效

weijianghai 1 mēnesi atpakaļ
vecāks
revīzija
5b8d08db9d

+ 46 - 39
src/main/java/com/nokia/dingtalk_api/service/AppTaskService.java

@@ -209,6 +209,9 @@ public class AppTaskService {
                         findFile(t, template, dirPath, fileList, processQueryKeys);
                     }
                 }
+                // 执行完删除重试任务
+                iRetryAppTaskService.removeById(t.getTaskId());
+                RETRY_TASKS.remove(t.getTaskId());
                 log.info("结束应用任务: {}", t);
                 String content = t.getAppName() + " " + t.getTaskName() + " 执行成功" + " : "
                         + gson.toJson(processQueryKeys);
@@ -224,24 +227,24 @@ public class AppTaskService {
             appTaskLogPo.setDetail(e.toString());
             log.error("线程中断: {}, {}", t, e, e);
             Thread.currentThread().interrupt();
-            retry(t);
             alert(t, "任务意外中断");
+            retry(t);
         } catch (TimeoutException e) {
             appTaskLogPo.setStatus(0);
             appTaskLogPo.setDetail(e.toString());
             log.error("应用任务执行超时: {}, {}", t, e, e);
-            retry(t);
             alert(t, "任务执行超时");
+            retry(t);
         } catch (Exception e) {
             appTaskLogPo.setStatus(0);
             appTaskLogPo.setDetail(e.toString());
             log.error("应用任务执行失败: {}, {}", t, e, e);
-            retry(t);
             Throwable rootCause = e;
             while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
                 rootCause = rootCause.getCause();
             }
             alert(t, rootCause.getMessage());
+            retry(t);
         } finally {
             // 记录日志
             appTaskLogPo.setFiles(gson.toJson(fileList));
@@ -289,44 +292,48 @@ public class AppTaskService {
      * @param content 内容
      */
     public void sendMessage(AppTaskBo t, String content) {
-        String robotCode = t.getRobotCode();
-        String robotSecret = t.getRobotSecret();
-        String openConversationId = t.getOpenConversationId();
-        String phones = t.getPhones();
-        String userIds = t.getUserIds();
-        if (TaskAlertTypeEnum.DEFAULT.name().equals(t.getAlertType())) {
-            AlertConfigPo alertConfigPo = iAlertConfigService.getById(1);
-            if (alertConfigPo == null) {
+        try {
+            String robotCode = t.getRobotCode();
+            String robotSecret = t.getRobotSecret();
+            String openConversationId = t.getOpenConversationId();
+            String phones = t.getPhones();
+            String userIds = t.getUserIds();
+            if (TaskAlertTypeEnum.DEFAULT.name().equals(t.getAlertType())) {
+                AlertConfigPo alertConfigPo = iAlertConfigService.getById(1);
+                if (alertConfigPo == null) {
+                    return;
+                }
+                robotCode = alertConfigPo.getRobotCode();
+                robotSecret = alertConfigPo.getRobotSecret();
+                openConversationId = alertConfigPo.getOpenConversationId();
+                phones = alertConfigPo.getPhones();
+                userIds = alertConfigPo.getUserIds();
+            } else if (TaskAlertTypeEnum.CUSTOM.name().equals(t.getAlertType())) {
+                robotCode = t.getAlertRobotCode();
+                robotSecret = t.getAlertRobotSecret();
+                openConversationId = t.getAlertOpenConversationId();
+                phones = t.getAlertPhones();
+                userIds = t.getAlertUserIds();
+            }
+            if (!StringUtils.hasText(robotCode)
+                    || !StringUtils.hasText(robotSecret)
+                    || (!StringUtils.hasText(openConversationId)
+                    && !StringUtils.hasText(phones)
+                    && !StringUtils.hasText(userIds))) {
                 return;
             }
-            robotCode = alertConfigPo.getRobotCode();
-            robotSecret = alertConfigPo.getRobotSecret();
-            openConversationId = alertConfigPo.getOpenConversationId();
-            phones = alertConfigPo.getPhones();
-            userIds = alertConfigPo.getUserIds();
-        } else if (TaskAlertTypeEnum.CUSTOM.name().equals(t.getAlertType())) {
-            robotCode = t.getAlertRobotCode();
-            robotSecret = t.getAlertRobotSecret();
-            openConversationId = t.getAlertOpenConversationId();
-            phones = t.getAlertPhones();
-            userIds = t.getAlertUserIds();
-        }
-        if (!StringUtils.hasText(robotCode)
-                || !StringUtils.hasText(robotSecret)
-                || (!StringUtils.hasText(openConversationId)
-                && !StringUtils.hasText(phones)
-                && !StringUtils.hasText(userIds))) {
-            return;
-        }
-        String accessToken = openService.getAccessToken(robotCode, robotSecret);
-        if (StringUtils.hasText(openConversationId)) {
-            DingTalkApiUtil.groupSendSampleText(content, accessToken, openConversationId,
-                    robotCode);
-        }
-        if (StringUtils.hasText(phones) || StringUtils.hasText(userIds)) {
-            List<String> userIdList = getUserIdList(phones, userIds,
-                    accessToken);
-            DingTalkApiUtil.batchSendOtoSampleText(content, accessToken, robotCode, userIdList);
+            String accessToken = openService.getAccessToken(robotCode, robotSecret);
+            if (StringUtils.hasText(openConversationId)) {
+                DingTalkApiUtil.groupSendSampleText(content, accessToken, openConversationId,
+                        robotCode);
+            }
+            if (StringUtils.hasText(phones) || StringUtils.hasText(userIds)) {
+                List<String> userIdList = getUserIdList(phones, userIds,
+                        accessToken);
+                DingTalkApiUtil.batchSendOtoSampleText(content, accessToken, robotCode, userIdList);
+            }
+        } catch (Exception e) {
+            log.error("发送通知失败: {}", e.getMessage(), e);
         }
     }
 

+ 1 - 1
src/main/java/com/nokia/dingtalk_api/service/DingtalkClientService.java

@@ -56,7 +56,7 @@ public class DingtalkClientService {
         // 测试机器人是否可用
         GetAccessTokenResponse r = DingTalkApiUtil.getAccessToken(po.getRobotCode(), robotSecret);
         redisTemplate.opsForValue().set(RedisPrefixEnum.DINGTALK_ACCESS_TOKEN.prefix + po.getRobotCode(),
-                r.body, r.body.expireIn, RedisPrefixEnum.DINGTALK_ACCESS_TOKEN.timeUnit);
+                r.body, r.body.expireIn - 60, RedisPrefixEnum.DINGTALK_ACCESS_TOKEN.timeUnit);
         OpenDingTalkClient clientOld = MAP.get(po.getRobotCode());
         if (clientOld != null) {
             MAP.remove(po.getRobotCode());

+ 1 - 2
src/main/java/com/nokia/dingtalk_api/service/OpenService.java

@@ -94,12 +94,11 @@ public class OpenService {
         String k = RedisPrefixEnum.DINGTALK_ACCESS_TOKEN.prefix + appKey;
         GetAccessTokenResponseBody o = (GetAccessTokenResponseBody) redisTemplate.opsForValue().get(k);
         if (o != null) {
-            redisTemplate.expire(k, o.expireIn, RedisPrefixEnum.DINGTALK_ACCESS_TOKEN.timeUnit);
             return o.accessToken;
         }
         GetAccessTokenResponse r = DingTalkApiUtil.getAccessToken(appKey, aesService.decrypt(appSecret));
         redisTemplate.opsForValue().set(RedisPrefixEnum.DINGTALK_ACCESS_TOKEN.prefix + appKey,
-                r.body, r.body.expireIn, RedisPrefixEnum.DINGTALK_ACCESS_TOKEN.timeUnit);
+                r.body, r.body.expireIn - 60, RedisPrefixEnum.DINGTALK_ACCESS_TOKEN.timeUnit);
         return r.body.accessToken;
     }