birthdaybot/server/worker.go

85 lines
1.8 KiB
Go
Raw Normal View History

2023-07-01 16:23:19 +01:00
package server
import (
"time"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
2023-07-01 16:23:19 +01:00
"github.com/charmbracelet/log"
)
type Worker struct {
server *Server
2023-07-01 16:23:19 +01:00
logger *log.Logger
stop chan bool
stopped chan bool
}
func NewWorker(server *Server) *Worker {
2023-07-01 16:23:19 +01:00
return &Worker{
server: server,
logger: server.Logger,
2023-07-01 16:23:19 +01:00
stop: make(chan bool, 1),
stopped: make(chan bool, 1),
}
}
func (w *Worker) Start() {
w.logger.Info("starting worker")
go w.run()
w.logger.Info("worker started")
}
func (w *Worker) Stop() {
w.logger.Info("stopping worker")
w.stop <- true
<-w.stopped
w.logger.Info("worker stopped")
}
func (w *Worker) notifyDay(year, month, day int) {
birthdays := model.FilterByDate(w.server.Birthdays(), year, month, day)
w.logger.Info("notifying for date", "birthdays", len(birthdays), "year", year, "month", month, "day", day)
for _, b := range birthdays {
w.logger.Info("notifying for birthday", "name", b.Name)
w.server.Notify(b)
}
}
2023-07-01 16:23:19 +01:00
func (w *Worker) run() {
// first we calculate the delta with 23:00
now := time.Now()
eleven := time.Date(now.Year(), now.Month(), now.Day(), 23, 0, 0, 0, now.Location())
if eleven.Before(now) {
// tomorrow at 23:00
eleven = time.Date(now.Year(), now.Month(), now.Day()+1, 23, 0, 0, 0, now.Location())
}
firstTick := eleven.Sub(now)
var ticker *time.Ticker
w.logger.Info("first timer tick set", "tick", firstTick)
select {
case <-time.After(firstTick):
ticker = time.NewTicker(24 * time.Hour)
w.notifyDay(eleven.Year(), int(eleven.Month()), eleven.Day())
case <-w.stop:
w.logger.Info("received stop signal")
w.stopped <- true
return
}
2023-07-01 16:23:19 +01:00
for {
select {
case t := <-ticker.C:
w.logger.Info("ticker ticked for worker", "tick", t)
w.notifyDay(t.Year(), int(t.Month()), t.Day())
2023-07-01 16:23:19 +01:00
case <-w.stop:
w.logger.Info("received stop signal")
w.stopped <- true
return
}
}
}