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

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

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

问题

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

这是我的调度程序:

package scheduler

import (
	"github.com/go-co-op/gocron"
	"talkconnectv2/modules/smtp"
	"time"
)

type Scheduling interface {
	CronJobExporting() *gocron.Scheduler
}

type scheduler struct {
	Scheduler *gocron.Scheduler
}

func NewScheduler(sch *gocron.Scheduler) Scheduling {
	return &scheduler{
		Scheduler: sch,
	}
}

//需要改进
func (sch *scheduler) CronJobExporting() *gocron.Scheduler {
	sch.Scheduler.Every(1).Days().Do(func() {
		testFormat()
	})
	sch.Scheduler.StartAsync()
	return sch.Scheduler
}

func testFormat() {
	smtp.NewEmailSmtp(
		smtp.EmailRequest{
			From:     "lalal@gmail.com",
			Password: "***********",
			To:       []string{"choirfilza@gmail.com"},
			Body: smtp.EmailMapping{
				EmailRegister: "testscheduler",
				Brand:         "testest",
				Tanggal:       time.Now().Format("2006-01-02 15:04:05"),
			},
		},
	).SendEmail()
}

这是主函数:

func main() {
	s := gocron.NewScheduler(time.UTC)
	ConfigRuntime()
	cache.Init()
	mongo.ConnectAll()
	scheduler.NewScheduler(s).CronJobExporting()
	StartGin()
	s.StartBlocking()
}
英文:

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

package scheduler

import (
	"github.com/go-co-op/gocron"
	"talkconnectv2/modules/smtp"
	"time"
)

type Scheduling interface {
	CronJobExporting() *gocron.Scheduler
}

type scheduler struct {
	Scheduler *gocron.Scheduler
}

func NewScheduler(sch *gocron.Scheduler) Scheduling {
	return &scheduler{
		Scheduler: sch,
	}
}

//need enhancement
func (sch *scheduler) CronJobExporting() *gocron.Scheduler {
	sch.Scheduler.Every(1).Days().Do(func() {
		testFormat()
	})
	sch.Scheduler.StartAsync()
	return sch.Scheduler
}

func testFormat() {
	smtp.NewEmailSmtp(
		smtp.EmailRequest{
			From:     "lalal@gmail.com",
			Password: "***********",
			To:       []string{"choirfilza@gmail.com"},
			Body: smtp.EmailMapping{
				EmailRegister: "testscheduler",
				Brand:         "testest",
				Tanggal:       time.Now().Format("2006-01-02 15:04:05"),
			},
		},
	).SendEmail()
}

this the main

func main() {
	s := gocron.NewScheduler(time.UTC)
	ConfigRuntime()
	cache.Init()
	mongo.ConnectAll()
	scheduler.NewScheduler(s).CronJobExporting()
	StartGin()
	s.StartBlocking()

}

答案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:

确定