当前位置:网站首页>golang定时器使用踩的坑:定时器每天执行一次
golang定时器使用踩的坑:定时器每天执行一次
2022-07-05 07:15:00 【java_xxxx】
项目背景以及问题
项目中有这样一个需求,需要用定时器在每天的上午10:00:00处理某些业务逻辑,但是遇到了2个问题。
不比比,上代码
go func() {
for {
now := time.Now()
var next time.Time
//每天10:00与苏轼论道
if now.Hour() < 10 || now.Hour() == 10 {
next = now
} else {
next = now.Add(time.Hour * 24)
}
//每天10:00与苏轼论道
next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location())
// 定时时间已过,防止项目重启时,再次触发定时器。如10点执行一次,10:10重启了服务,此时设置为下一天执行
logger.Info("UpdateOndutys Now=%s Next=%s", now, next)
timer := time.NewTimer(next.Sub(now))
select {
case ts := <-timer.C:
// go function() //业务逻辑
logger.Info("Start UpdateOnduty_7day ts=%s", ts.String())
time.Sleep(120 * time.Second)
timer.Stop()
}
}
}()
问题1:golang定时器有一个特点就是你设置一个过期的时间它仍然会执行,比如,当前是上午10:00:00,然后我设置定时器是今天的9:00:00去执行,它仍然会执行。
问题2:如果定时器是上午10:00:00执行,然后我10:44:44重启了项目,然后它就又执行一次,但是需求是一天只执行一次。
解决方案
在我们启动定时器之前判断一下当前时间与定时器的执行时间大小,如果当前时间大于定时器执行时间,说明定时器已经执行过,那么就设置为明天再执行。如果当前时间小于定时器时间那么定时器未执行过,按照正常逻辑今日执行。
go func() {
for {
now := time.Now()
var next time.Time
//每天10:00与苏轼论道
if now.Hour() < 10 || now.Hour() == 10 {
next = now
} else {
next = now.Add(time.Hour * 24)
}
//每天10:00与苏轼论道
next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location())
// 定时时间已过,防止项目重启时,再次触发定时器。如10点执行一次,10:10重启了服务,此时设置为下一天执行
if now.Unix() > next.Unix() {
next = now.Add(time.Hour * 24)
next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location())
}
logger.Info("UpdateOndutys Now=%s Next=%s", now, next)
timer := time.NewTimer(next.Sub(now))
select {
case ts := <-timer.C:
// go function() //业务逻辑
logger.Info("Start UpdateOnduty_7day ts=%s", ts.String())
time.Sleep(120 * time.Second)
timer.Stop()
}
}
}()
边栏推荐
- Unity ugui how to match and transform coordinates between different UI panels or uis
- Build a microservice cluster environment locally and learn to deploy automatically
- D2L installation
- Three body goal management notes
- Concurrent programming - deadlock troubleshooting and handling
- Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)
- Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
- Mathematical analysis_ Notes_ Chapter 8: multiple integral
- [framework] multi learner
- Database SQL practice 4. Find the last of employees in all assigned departments_ Name and first_ name
猜你喜欢

ROS2——功能包(六)

C语言数组专题训练

SOC_SD_DATA_FSM

【Node】nvm 版本管理工具

PHY drive commissioning - phy controller drive (II)

Ros2 - first acquaintance with ros2 (I)

睿智的目标检测59——Pytorch Focal loss详解与在YoloV4当中的实现

ROS2——初识ROS2(一)
![[idea] efficient plug-in save actions to improve your work efficiency](/img/6e/49037333964865d9900ddf5698f7e6.jpg)
[idea] efficient plug-in save actions to improve your work efficiency
![[node] NVM version management tool](/img/26/f13a2451c2f177a86bcb2920936468.png)
[node] NVM version management tool
随机推荐
现在有html文件,和用vs制作的mvc(连接了数据库),怎么两个相连?
Database SQL practice 3. Find the current salary details of the current leaders of each department and their corresponding department number Dept_ no
Ros2 topic (VIII)
Concurrent programming - deadlock troubleshooting and handling
ROS2——配置开发环境(五)
Mathematical analysis_ Notes_ Chapter 8: multiple integral
SOC_ SD_ DATA_ FSM
Binary search (half search)
Initialization of global and static variables
What does soda ash do?
[idea] efficient plug-in save actions to improve your work efficiency
PowerManagerService(一)— 初始化
数学分析_笔记_第8章:重积分
Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
[framework] multi learner
能量守恒和打造能量缺口
The difference between new and malloc
Brief description of inux camera (Mipi interface)