Add getting the next reminder in a cron
This commit is contained in:
parent
a9e98714ea
commit
b30c6eead8
|
@ -16,13 +16,16 @@
|
|||
|
||||
package cron
|
||||
|
||||
import "github.com/robfig/cron/v3"
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
var c *cron.Cron
|
||||
|
||||
// Init starts the cron
|
||||
func Init() {
|
||||
c = cron.New()
|
||||
c.Start()
|
||||
}
|
||||
|
||||
// Schedule schedules a job as a cron job
|
||||
|
|
|
@ -84,4 +84,5 @@ func FullInit() {
|
|||
|
||||
// Start the cron
|
||||
cron.Init()
|
||||
models.RegisterReminderCron()
|
||||
}
|
||||
|
|
|
@ -16,7 +16,12 @@
|
|||
|
||||
package models
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/cron"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TaskReminder holds a reminder on a task
|
||||
type TaskReminder struct {
|
||||
|
@ -30,3 +35,41 @@ type TaskReminder struct {
|
|||
func (TaskReminder) TableName() string {
|
||||
return "task_reminders"
|
||||
}
|
||||
|
||||
// RegisterReminderCron registers a cron function which runs every minute to check if any reminders are due the
|
||||
// next minute to send emails.
|
||||
func RegisterReminderCron() {
|
||||
if !config.MailerEnabled.GetBool() {
|
||||
log.Info("Mailer is disabled, not sending reminders per mail")
|
||||
return
|
||||
}
|
||||
|
||||
tz := config.GetTimeZone()
|
||||
const dbFormat = `2006-01-02 15:04:05`
|
||||
|
||||
log.Debugf("[Task Reminder Cron] Timezone is %s", tz)
|
||||
|
||||
err := cron.Schedule("* * * * *", func() {
|
||||
// By default, time.Now() includes nanoseconds which we don't save. That results in getting the wrong dates,
|
||||
// so we make sure the time we use to get the reminders don't contain nanoseconds.
|
||||
now := time.Now()
|
||||
now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, now.Location()).In(tz)
|
||||
nextMinute := now.Add(1 * time.Minute)
|
||||
|
||||
log.Debugf("[Task Reminder Cron] Looking for reminders between %s and %s to send...", now, nextMinute)
|
||||
|
||||
reminders := []*TaskReminder{}
|
||||
err := x.
|
||||
Where("reminder >= ? and reminder < ?", now.Format(dbFormat), nextMinute.Format(dbFormat)).
|
||||
Find(&reminders)
|
||||
if err != nil {
|
||||
log.Errorf("[Task Reminder Cron] Could not get reminders for the next minute: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("[Task Reminder Cron] Found %d reminders", len(reminders))
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Could not register reminder cron: %s", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue