仅在工作日运行cron任务

huangapple go评论119阅读模式
英文:

Run cron on weekdays only

问题

目前有以下的cron代码,来源于这里。我想让它只在工作日运行。

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. const INTERVAL_PERIOD time.Duration = 24 * time.Hour
  7. const HOUR_TO_TICK int = 23
  8. const MINUTE_TO_TICK int = 21
  9. const SECOND_TO_TICK int = 03
  10. type jobTicker struct {
  11. t *time.Timer
  12. }
  13. func getNextTickDuration() time.Duration {
  14. now := time.Now()
  15. nextTick := time.Date(now.Year(), now.Month(), now.Day(), HOUR_TO_TICK, MINUTE_TO_TICK, SECOND_TO_TICK, 0, time.Local)
  16. if nextTick.Before(now) {
  17. nextTick = nextTick.Add(INTERVAL_PERIOD)
  18. }
  19. return nextTick.Sub(time.Now())
  20. }
  21. func NewJobTicker() jobTicker {
  22. fmt.Println("new tick here")
  23. return jobTicker{time.NewTimer(getNextTickDuration())}
  24. }
  25. func (jt jobTicker) updateJobTicker() {
  26. fmt.Println("next tick here")
  27. jt.t.Reset(getNextTickDuration())
  28. }
  29. func main() {
  30. jt := NewJobTicker()
  31. for {
  32. <-jt.t.C
  33. fmt.Println(time.Now(), "- just ticked")
  34. jt.updateJobTicker()
  35. }
  36. }

如何使其只在工作日运行?

英文:

Currently have this cron code below which is from here Trying to make this can be so it can run the job only on weekdays only.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;time&quot;
  5. )
  6. const INTERVAL_PERIOD time.Duration = 24 * time.Hour
  7. const HOUR_TO_TICK int = 23
  8. const MINUTE_TO_TICK int = 21
  9. const SECOND_TO_TICK int = 03
  10. type jobTicker struct {
  11. t *time.Timer
  12. }
  13. func getNextTickDuration() time.Duration {
  14. now := time.Now()
  15. nextTick := time.Date(now.Year(), now.Month(), now.Day(), HOUR_TO_TICK, MINUTE_TO_TICK, SECOND_TO_TICK, 0, time.Local)
  16. if nextTick.Before(now) {
  17. nextTick = nextTick.Add(INTERVAL_PERIOD)
  18. }
  19. return nextTick.Sub(time.Now())
  20. }
  21. func NewJobTicker() jobTicker {
  22. fmt.Println(&quot;new tick here&quot;)
  23. return jobTicker{time.NewTimer(getNextTickDuration())}
  24. }
  25. func (jt jobTicker) updateJobTicker() {
  26. fmt.Println(&quot;next tick here&quot;)
  27. jt.t.Reset(getNextTickDuration())
  28. }
  29. func main() {
  30. jt := NewJobTicker()
  31. for {
  32. &lt;-jt.t.C
  33. fmt.Println(time.Now(), &quot;- just ticked&quot;)
  34. jt.updateJobTicker()
  35. }
  36. }

How can I make this only run on the weekdays only ?

答案1

得分: 2

你应该在**getNextTickDuration()**函数中检查星期几。代码可能如下所示:

  1. func getNextTickDuration() time.Duration {
  2. now := time.Now()
  3. nextTick := time.Date(now.Year(), now.Month(), now.Day(), HOUR_TO_TICK, MINUTE_TO_TICK, SECOND_TO_TICK, 0, time.Local)
  4. for nextTick.Before(now) || nextTick.Weekday() == time.Saturday || nextTick.Weekday() == time.Sunday {
  5. nextTick = nextTick.Add(INTERVAL_PERIOD)
  6. }
  7. return nextTick.Sub(time.Now())
  8. }

请注意,这是一个示例代码,其中的HOUR_TO_TICKMINUTE_TO_TICKSECOND_TO_TICKINTERVAL_PERIOD是需要根据你的具体需求进行定义的变量。

英文:

You should check for day of the week inside your getNextTickDuration() function. It may look like this

  1. func getNextTickDuration() time.Duration {
  2. now := time.Now()
  3. nextTick := time.Date(now.Year(), now.Month(), now.Day(), HOUR_TO_TICK, MINUTE_TO_TICK, SECOND_TO_TICK, 0, time.Local)
  4. for nextTick.Before(now) || nextTick.Weekday() == time.Saturday || nextTick.Weekday() == time.Sunday {
  5. nextTick = nextTick.Add(INTERVAL_PERIOD)
  6. }
  7. return nextTick.Sub(time.Now())
  8. }

huangapple
  • 本文由 发表于 2022年11月5日 05:15:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/74323034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定