77 "fmt"
88 "os"
99 "os/signal"
10+ "sync"
1011 "syscall"
1112
1213 "github.com/AarC10/GSW-V2/lib/db"
@@ -77,14 +78,16 @@ func telemetryConfigInitialize(config *viper.Viper) (func(), error) {
7778}
7879
7980// decomInitialize starts decommutation goroutines for each telemetry packet
80- func decomInitialize (ctx context.Context ) map [int ]chan []byte {
81+ func decomInitialize (ctx context.Context , wg * sync. WaitGroup ) map [int ]chan []byte {
8182 channelMap := make (map [int ]chan []byte )
8283
8384 for _ , packet := range proc .GswConfig .TelemetryPackets {
8485 finalOutputChannel := make (chan []byte )
8586 channelMap [packet .Port ] = finalOutputChannel
8687
88+ wg .Add (1 )
8789 go func (packet tlm.TelemetryPacket , ch chan []byte ) {
90+ defer wg .Done ()
8891 err := proc .TelemetryPacketWriter (ctx , packet , finalOutputChannel , * shmDir )
8992 if err != nil && ! errors .Is (err , context .Canceled ) {
9093 logger .Error ("error initializing packet writer" , zap .Error (err ))
@@ -96,19 +99,19 @@ func decomInitialize(ctx context.Context) map[int]chan []byte {
9699 return channelMap
97100}
98101
99- func dbInitialize (ctx context.Context , channelMap map [int ]chan []byte , host string , port int ) error {
102+ func dbInitialize (ctx context.Context , channelMap map [int ]chan []byte , host string , port int , wg * sync. WaitGroup ) error {
100103 dbHandler := db.InfluxDBV1Handler {}
101- err := dbHandler .Initialize (host , port )
102- if err != nil {
104+ if err := dbHandler .Initialize (host , port ); err != nil {
103105 return err
104106 }
105107
106108 for _ , packet := range proc .GswConfig .TelemetryPackets {
109+ wg .Add (1 )
107110 go func (packet tlm.TelemetryPacket , ch chan []byte ) {
108- proc .DatabaseWriter (& dbHandler , packet , ch )
111+ defer wg .Done ()
112+ proc .DatabaseWriter (ctx , & dbHandler , packet , ch )
109113 }(packet , channelMap [packet .Port ])
110114 }
111-
112115 return nil
113116}
114117
@@ -146,49 +149,48 @@ func main() {
146149 flag .Parse ()
147150 logger .InitLogger ()
148151
149- // Read gsw_service config
150152 config , profilingPort := readConfig ()
151153
152- ctx , cancel := context .WithCancel (context .Background ())
153- defer cancel ()
154-
155154 if profilingPort != 0 {
156155 initProfiling (profilingPort )
157156 }
158157
158+ telemetryConfigCleanup , err := telemetryConfigInitialize (config )
159+ if err != nil {
160+ logger .Fatal ("Exiting GSW..." )
161+ return
162+ }
163+ defer telemetryConfigCleanup ()
164+
165+ ctx , cancel := context .WithCancel (context .Background ())
166+ defer cancel ()
167+
159168 // Setup signal handling
160169 sigs := make (chan os.Signal , 1 )
161170 signal .Notify (sigs , syscall .SIGTERM , syscall .SIGINT )
162-
163171 go func () {
164172 sig := <- sigs
165173 logger .Debug ("Received signal" , zap .String ("signal" , sig .String ()))
166174 cancel ()
167175 }()
168176
169- telemetryConfigCleanup , err := telemetryConfigInitialize (config )
170- if err != nil {
171- logger .Panic ("Exiting GSW..." )
172- return
173- }
174- defer telemetryConfigCleanup ()
177+ var wg sync.WaitGroup
178+
179+ // Start decom writers
180+ channelMap := decomInitialize (ctx , & wg )
175181
176- channelMap := decomInitialize ( ctx )
182+ // Start DB writers
177183 if config .IsSet ("database_host_name" ) && config .IsSet ("database_port_number" ) {
178- err = dbInitialize (ctx , channelMap , config .GetString ("database_host_name" ), config .GetInt ("database_port_number" ))
179- if err != nil {
184+ if err = dbInitialize (ctx , channelMap , config .GetString ("database_host_name" ), config .GetInt ("database_port_number" ), & wg ); err != nil {
180185 logger .Warn ("DB Initialization failed, telemetry packets will not be published to the database" , zap .Error (err ))
181186 }
182187 } else {
183188 logger .Warn ("database_host_name or database_port_number is not set, telemetry packets will not be published to the database" )
184189 }
185190
186- // Wait for context cancellation or signal handling
191+ // Wait for shutdown signal
187192 <- ctx .Done ()
188193 logger .Info ("Shutting down GSW..." )
189-
190- for i , channel := range channelMap {
191- <- channel
192- logger .Info ("channel closed" , zap .Int ("port" , i ))
193- }
194+ wg .Wait ()
195+ logger .Info ("GSW stopped" )
194196}
0 commit comments