当前位置:网站首页>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()
}
}
}()
边栏推荐
- MySQL setting trigger problem
- Chapter 2: try to implement a simple bean container
- [software testing] 03 -- overview of software testing
- What is soda?
- 2022年中纪实 -- 一个普通人的经历
- Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
- Logical structure and physical structure
- 氫氧化鈉是什麼?
- Now there are HTML files and MVC made with vs (connected to the database). How can they be connected?
- Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
猜你喜欢

ROS2——初识ROS2(一)

Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘

window navicat连接阿里云服务器mysql步骤及常见问题

并发编程 — 如何中断/停止一个运行中的线程?

SOC_ SD_ CMD_ FSM

Logical structure and physical structure

Brief description of inux camera (Mipi interface)

Special training of C language array
![[software testing] 03 -- overview of software testing](/img/1e/0b6458160e34e43f021ea4797de70a.jpg)
[software testing] 03 -- overview of software testing

PHY驱动调试之 --- PHY控制器驱动(二)
随机推荐
inux摄像头(mipi接口)简要说明
[solved] there is something wrong with the image
【obs】x264编码:“buffer_size“
Energy conservation and creating energy gap
PostMessage communication
In C language, int a= 'R'
基于FPGA的一维卷积神经网络CNN的实现(八)激活层实现
What if the DataGrid cannot see the table after connecting to the database
testing framework
Concurrent programming - deadlock troubleshooting and handling
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial
NPM and package common commands
Xiaomi written test real question 1
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
[framework] multi learner
SOC_SD_DATA_FSM
Interpretation of the earliest sketches - image translation work sketchygan
Spinningup drawing curve
ImportError: No module named ‘Tkinter‘
PHY drive commissioning - phy controller drive (II)