英文:
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
当调用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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论