From 7f554bd6d6e2d44f8b99d0d4ce53a16f3359ab92 Mon Sep 17 00:00:00 2001 From: Julian K Date: Wed, 14 Jun 2017 09:44:15 +0200 Subject: [PATCH] [TASK] Delay startup until a multiple of the period since zero time (#68) --- cmd/yanic/main.go | 21 +++++++++++++++------ config_example.toml | 2 ++ runtime/config.go | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cmd/yanic/main.go b/cmd/yanic/main.go index ed23888..115cb48 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.Synchronize.Duration; duration > 0 { + now := time.Now() + delay := duration - 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 c6e15ba..34ebcf7 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 +synchronize = "1m" # how oftern request per multicast collect_interval = "1m" # on which interface diff --git a/runtime/config.go b/runtime/config.go index c5fb4a7..b6b3846 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -10,6 +10,7 @@ import ( type Config struct { Respondd struct { Enable bool `toml:"enable"` + Synchronize Duration `toml:"synchronize"` Interface string `toml:"interface"` Port int `toml:"port"` CollectInterval Duration `toml:"collect_interval"`