56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/parser"
|
|
"github.com/charmbracelet/log"
|
|
)
|
|
|
|
type Server struct {
|
|
logger *log.Logger
|
|
config *model.Config
|
|
workers []*Worker
|
|
birthdays []*model.Birthday
|
|
}
|
|
|
|
func New(config *model.Config) (*Server, error) {
|
|
logger := log.New(os.Stderr)
|
|
|
|
logger.Info("Parsing CSV file", "birthdayFile", config.BirthdayFile)
|
|
|
|
birthdays, err := parser.ParseCSV(config.BirthdayFile)
|
|
if err != nil {
|
|
logger.Error("cannot parse CSV file", "birthdayFile", config.BirthdayFile, "error", err)
|
|
return nil, fmt.Errorf("cannot parse CSV file %s: %w", config.BirthdayFile, err)
|
|
}
|
|
|
|
logger.Info("creating server")
|
|
|
|
server := &Server{
|
|
logger: logger,
|
|
config: config,
|
|
birthdays: birthdays,
|
|
workers: []*Worker{NewWorker(logger)},
|
|
}
|
|
|
|
return server, nil
|
|
}
|
|
|
|
func (s *Server) Start() {
|
|
s.logger.Info("starting server")
|
|
for _, worker := range s.workers {
|
|
worker.Start()
|
|
}
|
|
s.logger.Info("server started", "workers", len(s.workers))
|
|
}
|
|
|
|
func (s *Server) Stop() {
|
|
s.logger.Info("stopping server")
|
|
for _, worker := range s.workers {
|
|
worker.Stop()
|
|
}
|
|
s.logger.Info("server stopped", "workers", len(s.workers))
|
|
}
|