From 94df957b020a29c3746bee6ecf0266e4b002e1f5 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sat, 1 Jul 2023 17:31:09 +0200 Subject: [PATCH] Add signal listeners to the cmd command --- README.md | 3 ++- cmd/birthdaybot/main.go | 11 +++++++---- server/server.go | 12 ++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 42b4ed0..cdc94a2 100644 --- a/README.md +++ b/README.md @@ -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! diff --git a/cmd/birthdaybot/main.go b/cmd/birthdaybot/main.go index 9bbebe6..9b6e35e 100644 --- a/cmd/birthdaybot/main.go +++ b/cmd/birthdaybot/main.go @@ -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() } diff --git a/server/server.go b/server/server.go index 9da5414..7d26a37 100644 --- a/server/server.go +++ b/server/server.go @@ -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)) }