ddnsclient/main.go

58 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-03-14 16:35:06 +01:00
package ddnsclient
import (
2021-03-15 19:13:11 +01:00
"os"
"os/signal"
"syscall"
2021-03-15 08:31:47 +01:00
"time"
2021-03-14 16:35:06 +01:00
"github.com/datahearth/ddnsclient/pkg/providers/ovh"
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-15 08:31:47 +01:00
"github.com/spf13/viper"
2021-03-14 16:35:06 +01:00
)
// Start create a new instance of ddns-client
func Start(logger logrus.FieldLogger) error {
2021-03-15 19:13:11 +01:00
log := logger.WithFields(logrus.Fields{
"pkg": "ddnsclient",
"component": "root",
})
log.Debugln("create OVH provider")
2021-03-15 08:31:47 +01:00
ovh, err := ovh.NewOVH(logger)
2021-03-14 16:35:06 +01:00
if err != nil {
return err
}
2021-03-15 08:31:47 +01:00
2021-03-15 19:13:11 +01:00
log.Debugln("creating watcher with OVH provider")
2021-03-15 08:31:47 +01:00
w, err := watcher.NewWatcher(logger, ovh, viper.GetString("web-ip"))
2021-03-14 16:35:06 +01:00
if err != nil {
return err
}
2021-03-15 19:13:11 +01:00
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
chClose := make(chan struct{})
chErr := make(chan error)
defer close(chClose)
defer close(chErr)
defer close(sigc)
log.Infoln("Start watching periodically for changes!")
2021-03-17 08:14:06 +01:00
t := time.NewTicker(viper.GetDuration("update-time")*time.Second)
go w.Run(t, chClose, chErr)
2021-03-15 19:13:11 +01:00
2021-03-14 16:35:06 +01:00
for {
2021-03-15 19:13:11 +01:00
select {
case err := <-chErr:
log.WithError(err).Errorln("An error occured while running the watcher. Retrying in the next tick")
continue
case <-sigc:
log.Infoln("Interrupt signal received. Stopping watcher...")
chClose <- struct{}{}
return nil
}
2021-03-14 16:35:06 +01:00
}
}