Golang: The matter that you want to execute the process at exactly 00:00 or 30 minutes at the specified interval.

The ticker doesn't work as expected!

There is a job that I want to be resident and execute regularly, but if it is a normal ticker, the interval count starts from the time the ticker is created after starting it, so the time cannot be set exactly. Moreover, even if the ticker was set to interval so that it would be 12:00:00, it would occasionally move at 11: 59: 59.999, and the processed data would be duplicated and registered.

I also wrote how to use the ticker and practice time control.

https://github.com/xbridges/Scheduler Simply, the ticker will call it exactly at the specified time interval.

ex) sch, next := NewScheduler(3600, 0)

If you start like this, the ticker will fire once every hour. Furthermore, if you use the offset value, the interval is every hour (3600), but if you set offset: 600, the ticker will fire at 05 minutes every hour.

I wrote it awesomely, but what I did was just rap (laughs)

I'm just wrapping the ticker, but Kimo

  is := time.Duration(interval) * time.Second  // <--Set the specified time to Duration
  n := time.Now()   // <--Current time
  ns := n.Round(is) // <--It feels like masking the current time with an interval time and rounding it.

If you do this, the ticker will occasionally move early, and even if it reaches 59 minutes 59 seconds .9999, it will be rounded off, so the time will properly fire at the next 00 minutes.

I made a note because it was running regularly and I was a little addicted to it.

reference

http://okzk.hatenablog.com/entry/2015/12/01/001924

Recommended Posts

Golang: The matter that you want to execute the process at exactly 00:00 or 30 minutes at the specified interval.
When you want to adjust the axis scale interval with APLpy
[Python3] Code that can be used when you want to change the extension of an image at once
The story of IPv6 address that I want to keep at a minimum
When you want to erase characters at once in terminal or line unit
When you want to update the chrome driver.
Do you want me to fix that copy?