I have a go service that collects some data i’m interested in and exposes it via grpc. The data collection portion I start as a “service” inside my main. But as I add functionality to the program, I can tell theres likely a better way to do things…for instance:
snippet from main:
func (app *Application) startServcies() {
// pass the config to the rtsc service
rtscWatcher := services.ErcotRtscWatcher{
Details: &app.Config.Details.RTSC,
InfoLog: app.InfoLog,
ErrorLog: app.ErrorLog,
Stream: app.RtscStream,
}
sppWatcher := services.ErcotSppWatcher{
Details: &app.Config.Details.SPP,
InfoLog: app.InfoLog,
ErrorLog: app.ErrorLog,
Stream: app.SppStream,
}
// start the watcher
go rtscWatcher.Run()
go sppWatcher.Run()
where I am implementing the Run method on each service type and then instantiating them one by one inside main.
an example Run method:
func (s *WeatherMonitor) Run() {
// ensure user config doesn't do anything cheeky like set runtime to 0
if err := ServiceValidator(s.Details); err != nil {
s.ErrorLog.Println(err)
return
}
// unpack the time and zone from the config
t, z := s.Details.StartAt[0], s.Details.StartAt[1]
// set the zone
tz, _ := time.LoadLocation(z)
if !s.Details.Scheduled {
// this branch starts immediately
s.InfoLog.Printf("%v is starting. running for %vs every %vs", s.Details.Name, s.Details.Runtime, s.Details.Refresh)
for i := 0; i < (s.Details.Runtime / s.Details.Refresh); i++ {
go GetWeather(s.Stream, s.ErrorLog)
msg := <-s.Stream
s.Store = append(s.Store, &msg)
time.Sleep(time.Duration(s.Details.Refresh) * time.Second)
}
if !s.Details.ReRun {
s.InfoLog.Println("terminating", s.Details.Name)
return
}
s.InfoLog.Println(s.Details.Name, "rotating service")
}
}
suggestions?