birthdaybot/server/worker.go

49 lines
799 B
Go
Raw Normal View History

2023-07-01 16:23:19 +01:00
package server
import (
"time"
"github.com/charmbracelet/log"
)
type Worker struct {
logger *log.Logger
stop chan bool
stopped chan bool
}
func NewWorker(logger *log.Logger) *Worker {
return &Worker{
logger: 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)
case <-w.stop:
w.logger.Info("received stop signal")
w.stopped <- true
return
}
}
}