仅在工作日运行cron任务

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

Run cron on weekdays only

问题

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

package main

import (
    "fmt"
    "time"
)

const INTERVAL_PERIOD time.Duration = 24 * time.Hour

const HOUR_TO_TICK int = 23
const MINUTE_TO_TICK int = 21
const SECOND_TO_TICK int = 03

type jobTicker struct {
    t *time.Timer
}

func getNextTickDuration() time.Duration {
    now := time.Now()
    nextTick := time.Date(now.Year(), now.Month(), now.Day(), HOUR_TO_TICK, MINUTE_TO_TICK, SECOND_TO_TICK, 0, time.Local)
    if nextTick.Before(now) {
        nextTick = nextTick.Add(INTERVAL_PERIOD)
    }
    return nextTick.Sub(time.Now())
}

func NewJobTicker() jobTicker {
    fmt.Println("new tick here")
    return jobTicker{time.NewTimer(getNextTickDuration())}
}

func (jt jobTicker) updateJobTicker() {
    fmt.Println("next tick here")
    jt.t.Reset(getNextTickDuration())
}

func main() {
    jt := NewJobTicker()
    for {
        <-jt.t.C
        fmt.Println(time.Now(), "- just ticked")
        jt.updateJobTicker()
    }
}

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

英文:

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.

package main

import (
    &quot;fmt&quot;
    &quot;time&quot;
)

const INTERVAL_PERIOD time.Duration = 24 * time.Hour

const HOUR_TO_TICK int = 23
const MINUTE_TO_TICK int = 21
const SECOND_TO_TICK int = 03

type jobTicker struct {
    t *time.Timer
}

func getNextTickDuration() time.Duration {
    now := time.Now()
    nextTick := time.Date(now.Year(), now.Month(), now.Day(), HOUR_TO_TICK, MINUTE_TO_TICK, SECOND_TO_TICK, 0, time.Local)
    if nextTick.Before(now) {
        nextTick = nextTick.Add(INTERVAL_PERIOD)
    }
    return nextTick.Sub(time.Now())
}

func NewJobTicker() jobTicker {
    fmt.Println(&quot;new tick here&quot;)
    return jobTicker{time.NewTimer(getNextTickDuration())}
}

func (jt jobTicker) updateJobTicker() {
    fmt.Println(&quot;next tick here&quot;)
    jt.t.Reset(getNextTickDuration())
}

func main() {
    jt := NewJobTicker()
    for {
        &lt;-jt.t.C
        fmt.Println(time.Now(), &quot;- just ticked&quot;)
        jt.updateJobTicker()
    }
}

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

答案1

得分: 2

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

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

请注意,这是一个示例代码,其中的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

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

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:

确定