mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 08:47:35 +08:00
move context up a level
This commit is contained in:
@@ -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,8 +47,12 @@ 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 {
|
||||||
|
case <-ctx.Done():
|
||||||
|
ncutils.Log("shutting down daemon")
|
||||||
|
return
|
||||||
|
default:
|
||||||
var cfg config.ClientConfig
|
var cfg config.ClientConfig
|
||||||
cfg.Network = network
|
cfg.Network = network
|
||||||
cfg.ReadConfig()
|
cfg.ReadConfig()
|
||||||
@@ -63,10 +70,7 @@ func Netclient(network string) {
|
|||||||
defer client.Disconnect(250)
|
defer client.Disconnect(250)
|
||||||
go Checkin(ctx, cfg, network)
|
go Checkin(ctx, cfg, network)
|
||||||
go Metrics(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()))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user