diff --git a/cmd/yanic/main.go b/cmd/yanic/main.go index ed23888d..eff34baa 100644 --- a/cmd/yanic/main.go +++ b/cmd/yanic/main.go @@ -6,6 +6,7 @@ import ( "os" "os/signal" "syscall" + "time" "github.com/FreifunkBremen/yanic/database" "github.com/FreifunkBremen/yanic/database/all" @@ -58,18 +59,26 @@ func main() { nodes.Start() meshviewer.Start(config, nodes) - if config.Respondd.Enable { - collector = respond.NewCollector(connections, nodes, config.Respondd.Interface, config.Respondd.Port) - collector.Start(config.Respondd.CollectInterval.Duration) - defer collector.Close() - } - if config.Webserver.Enable { log.Println("starting webserver on", config.Webserver.Bind) srv := webserver.New(config.Webserver.Bind, config.Webserver.Webroot) go srv.Close() } + if config.Respondd.Enable { + // Delaying startup to start at a multiple of `duration` since the zero time. + if duration := config.Respondd.Syncronize.Duration; duration > 0 { + now := time.Now() + delay := now.Sub(now.Truncate(duration)) + log.Printf("delaying %0.1f seconds", delay.Seconds()) + time.Sleep(delay) + } + + collector = respond.NewCollector(connections, nodes, config.Respondd.Interface, config.Respondd.Port) + collector.Start(config.Respondd.CollectInterval.Duration) + defer collector.Close() + } + // Wait for INT/TERM sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) diff --git a/config_example.toml b/config_example.toml index c6e15ba5..c9dff887 100644 --- a/config_example.toml +++ b/config_example.toml @@ -1,6 +1,8 @@ # Send respondd request to update information [respondd] enable = true +# Delay startup until a multiple of the period since zero time +syncronize = "1m" # how oftern request per multicast collect_interval = "1m" # on which interface diff --git a/runtime/config.go b/runtime/config.go index c5fb4a7e..c84c6007 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -10,6 +10,7 @@ import ( type Config struct { Respondd struct { Enable bool `toml:"enable"` + Syncronize Duration `toml:"syncronize"` Interface string `toml:"interface"` Port int `toml:"port"` CollectInterval Duration `toml:"collect_interval"`