birthdaybot/server/worker.go
2023-07-04 12:29:25 +02:00

53 lines
923 B
Go

package server
import (
"time"
"github.com/charmbracelet/log"
)
type Worker struct {
server *Server
logger *log.Logger
stop chan bool
stopped chan bool
}
func NewWorker(server *Server) *Worker {
return &Worker{
server: server,
logger: server.Logger,
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) run() {
ticker := time.NewTicker(1 * time.Second)
for {
select {
case t := <-ticker.C:
w.logger.Info("ticker ticked for worker", "tick", t)
// ToDo: improve logic here
b := w.server.Birthdays()[0]
w.server.Notify(b)
case <-w.stop:
w.logger.Info("received stop signal")
w.stopped <- true
return
}
}
}