当前位置:网站首页>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()
}
}
}()
边栏推荐
猜你喜欢
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
U-boot initialization and workflow analysis
第 2 章:小试牛刀,实现一个简单的Bean容器
Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘
Ros2 - node (VII)
SD_ CMD_ SEND_ SHIFT_ REGISTER
Mipi interface, DVP interface and CSI interface of camera
[software testing] 03 -- overview of software testing
PostMessage communication
数学分析_笔记_第8章:重积分
随机推荐
解读最早的草图-图像翻译工作SketchyGAN
C语言数组专题训练
arcpy. SpatialJoin_ Analysis spatial connection analysis
ROS2——功能包(六)
What is sodium hydroxide?
Brief description of inux camera (Mipi interface)
Delayqueue usage and scenarios of delay queue
Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘
NPM and package common commands
The difference between new and malloc
【idea】Could not autowire. No beans of xxx type found
GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
Logical structure and physical structure
Unity 之 ExecuteAlways正在取代ExecuteInEditMode
【软件测试】04 -- 软件测试与软件开发
[untitled]
[node] differences among NPM, yarn and pnpm
SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
An article was opened to test the real situation of outsourcing companies
ROS2——topic话题(八)