ddnsclient/main.go

69 lines
1.5 KiB
Go
Raw Normal View History

2021-03-14 16:35:06 +01:00
package ddnsclient
import (
"errors"
2021-03-15 19:13:11 +01:00
"os"
"os/signal"
"syscall"
2021-03-15 08:31:47 +01:00
"time"
2021-05-17 13:15:39 +02:00
"github.com/datahearth/ddnsclient/pkg/utils"
2021-03-15 08:31:47 +01:00
"github.com/datahearth/ddnsclient/pkg/watcher"
2021-03-14 16:35:06 +01:00
"github.com/sirupsen/logrus"
)
2021-03-14 16:35:06 +01:00
// Start create a new instance of ddns-client
2021-05-17 13:15:39 +02:00
func Start(logger logrus.FieldLogger, config utils.ClientConfig) error {
2021-03-15 19:13:11 +01:00
log := logger.WithFields(logrus.Fields{
"pkg": "ddnsclient",
"component": "root",
})
2021-05-17 13:15:39 +02:00
ws := make([]watcher.Watcher, 0, len(config.Watchers))
for _, cw := range config.Watchers {
w, err := watcher.NewWatcher(logger, &cw, config.WebIP)
if err != nil {
logger.Warnf("Provider error: %v. Skipping...\n", err.Error())
continue
}
ws = append(ws, w)
}
if len(ws) == 0 {
2021-05-27 12:33:02 +02:00
return errors.New("no valid watchers were created. Checkout [watchers] configuration and its [providers] configuration")
2021-03-14 16:35:06 +01:00
}
2021-03-15 19:13:11 +01:00
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer close(sigc)
2021-03-15 19:13:11 +01:00
chClose := make(chan struct{})
chErr := make(chan error)
defer close(chClose)
defer close(chErr)
logger.Infoln("Start watching periodically for changes!")
for _, w := range ws {
tickTime := config.UpdateTime
if tickTime == 0 {
tickTime = 180
}
2021-03-15 19:13:11 +01:00
t := time.NewTicker(time.Duration(tickTime) * time.Second)
go w.Run(t, chClose, chErr)
}
2021-03-14 16:35:06 +01:00
for {
2021-03-15 19:13:11 +01:00
select {
case err := <-chErr:
log.Errorln(err.Error())
2021-03-15 19:13:11 +01:00
continue
case <-sigc:
log.Infoln("Interrupt signal received. Stopping watcher...")
chClose <- struct{}{}
return nil
}
2021-03-14 16:35:06 +01:00
}
}