当前位置:网站首页>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()
}
}
}()
边栏推荐
- What does soda ash do?
- The SQL implementation has multiple records with the same ID, and the latest one is taken
- NPM and package common commands
- ROS2——初识ROS2(一)
- D2L installation
- The difference between NPM install -g/-save/-save-dev
- Altimeter data knowledge point 2
- Docker installs MySQL and uses Navicat to connect
- npm install -g/--save/--save-dev的区别
- [software testing] 06 -- basic process of software testing
猜你喜欢

2022年PMP项目管理考试敏捷知识点(7)

逻辑结构与物理结构

Machine learning Seaborn visualization

Ros2 - function package (VI)

Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL

Ros2 - install ros2 (III)

你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?

Ros2 - configuration development environment (V)

Netease to B, soft outside, hard in

U-boot initialization and workflow analysis
随机推荐
[software testing] 02 -- software defect management
全局变量和静态变量的初始化
Altimeter data knowledge point 2
1290_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
2022年PMP项目管理考试敏捷知识点(7)
Typescript get timestamp
[software testing] 05 -- principles of software testing
【软件测试】03 -- 软件测试概述
DelayQueue延迟队列的使用和场景
Implementation of one-dimensional convolutional neural network CNN based on FPGA (VIII) implementation of activation layer
How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
[idea] efficient plug-in save actions to improve your work efficiency
ROS2——安装ROS2(三)
Import CV2 prompt importerror: libgl so. 1: Cannot open shared object file: no such file or directory
[untitled]
window navicat连接阿里云服务器mysql步骤及常见问题
Unity 之 ExecuteAlways正在取代ExecuteInEditMode
Special training of C language array
【obs】x264编码:“buffer_size“
睿智的目标检测59——Pytorch Focal loss详解与在YoloV4当中的实现