Go 如何在每天的特定时间运行作业?

Go 如何在每天的特定时间运行作业?,go,time,Go,Time,我想每天晚上9点打印出我的工作。我怎么能在围棋中做到这一点 以下是到目前为止我得到的信息: timer:=time.NewTimer(3*time.Second) 为了{ 现在:=时间。现在() next:=now.Add(time.Hour*24) todayNine:=time.Date(next.Year(),next.Month(),next.Day(),9,0,0,0,next.Location()).AddDate(0,0,-1) Today十五:=time.Date(next.Ye

我想每天晚上9点打印出我的工作。我怎么能在围棋中做到这一点

以下是到目前为止我得到的信息:

timer:=time.NewTimer(3*time.Second)
为了{
现在:=时间。现在()
next:=now.Add(time.Hour*24)
todayNine:=time.Date(next.Year(),next.Month(),next.Day(),9,0,0,0,next.Location()).AddDate(0,0,-1)
Today十五:=time.Date(next.Year(),next.Month(),next.Day(),15,0,0,0,next.Location()).AddDate(0,0,-1)
todayEnd:=time.Date(下一年(),下一个月(),下一天(),0,0,0,0,下一个位置()).AddDate(0,0,-1)
如果现在。之前(今天九点){
计时器重置(今天九点(现在))
}否则,如果现在。在(今天15号)之前{
计时器重置(今天15.Sub(现在))
}否则,如果现在。之前(今天){
计时器重置(todayEnd.Sub(现在))
}

我将探索操作系统级或基础设施级系统,以在这些时候触发执行(Cron作业在*nix中,Cron在k8s中等等)

但是,如果您想纯粹使用
go
,您可以尝试同时使用
ticker
Clock

package main

import (
    "context"
    "fmt"
    "os"
    "time"
    "os/signal"
)

func main() {
    ticker := time.NewTicker(time.Minute)
    done := make(chan bool)
    ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
    defer stop()
    go func() {
        for {
            select {
            case <-done:
                return
            case <-ticker.C:
                h, m, _ := time.Now().Clock()
                if m == 0 && (  h == 9 || h == 15 ) {
                    fmt.Printf("Doing the job")
                }
            }
        }
    }()

    <-ctx.Done()
    stop()
    done <- true
}
主程序包
进口(
“上下文”
“fmt”
“操作系统”
“时间”
“操作系统/信号”
)
func main(){
股票代码:=time.NewTicker(time.Minute)
完成:=制作(陈波)
ctx,stop:=signal.NotifyContext(context.Background(),os.Interrupt)
延迟停止()
go func(){
为了{
挑选{

case我将探索操作系统级或基础架构级系统,以在这些时间触发执行(Cron作业在*nix中,Cron在k8s中,等等)

但是,如果您想纯粹使用
go
,您可以尝试同时使用
ticker
Clock

package main

import (
    "context"
    "fmt"
    "os"
    "time"
    "os/signal"
)

func main() {
    ticker := time.NewTicker(time.Minute)
    done := make(chan bool)
    ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
    defer stop()
    go func() {
        for {
            select {
            case <-done:
                return
            case <-ticker.C:
                h, m, _ := time.Now().Clock()
                if m == 0 && (  h == 9 || h == 15 ) {
                    fmt.Printf("Doing the job")
                }
            }
        }
    }()

    <-ctx.Done()
    stop()
    done <- true
}
主程序包
进口(
“上下文”
“fmt”
“操作系统”
“时间”
“操作系统/信号”
)
func main(){
股票代码:=time.NewTicker(time.Minute)
完成:=制作(陈波)
ctx,stop:=signal.NotifyContext(context.Background(),os.Interrupt)
延迟停止()
go func(){
为了{
挑选{

case我会使用
cron

文档中的示例:

c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly",      func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()
..
// Funcs are invoked in their own goroutine, asynchronously.
...
// Funcs may also be added to a running Cron
c.AddFunc("@daily", func() { fmt.Println("Every day") })
..
// Inspect the cron job entries' next and previous run times.
inspect(c.Entries())
..
c.Stop()  // Stop the scheduler (does not stop any jobs already running).

我会使用
cron

文档中的示例:

c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly",      func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()
..
// Funcs are invoked in their own goroutine, asynchronously.
...
// Funcs may also be added to a running Cron
c.AddFunc("@daily", func() { fmt.Println("Every day") })
..
// Inspect the cron job entries' next and previous run times.
inspect(c.Entries())
..
c.Stop()  // Stop the scheduler (does not stop any jobs already running).

您可以部分时间使用ticker和check hour.Now()。但是如果您的服务有多个实例在运行,这将是一个棘手的问题。您最终会让每个实例执行一次作业。我建议使用go二进制文件之外的其他系统来启动作业。它可能是一个cron作业或k8 cron。如果您想纯粹使用go“ticker”来执行此操作应使用您的操作系统可能有一种方法,可以根据您的需要定期安排程序在特定时间运行。您使用的是什么操作系统?对不起,我没有尽快回复,这是我的解决方案,请让我知道您的想法,谢谢!您可以使用自动售票机并在部分时间检查小时。现在()。但是,如果有多个服务实例在运行,这将是一个棘手的问题。。每个实例都会执行一次作业。我建议使用go二进制文件之外的其他系统来启动作业。它可能是一个cron作业或k8 cron。如果您想纯粹使用go“ticker”来执行此操作应使用您的操作系统可能有一种方法,可以根据您的需要定期安排程序在特定时间运行。您使用的是什么操作系统?抱歉,我没有尽快回复,这是我的解决方案,请让我知道您的想法,谢谢!如果您停止并重新运行服务器,将重新启动票证?因此使用票证可能不准确在这种情况下,我使用它来实现提醒。我更喜欢使用crontab并为这样的作业生成端点,所以运行一个脚本来命中端点,为您的任务生成白名单ipvm@Pocket正如我在问题的回答和评论中提到的,在这个场景中,在go二进制之外的基础设施级别触发器是理想的,但是如果它们是wan的话t无论出于何种原因,这都是一个可能的解决方案。或者,我没有尽快回复,这是我的解决方案,请让我知道你的想法,谢谢!如果你停止并重新运行服务器,将重新启动罚单?因此,在这种情况下使用ticker可能不准确,我使用它是为了实现提醒。我更喜欢使用crontab和像这样为作业创建端点,所以为此运行一个脚本以命中端点,为您的作业创建白名单ipvm@Pocket正如我在问题的回答和评论中提到的,在这个场景中,在go二进制文件之外的基础设施级别触发器是理想的,但是如果他们出于任何原因想纯粹使用go来实现这一点,这是可能的解决方案抱歉我没有尽快回复,这是我的解决方案,请让我知道你的想法,谢谢!抱歉我没有尽快回复,这是我的解决方案,请让我知道你的想法,谢谢!抱歉我没有尽快回复,这是我的解决方案,请让我知道你的想法,谢谢!