当前位置:网站首页>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()
}
}
}()
边栏推荐
- Interpretation of the earliest sketches - image translation work sketchygan
- HDU1232 畅通工程(并查集)
- Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
- Ros2 - Service Service (IX)
- What is soda?
- Lexin interview process
- ROS2——初识ROS2(一)
- Course learning accumulation ppt
- Mathematical analysis_ Notes_ Chapter 8: multiple integral
- ROS2——工作空间(五)
猜你喜欢
[vscode] prohibit the pylance plug-in from automatically adding import
Target detection series - detailed explanation of the principle of fast r-cnn
[software testing] 03 -- overview of software testing
DelayQueue延迟队列的使用和场景
SD_ CMD_ SEND_ SHIFT_ REGISTER
Logical structure and physical structure
Ethtool principle introduction and troubleshooting ideas for network card packet loss (with ethtool source code download)
postmessage通信
Altimeter data knowledge point 2
ROS2——功能包(六)
随机推荐
ROS2——ROS2对比ROS1(二)
Three body goal management notes
Mathematical analysis_ Notes_ Chapter 8: multiple integral
[software testing] 06 -- basic process of software testing
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
Mid 2022 documentary -- the experience of an ordinary person
D2L installation
Tshydro tool
[tf1] save and load parameters
【Node】nvm 版本管理工具
An article was opened to test the real situation of outsourcing companies
Ros2 - first acquaintance with ros2 (I)
Import CV2 prompt importerror: libgl so. 1: Cannot open shared object file: no such file or directory
[node] differences among NPM, yarn and pnpm
二分查找(折半查找)
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
【软件测试】06 -- 软件测试的基本流程
网易To B,柔外刚中
目标检测系列——Faster R-CNN原理详解
M2dgr slam data set of multi-source and multi scene ground robot