mirror of
https://github.com/gravitl/netmaker.git
synced 2025-02-27 01:23:00 +08:00
move context up a level
This commit is contained in:
parent
5458379b32
commit
f11fa093fb
1 changed files with 31 additions and 27 deletions
|
@ -18,15 +18,18 @@ import (
|
|||
|
||||
//Daemon runs netclient daemon from command line
|
||||
func Daemon() error {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
networks, err := ncutils.GetSystemNetworks()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, network := range networks {
|
||||
go Netclient(network)
|
||||
}
|
||||
for {
|
||||
go Netclient(ctx, network)
|
||||
}
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
|
||||
<-quit
|
||||
cancel()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -44,8 +47,12 @@ func SetupMQTT(cfg config.ClientConfig) mqtt.Client {
|
|||
}
|
||||
|
||||
//Netclient sets up Message Queue and subsribes/publishes updates to/from server
|
||||
func Netclient(network string) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
func Netclient(ctx context.Context, network string) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
ncutils.Log("shutting down daemon")
|
||||
return
|
||||
default:
|
||||
var cfg config.ClientConfig
|
||||
cfg.Network = network
|
||||
cfg.ReadConfig()
|
||||
|
@ -63,10 +70,7 @@ func Netclient(network string) {
|
|||
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
|
||||
|
@ -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()))
|
||||
}
|
||||
|
||||
//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) {
|
||||
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) {
|
||||
ncutils.Log("received message to update keys " + string(msg.Payload()))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue