当前位置:网站首页>The golang timer uses the stepped pit: the timer is executed once a day
The golang timer uses the stepped pit: the timer is executed once a day
2022-07-05 07:17:00 【java_ xxxx】
golang The timer uses a stepping pit
Project background and problems
There is such a requirement in the project , You need to use a timer every morning 10:00:00 Handle some business logic , But I met 2 A question .
Bubibi , Code up
go func() {
for {
now := time.Now()
var next time.Time
// Every day 10:00 On Tao with Su Shi
if now.Hour() < 10 || now.Hour() == 10 {
next = now
} else {
next = now.Add(time.Hour * 24)
}
// Every day 10:00 On Tao with Su Shi
next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location())
// The fixed time has passed , Prevent project restart , Trigger the timer again . Such as 10 Click to execute once ,10:10 Restart the service , At this time, it is set to execute on the next day
logger.Info("UpdateOndutys Now=%s Next=%s", now, next)
timer := time.NewTimer(next.Sub(now))
select {
case ts := <-timer.C:
// go function() // Business logic
logger.Info("Start UpdateOnduty_7day ts=%s", ts.String())
time.Sleep(120 * time.Second)
timer.Stop()
}
}
}()
problem 1:golang One feature of the timer is that you set an expiration time, and it will still execute , such as , It's morning now 10:00:00, Then I set the timer today 9:00:00 To carry out , It will still execute .
problem 2: If the timer is in the morning 10:00:00 perform , Then I 10:44:44 Project restarted , Then it executes again , But requirements are implemented only once a day .
Solution
Before we start the timer, judge the current time and the execution time of the timer , If the current time is greater than the timer execution time , It indicates that the timer has been executed , Then set it to execute tomorrow . If the current time is less than the timer time, the timer has not been executed , Follow the normal logic today .
go func() {
for {
now := time.Now()
var next time.Time
// Every day 10:00 On Tao with Su Shi
if now.Hour() < 10 || now.Hour() == 10 {
next = now
} else {
next = now.Add(time.Hour * 24)
}
// Every day 10:00 On Tao with Su Shi
next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location())
// The fixed time has passed , Prevent project restart , Trigger the timer again . Such as 10 Click to execute once ,10:10 Restart the service , At this time, it is set to execute on the next day
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() // Business logic
logger.Info("Start UpdateOnduty_7day ts=%s", ts.String())
time.Sleep(120 * time.Second)
timer.Stop()
}
}
}()
边栏推荐
- Target detection series - detailed explanation of the principle of fast r-cnn
- ROS2——初识ROS2(一)
- PostMessage communication
- golang定时器使用踩的坑:定时器每天执行一次
- Unity 之 ExecuteAlways正在取代ExecuteInEditMode
- 二分查找(折半查找)
- [vscode] search using regular expressions
- Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
- SOC_SD_CMD_FSM
- 睿智的目标检测59——Pytorch Focal loss详解与在YoloV4当中的实现
猜你喜欢

Do you choose pandas or SQL for the top 1 of data analysis in your mind?

【软件测试】03 -- 软件测试概述

IPage can display data normally, but total is always equal to 0

Delayqueue usage and scenarios of delay queue

并发编程 — 死锁排查及处理

M2DGR 多源多场景 地面机器人SLAM数据集

Logical structure and physical structure

SD_CMD_RECEIVE_SHIFT_REGISTER

Special training of C language array

Ros2 - install ros2 (III)
随机推荐
【软件测试】03 -- 软件测试概述
能量守恒和打造能量缺口
M2dgr slam data set of multi-source and multi scene ground robot
GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
testing framework
Unity 之 ExecuteAlways正在取代ExecuteInEditMode
Unity UGUI不同的UI面板或者UI之间如何进行坐标匹配和变换
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
2022.06.27_ One question per day
How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
PHY drive commissioning - phy controller drive (II)
R language learning notes 1
mingling
SD_CMD_SEND_SHIFT_REGISTER
[vscode] search using regular expressions
Database SQL practice 4. Find the last of employees in all assigned departments_ Name and first_ name
npm install -g/--save/--save-dev的区别
[vscode] recommended plug-ins
Oracle code use
基于FPGA的一维卷积神经网络CNN的实现(八)激活层实现