From 13bd6394a6cc5f319eed5bd3ac23531d2badbad3 Mon Sep 17 00:00:00 2001 From: Luoye Date: Mon, 12 Oct 2020 17:16:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E9=94=99=E8=AF=AF=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/qyi/e5/config/Start.java | 33 +++++++++++++++++++ .../io/qyi/e5/service/task/impl/TaskImpl.java | 26 +++++++++++++-- .../java/io/qyi/e5/util/redis/RedisUtil.java | 18 +++++++--- 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/qyi/e5/config/Start.java diff --git a/src/main/java/io/qyi/e5/config/Start.java b/src/main/java/io/qyi/e5/config/Start.java new file mode 100644 index 0000000..de51060 --- /dev/null +++ b/src/main/java/io/qyi/e5/config/Start.java @@ -0,0 +1,33 @@ +package io.qyi.e5.config; + +import io.qyi.e5.service.task.ITask; +import io.qyi.e5.util.redis.RedisUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +/** + * @program: e5 + * @description: + * @author: 落叶随风 + * @create: 2020-10-12 16:58 + **/ +@Component +@Slf4j +public class Start { + @Autowired + RedisUtil redisUtil; + @Autowired + ITask Task; + + @PostConstruct + public void initRedis() { + log.info("清空redis...... "); + redisUtil.delAll(); + /* log.info("重新添加队列...... "); + Task.sendTaskOutlookMQALL();*/ + + } +} diff --git a/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java index ab73a31..877df43 100644 --- a/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java +++ b/src/main/java/io/qyi/e5/service/task/impl/TaskImpl.java @@ -12,6 +12,7 @@ import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @@ -41,6 +42,9 @@ public class TaskImpl implements ITask { @Autowired IOutlookLogService outlookLogService; + @Value("${outlook.error.countMax}") + int errorCountMax; + @Override @Async public void sendTaskOutlookMQ(int github_id) { @@ -83,15 +87,31 @@ public class TaskImpl implements ITask { return false; } boolean isExecuteE5 ; + String errorKey = "user.mq:" + github_id + ":error"; try { int mail_count = outlookService.getMailList(Outlook); outlookLogService.addLog(github_id, "ok", 1, "读取邮件数量:" + mail_count); + if (redisUtil.hasKey(errorKey)) { + redisUtil.del(errorKey); + } isExecuteE5 = true; } catch (Exception e) { e.printStackTrace(); - outlookLogService.addLog(github_id, "error", 0, e.getMessage()); - outlookLogService.addLog(github_id, "error", 0, "检测到错误,下次将不再自动调用,请修正错误后再授权开启续订。" ); - isExecuteE5 = false; + /*连续错误判断*/ + if (!redisUtil.hasKey(errorKey)) { + redisUtil.set(errorKey, 1); + isExecuteE5 = true; + } else { + int error_count = (int)redisUtil.get(errorKey); + if (error_count >= errorCountMax) { + outlookLogService.addLog(github_id, "error", 0, e.getMessage()); + outlookLogService.addLog(github_id, "error", 0, "检测到3次连续错误,下次将不再自动调用,请修正错误后再授权开启续订。"); + isExecuteE5 = false; + } else { + redisUtil.incr(errorKey, 1); + isExecuteE5 = true; + } + } } return isExecuteE5; } diff --git a/src/main/java/io/qyi/e5/util/redis/RedisUtil.java b/src/main/java/io/qyi/e5/util/redis/RedisUtil.java index ec89cf2..3ec1ac7 100644 --- a/src/main/java/io/qyi/e5/util/redis/RedisUtil.java +++ b/src/main/java/io/qyi/e5/util/redis/RedisUtil.java @@ -51,11 +51,12 @@ public class RedisUtil { /** * 删除所有键值对 + * + * @throws * @title deleteALl * @description * @author 落叶随风 * @updateTime 2020/4/22 22:53 - * @throws */ public void deleteALL() { Set keys = redisTemplate.keys("*"); @@ -581,7 +582,9 @@ public class RedisUtil { return 0; } } + /** + * @throws * @title lRemove * @description * @author 落叶随风 @@ -590,11 +593,10 @@ public class RedisUtil { * @param: value * @updateTime 2020/2/4 14:59 * @return: long - * @throws */ public boolean lTrim(String key, long start, long end) { try { - redisTemplate.opsForList().trim(key,start,end); + redisTemplate.opsForList().trim(key, start, end); return true; } catch (Exception e) { e.printStackTrace(); @@ -602,4 +604,12 @@ public class RedisUtil { } } -} + public void reName(String old, String new_key) { + redisTemplate.rename(old, new_key); + } + + public void delAll() { + Set keys = redisTemplate.keys("*"); + redisTemplate.delete(keys); + } +} \ No newline at end of file