Add signal listeners to the cmd command

This commit is contained in:
Miguel de la Cruz 2023-07-01 17:31:09 +02:00
parent e3fcf6adf1
commit 94df957b02
3 changed files with 15 additions and 11 deletions

View file

@ -13,10 +13,11 @@ can be used as a base to create your own configuration, and there is a
## Roadmap
- [ ] Create the bot scaffold
- [X] Create the bot scaffold
- [X] Define how to read the birthdays info
- [X] Add a logger instance to the config
- [ ] Add config validation on server creation
- [ ] Create a configurable template to fill with each notification
- [ ] Create different message systems to use with the bot
- [ ] Reduce logger verbosity
- [ ] Enjoy!

View file

@ -3,7 +3,8 @@ package main
import (
"flag"
"os"
"time"
"os/signal"
"syscall"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"git.ctrlz.es/mgdelacroix/birthdaybot/server"
@ -26,9 +27,11 @@ func main() {
os.Exit(1)
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
srv.Start()
time.Sleep(5 * time.Second)
s := <-c
srv.Logger.Info("received signal, stopping", "signal", s)
srv.Stop()
}

View file

@ -10,7 +10,7 @@ import (
)
type Server struct {
logger *log.Logger
Logger *log.Logger
config *model.Config
workers []*Worker
birthdays []*model.Birthday
@ -30,7 +30,7 @@ func New(config *model.Config) (*Server, error) {
logger.Info("creating server")
server := &Server{
logger: logger,
Logger: logger,
config: config,
birthdays: birthdays,
workers: []*Worker{NewWorker(logger)},
@ -40,17 +40,17 @@ func New(config *model.Config) (*Server, error) {
}
func (s *Server) Start() {
s.logger.Info("starting server")
s.Logger.Info("starting server")
for _, worker := range s.workers {
worker.Start()
}
s.logger.Info("server started", "workers", len(s.workers))
s.Logger.Info("server started", "workers", len(s.workers))
}
func (s *Server) Stop() {
s.logger.Info("stopping server")
s.Logger.Info("stopping server")
for _, worker := range s.workers {
worker.Stop()
}
s.logger.Info("server stopped", "workers", len(s.workers))
s.Logger.Info("server stopped", "workers", len(s.workers))
}