Gocron在日期更改时无法正常工作(GOLANG)

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

Gocron doesn't work when the day changes (GOLANG)

问题

我制作了一个调度程序,希望在日期更改后运行该调度程序,但实际上在日期更改后它并不起作用。我使用Heroku作为服务器,Heroku不支持这个吗?

这是我的调度程序:

  1. package scheduler
  2. import (
  3. "github.com/go-co-op/gocron"
  4. "talkconnectv2/modules/smtp"
  5. "time"
  6. )
  7. type Scheduling interface {
  8. CronJobExporting() *gocron.Scheduler
  9. }
  10. type scheduler struct {
  11. Scheduler *gocron.Scheduler
  12. }
  13. func NewScheduler(sch *gocron.Scheduler) Scheduling {
  14. return &scheduler{
  15. Scheduler: sch,
  16. }
  17. }
  18. //需要改进
  19. func (sch *scheduler) CronJobExporting() *gocron.Scheduler {
  20. sch.Scheduler.Every(1).Days().Do(func() {
  21. testFormat()
  22. })
  23. sch.Scheduler.StartAsync()
  24. return sch.Scheduler
  25. }
  26. func testFormat() {
  27. smtp.NewEmailSmtp(
  28. smtp.EmailRequest{
  29. From: "lalal@gmail.com",
  30. Password: "***********",
  31. To: []string{"choirfilza@gmail.com"},
  32. Body: smtp.EmailMapping{
  33. EmailRegister: "testscheduler",
  34. Brand: "testest",
  35. Tanggal: time.Now().Format("2006-01-02 15:04:05"),
  36. },
  37. },
  38. ).SendEmail()
  39. }

这是主函数:

  1. func main() {
  2. s := gocron.NewScheduler(time.UTC)
  3. ConfigRuntime()
  4. cache.Init()
  5. mongo.ConnectAll()
  6. scheduler.NewScheduler(s).CronJobExporting()
  7. StartGin()
  8. s.StartBlocking()
  9. }
英文:

I made a scheduler with the hope that when the day changes this scheduler will run, but in fact after the day it changes it doesn't work. I use heroku as a server, does heroku not support this?

this is my scheduler

  1. package scheduler
  2. import (
  3. "github.com/go-co-op/gocron"
  4. "talkconnectv2/modules/smtp"
  5. "time"
  6. )
  7. type Scheduling interface {
  8. CronJobExporting() *gocron.Scheduler
  9. }
  10. type scheduler struct {
  11. Scheduler *gocron.Scheduler
  12. }
  13. func NewScheduler(sch *gocron.Scheduler) Scheduling {
  14. return &scheduler{
  15. Scheduler: sch,
  16. }
  17. }
  18. //need enhancement
  19. func (sch *scheduler) CronJobExporting() *gocron.Scheduler {
  20. sch.Scheduler.Every(1).Days().Do(func() {
  21. testFormat()
  22. })
  23. sch.Scheduler.StartAsync()
  24. return sch.Scheduler
  25. }
  26. func testFormat() {
  27. smtp.NewEmailSmtp(
  28. smtp.EmailRequest{
  29. From: "lalal@gmail.com",
  30. Password: "***********",
  31. To: []string{"choirfilza@gmail.com"},
  32. Body: smtp.EmailMapping{
  33. EmailRegister: "testscheduler",
  34. Brand: "testest",
  35. Tanggal: time.Now().Format("2006-01-02 15:04:05"),
  36. },
  37. },
  38. ).SendEmail()
  39. }

this the main

  1. func main() {
  2. s := gocron.NewScheduler(time.UTC)
  3. ConfigRuntime()
  4. cache.Init()
  5. mongo.ConnectAll()
  6. scheduler.NewScheduler(s).CronJobExporting()
  7. StartGin()
  8. s.StartBlocking()
  9. }

答案1

得分: 1

Heroku每24小时重新启动您的dyno

当调用CronJobExporting时,它将等待24小时并运行您的testFormat函数。

很有可能在gocron等待足够长的时间来运行您的任务之前,您的dyno会重新启动。

要在Heroku上运行每日任务,您可以使用Heroku Scheduler

英文:

Heroku restarts your dyno every 24h.

When CronJobExporting is called, it will wait 24 hour and run your testFormat function.

There's a high probability that your dyno is restarted before gocron has waited enough to run your task.

To run daily task on Heroku, you can use Heroku Scheduler.

huangapple
  • 本文由 发表于 2021年6月15日 09:31:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/67978907.html
匿名

发表评论

匿名网友

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

确定