go routines now run forever like they are supposed to

This commit is contained in:
Matthew R Kasun 2022-01-03 22:21:52 +00:00
parent 7bc99d3076
commit f5aa383541

View file

@ -30,6 +30,7 @@ func Daemon() error {
signal.Notify(quit, syscall.SIGTERM, os.Interrupt) signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
<-quit <-quit
cancel() cancel()
ncutils.Log("all done")
return nil return nil
} }
@ -48,11 +49,7 @@ 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(ctx context.Context, network string) { func Netclient(ctx context.Context, network string) {
select { ncutils.Log("netclient go routine started for " + network)
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()
@ -70,7 +67,10 @@ func Netclient(ctx context.Context, 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)
} <-ctx.Done()
ncutils.Log("shutting down daemon")
return
ncutils.Log("netclient go routine ended for " + network)
} }
//All -- mqtt message hander for all ('#') topics //All -- mqtt message hander for all ('#') topics
@ -97,6 +97,7 @@ var UpdateKeys mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message)
//Checkin -- go routine that checks for public or local ip changes, publishes changes //Checkin -- go routine that checks for public or local ip changes, publishes changes
// if there are no updates, simply "pings" the server as a checkin // if there are no updates, simply "pings" the server as a checkin
func Checkin(ctx context.Context, cfg config.ClientConfig, network string) { func Checkin(ctx context.Context, cfg config.ClientConfig, network string) {
for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
ncutils.Log("Checkin cancelled") ncutils.Log("Checkin cancelled")
@ -135,6 +136,7 @@ func Checkin(ctx context.Context, cfg config.ClientConfig, network string) {
ncutils.Log("Checkin complete") ncutils.Log("Checkin complete")
} }
} }
}
//UpdateEndpoint -- publishes an endpoint update to broker //UpdateEndpoint -- publishes an endpoint update to broker
func UpdateEndpoint(cfg config.ClientConfig, network, ip string) { func UpdateEndpoint(cfg config.ClientConfig, network, ip string) {
@ -159,7 +161,7 @@ func UpdateLocalAddress(cfg config.ClientConfig, network, ip string) {
//Hello -- ping the broker to let server know node is alive and doing fine //Hello -- ping the broker to let server know node is alive and doing fine
func Hello(cfg config.ClientConfig, network string) { func Hello(cfg config.ClientConfig, network string) {
client := SetupMQTT(cfg) client := SetupMQTT(cfg)
if token := client.Publish("ping/"+network+"/"+cfg.Node.ID, 0, false, "hello world!"); token.Wait() && token.Error() != nil { if token := client.Publish("ping/"+cfg.Node.ID, 0, false, "hello world!"); token.Wait() && token.Error() != nil {
ncutils.Log("error publishing ping " + token.Error().Error()) ncutils.Log("error publishing ping " + token.Error().Error())
} }
client.Disconnect(250) client.Disconnect(250)
@ -167,6 +169,7 @@ func Hello(cfg config.ClientConfig, network string) {
//Metics -- go routine that collects wireguard metrics and publishes to broker //Metics -- go routine that collects wireguard metrics and publishes to broker
func Metrics(ctx context.Context, cfg config.ClientConfig, network string) { func Metrics(ctx context.Context, cfg config.ClientConfig, network string) {
for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
ncutils.Log("Metrics collection cancelled") ncutils.Log("Metrics collection cancelled")
@ -199,3 +202,4 @@ func Metrics(ctx context.Context, cfg config.ClientConfig, network string) {
ncutils.Log("metrics collection complete") ncutils.Log("metrics collection complete")
} }
} }
}