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 ## Roadmap
- [ ] Create the bot scaffold - [X] Create the bot scaffold
- [X] Define how to read the birthdays info - [X] Define how to read the birthdays info
- [X] Add a logger instance to the config - [X] Add a logger instance to the config
- [ ] Add config validation on server creation - [ ] Add config validation on server creation
- [ ] Create a configurable template to fill with each notification - [ ] Create a configurable template to fill with each notification
- [ ] Create different message systems to use with the bot - [ ] Create different message systems to use with the bot
- [ ] Reduce logger verbosity
- [ ] Enjoy! - [ ] Enjoy!

View file

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

View file

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