系列文章目录
背景
基于 Spring Boot 开发的项目中,使用 @Scheduled(cron = "0 0 1 * * ?")
定义的定时任务,在正常运行一段时间后,突然停止运行,系统也没有报错,系统也能正常提供服务没有假死。
按如下思路逐一排查
- jar 包版本冲突(刚上线了新版本)
- 定时任务出现异常,但是异常被丢了
- 数据量过大,执行时间过长
经过一上午的排查,排除了以上 3 点可能的原因,最终发现 @EnableScheduling
注解被删除了!!!😓😓😓😓😓😓
出现了这么低级的失误,坑定是要找出责任人的,那么如何找呢?本文提供 2 种方式
Blame
使用 git blame
查看最后一次修改信息
git blame -L <start>,<end> path/to/file
查看指定文件的指定行号范围的最后一次修改信息
出问题的代码是 src/main/java/com/example/Application.java
的 47
行,所以命令如下:
git blame -L 40,50 src/main/java/com/example/Application.java
输出结果如下:
23c169d949 (zhang.san 2023-03-08 16:31:44 +0800 40) com.example.basic.model.DateFormatter.class,
23c169d949 (zhang.san 2023-03-08 16:31:44 +0800 41) com.example.basic.exception.GlobalExceptionHandler.class}
23c169d949 (zhang.san 2023-03-08 16:31:44 +0800 42) )})
8006f6c954 (li.si 2021-10-27 17:41:19 +0800 43) @SpringBootApplication
965c9697a9 (zhang.san 2022-05-09 17:22:44 +0800 44) @EnableYLResourceServer
b6a6a2ee06 (zhang.san 2021-12-04 13:54:41 +0800 45) @EnableCaching
b6a6a2ee06 (zhang.san 2021-12-04 13:54:41 +0800 46) @EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
f28ad73a65 (zhao.liu 2023-07-11 16:23:08 +0800 47) //@EnableScheduling
8006f6c954 (li.si 2021-10-27 17:41:19 +0800 48) @Slf4j
927befc6b4 (wang.wu 2022-03-29 14:45:17 +0800 49) @EnableJpaAuditing
927befc6b4 (wang.wu 2022-03-29 14:45:17 +0800 50) @EntityScan("com.example.biz.model.entity")
找到了注释 @EnableScheduling
注解的责任人 zhao.liu
Log
使用 git log
查看修改历史
git log -S 'str' -p path/to/file
在提交历史中查找指定的文件中包含特定字符串的代码更改,并显示相关的变更内容
出问题的代码是 src/main/java/com/example/Application.java
中的 //@EnableScheduling
所以命令如下:
git log -S '//@EnableScheduling' -p src/main/java/com/example/Application.java
输出结果如下:
commit 866f388b8b7f4cf43b16e146e120d9c0746ccc10
Author: zhao.liu <zhao.liu@example.email.com>
Date: Thu Jul 6 14:51:22 2023 +0800
int excludeTenantInterceptor
diff --git a/server/src/main/java/com/example/server/Application.java b/server/src/main/java/com/example/server/Application.java
index 5fd94ae6e..174d1657c 100644
--- a/server/src/main/java/com/example/server/Application.java
+++ b/server/src/main/java/com/example/server/Application.java
@@ -14,7 +14,6 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
-import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;
@@ -44,7 +43,7 @@ import tk.mybatis.spring.annotation.MapperScan;
@EnableYLResourceServer
@EnableCaching
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
-@EnableScheduling
+//@EnableScheduling
@Slf4j
@EnableJpaAuditing
@EntityScan("com.example.biz.model.entity")
找到了注释 @EnableScheduling
注解的责任人 zhao.liu
总结
git blame
最常用,可以直接找到修改者
git log
不单单可以找到修改者,还可以查看有没有发生过类似的事情,以防微杜渐