当前位置:网站首页>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()
}
}
}()
边栏推荐
- Reading literature sorting 20220104
- SOC_SD_DATA_FSM
- ORACLE CREATE SEQUENCE,ALTER SEQUENCE,DROP SEQUENCE
- Binary search (half search)
- How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
- [vscode] recommended plug-ins
- Xiaomi written test real question 1
- PowerManagerService(一)— 初始化
- 基于FPGA的一维卷积神经网络CNN的实现(八)激活层实现
- Literacy Ethernet MII interface types Daquan MII, RMII, smii, gmii, rgmii, sgmii, XGMII, XAUI, rxaui
猜你喜欢
![[software testing] 04 -- software testing and software development](/img/bd/49bba7ee455ce59e726a2fdeafc7c3.jpg)
[software testing] 04 -- software testing and software development

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

ROS2——安装ROS2(三)

Ros2 - install ros2 (III)

Ros2 - common command line (IV)

1290_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析

【软件测试】02 -- 软件缺陷管理

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

C learning notes

Chapter 2: try to implement a simple bean container
随机推荐
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
Logical structure and physical structure
arcpy. SpatialJoin_ Analysis spatial connection analysis
Intelligent target detection 59 -- detailed explanation of pytoch focal loss and its implementation in yolov4
DataGrid offline installation of database driver
[tf1] save and load parameters
Ros2 - node (VII)
Solve tensorfow GPU modulenotfounderror: no module named 'tensorflow_ core. estimator‘
PHY drive commissioning - phy controller drive (II)
Three body goal management notes
M2dgr slam data set of multi-source and multi scene ground robot
【Node】npm、yarn、pnpm 区别
小米笔试真题一
Install deeptools in CONDA mode
What if the DataGrid cannot see the table after connecting to the database
Inftnews | drink tea and send virtual stocks? Analysis of Naixue's tea "coin issuance"
纯碱是做什么的?
一文揭开,测试外包公司的真实情况
ROS2——功能包(六)
[tf] Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initial