当前位置:网站首页>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()
}
}
}()
边栏推荐
- 数学分析_笔记_第8章:重积分
- [node] differences among NPM, yarn and pnpm
- 基于Cortex-M3、M4的GPIO口位带操作宏定义(可总线输入输出,可用于STM32、ADuCM4050等)
- What is sodium hydroxide?
- [software testing] 05 -- principles of software testing
- ROS2——常用命令行(四)
- What is soda?
- PHY drive commissioning - phy controller drive (II)
- Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
- Ros2 - first acquaintance with ros2 (I)
猜你喜欢

Ros2 - configuration development environment (V)

三体目标管理笔记

Tshydro tool

一文揭开,测试外包公司的真实情况

Ros2 - workspace (V)

PHY drive commissioning --- mdio/mdc interface Clause 22 and 45 (I)

DataGrid offline installation of database driver

SOC_ SD_ CMD_ FSM

逻辑结构与物理结构

Mathematical analysis_ Notes_ Chapter 8: multiple integral
随机推荐
[software testing] 05 -- principles of software testing
Oracle code use
Initialization of global and static variables
[vscode] search using regular expressions
网易To B,柔外刚中
ROS2——常用命令行(四)
【idea】Could not autowire. No beans of xxx type found
ROS2——功能包(六)
[solved] there is something wrong with the image
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
第 2 章:小试牛刀,实现一个简单的Bean容器
[vscode] prohibit the pylance plug-in from automatically adding import
Ros2 - common command line (IV)
npm install -g/--save/--save-dev的区别
基于FPGA的一维卷积神经网络CNN的实现(八)激活层实现
iNFTnews | 喝茶送虚拟股票?浅析奈雪的茶“发币”
Ros2 - configuration development environment (V)
[idea] efficient plug-in save actions to improve your work efficiency
Unity UGUI不同的UI面板或者UI之间如何进行坐标匹配和变换
window navicat连接阿里云服务器mysql步骤及常见问题