英文:
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("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
function and endpoint that will collect automatically at the midnight
httpRouter.Post("/master/upsertIntoServices", 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("Asia/Jakarta")
scheduler := cron.New(cron.WithLocation(addCron))
defer scheduler.Stop()
scheduler.AddFunc("@midnight", 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论