move context up a level

This commit is contained in:
Matthew R Kasun
2022-01-03 16:26:40 -05:00
parent f968ecf4f5
commit efd8764de3

View File

@@ -18,15 +18,18 @@ import (
//Daemon runs netclient daemon from command line //Daemon runs netclient daemon from command line
func Daemon() error { func Daemon() error {
ctx, cancel := context.WithCancel(context.Background())
networks, err := ncutils.GetSystemNetworks() networks, err := ncutils.GetSystemNetworks()
if err != nil { if err != nil {
return err return err
} }
for _, network := range networks { for _, network := range networks {
go Netclient(network) go Netclient(ctx, network)
}
for {
} }
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
<-quit
cancel()
return nil return nil
} }
@@ -44,29 +47,30 @@ func SetupMQTT(cfg config.ClientConfig) mqtt.Client {
} }
//Netclient sets up Message Queue and subsribes/publishes updates to/from server //Netclient sets up Message Queue and subsribes/publishes updates to/from server
func Netclient(network string) { func Netclient(ctx context.Context, network string) {
ctx, cancel := context.WithCancel(context.Background()) select {
var cfg config.ClientConfig case <-ctx.Done():
cfg.Network = network ncutils.Log("shutting down daemon")
cfg.ReadConfig() return
//fix NodeID to remove ### so NodeID can be used as message topic default:
//remove with GRA-73 var cfg config.ClientConfig
cfg.Node.ID = strings.ReplaceAll(cfg.Node.ID, "###", "-") cfg.Network = network
ncutils.Log("daemon started for network:" + network) cfg.ReadConfig()
client := SetupMQTT(cfg) //fix NodeID to remove ### so NodeID can be used as message topic
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil { //remove with GRA-73
log.Fatal(token.Error()) cfg.Node.ID = strings.ReplaceAll(cfg.Node.ID, "###", "-")
ncutils.Log("daemon started for network:" + network)
client := SetupMQTT(cfg)
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
log.Fatal(token.Error())
}
client.AddRoute("update/"+cfg.Node.ID, NodeUpdate)
client.AddRoute("update/peers/"+cfg.Node.ID, UpdatePeers)
client.AddRoute("update/keys/"+cfg.Node.ID, UpdateKeys)
defer client.Disconnect(250)
go Checkin(ctx, cfg, network)
go Metrics(ctx, cfg, network)
} }
client.AddRoute("update/"+cfg.Node.ID, NodeUpdate)
client.AddRoute("update/peers/"+cfg.Node.ID, UpdatePeers)
client.AddRoute("update/keys/"+cfg.Node.ID, UpdateKeys)
defer client.Disconnect(250)
go Checkin(ctx, cfg, network)
go Metrics(ctx, cfg, network)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
<-quit
cancel()
} }
//All -- mqtt message hander for all ('#') topics //All -- mqtt message hander for all ('#') topics
@@ -80,12 +84,12 @@ var NodeUpdate mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message)
ncutils.Log("received message to update node " + string(msg.Payload())) ncutils.Log("received message to update node " + string(msg.Payload()))
} }
//NodeUpdate -- mqtt message handler for /update/peers/<NodeID> topic //UpdatePeers -- mqtt message handler for /update/peers/<NodeID> topic
var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
ncutils.Log("received message to update peers " + string(msg.Payload())) ncutils.Log("received message to update peers " + string(msg.Payload()))
} }
//NodeUpdate -- mqtt message handler for /update/keys/<NodeID> topic //UpdateKeys -- mqtt message handler for /update/keys/<NodeID> topic
var UpdateKeys mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { var UpdateKeys mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
ncutils.Log("received message to update keys " + string(msg.Payload())) ncutils.Log("received message to update keys " + string(msg.Payload()))
} }