当前位置:网站首页>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()
}
}
}()
边栏推荐
- 【Node】npm、yarn、pnpm 区别
- 【软件测试】05 -- 软件测试的原则
- 目标检测系列——Faster R-CNN原理详解
- DataGrid offline installation of database driver
- [nvidia] CUDA_ VISIBLE_ DEVICES
- ImportError: No module named ‘Tkinter‘
- Use of Pai platform
- ROS2——node节点(七)
- Error: "mountvolume.setup failed for volume PVC fault handling
- [solved] there is something wrong with the image
猜你喜欢

M2dgr slam data set of multi-source and multi scene ground robot

Ros2 topic (VIII)

Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘

C#学习笔记

What if the DataGrid cannot see the table after connecting to the database

ROS2——初识ROS2(一)

U-boot initialization and workflow analysis

SOC_ SD_ DATA_ FSM

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

U-Boot初始化及工作流程分析
随机推荐
Ros2 - install ros2 (III)
MySQL setting trigger problem
Ros2 - workspace (V)
testing framework
[software testing] 04 -- software testing and software development
Negative number storage and type conversion in programs
SD_CMD_SEND_SHIFT_REGISTER
Skywalking all
苏打粉是什么?
DelayQueue延迟队列的使用和场景
C语言数组专题训练
Netease to B, soft outside, hard in
Matlab在线性代数中的应用(四):相似矩阵及二次型
【无标题】
ROS2——初识ROS2(一)
Ros2 - ros2 vs. ros1 (II)
What is sodium hydroxide?
Energy conservation and creating energy gap
[software testing] 03 -- overview of software testing
What is soda?