调度程序每天午夜获取数据。

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

scheduler fetch data every midnight

问题

如何添加一个具有特定时间的调度程序,以便应用程序可以在指定的时间同步数据

addCron, _ := time.LoadLocation("Asia/Jakarta")
scheduler := cron.New(cron.WithLocation(addCron))

defer scheduler.Stop()

scheduler.AddFunc("10 00 * * 1-6", ExecuteRoutine)

// start scheduler
go scheduler.Start()
done := make(chan bool)

// trigger shutdown
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
done <- true

如何将该端点添加到函数ExecuteRoutine中,以便它可以在每天午夜执行cron作业

httpRouter.Post("/master/upsertIntoServices", services.UpsertIntoServices)
英文:

how to add a scheduler with a specific time so that the application can synchronize data at a specified time

addCron, _ := time.LoadLocation(&quot;Asia/Jakarta&quot;)
scheduler := cron.New(cron.WithLocation(addCron))

defer scheduler.Stop()

scheduler.AddFunc(&quot;10 00 * * 1-6&quot;, ExecuteRoutine)

// start scheduler
go scheduler.Start()
done := make(chan bool)

// trigger shutdown
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
&lt;-sig
done &lt;- true

function and endpoint that will collect automatically at the midnight

httpRouter.Post(&quot;/master/upsertIntoServices&quot;, services.UpsertIntoServices)

how to add that endpoint into a function ExecuteRoutine, so it can do a cron job every midnight

答案1

得分: -2

这是一个简单的示例,用于从你的代码中在午夜调度作业。

addCron, _ := time.LoadLocation("Asia/Jakarta")
scheduler := cron.New(cron.WithLocation(addCron))

defer scheduler.Stop()

scheduler.AddFunc("@midnight", ExecuteRoutine)

// 启动调度器
scheduler.Start()

你可以在文档中找到更多信息。

只有几个提醒:

  • 当调用 start 命令时,你不需要运行 goroutine。

  • 在逻辑的某个点上,你需要停止调度器。

英文:

This is a simple example, to scheduler jobs at midnight from your code

addCron, _ := time.LoadLocation(&quot;Asia/Jakarta&quot;)
scheduler := cron.New(cron.WithLocation(addCron))

defer scheduler.Stop()

scheduler.AddFunc(&quot;@midnight&quot;, ExecuteRoutine)

// start scheduler
scheduler.Start()

You can discover, more information here in the doc

Only a few reminders:

  • You don't need to run a goroutine when will call the command start.

  • you do need to stop your scheduler at some point in your logic

huangapple
  • 本文由 发表于 2021年12月20日 21:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/70422958.html
匿名

发表评论

匿名网友

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

确定