[TASK] Delay startup until a multiple of the period since zero time (#68)

This commit is contained in:
Julian K 2017-06-14 09:44:15 +02:00 committed by Geno
parent 88975d2566
commit 7f554bd6d6
3 changed files with 18 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time"
"github.com/FreifunkBremen/yanic/database" "github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/database/all" "github.com/FreifunkBremen/yanic/database/all"
@ -58,18 +59,26 @@ func main() {
nodes.Start() nodes.Start()
meshviewer.Start(config, nodes) 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 { if config.Webserver.Enable {
log.Println("starting webserver on", config.Webserver.Bind) log.Println("starting webserver on", config.Webserver.Bind)
srv := webserver.New(config.Webserver.Bind, config.Webserver.Webroot) srv := webserver.New(config.Webserver.Bind, config.Webserver.Webroot)
go srv.Close() 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 // Wait for INT/TERM
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

View File

@ -1,6 +1,8 @@
# Send respondd request to update information # Send respondd request to update information
[respondd] [respondd]
enable = true enable = true
# Delay startup until a multiple of the period since zero time
synchronize = "1m"
# how oftern request per multicast # how oftern request per multicast
collect_interval = "1m" collect_interval = "1m"
# on which interface # on which interface

View File

@ -10,6 +10,7 @@ import (
type Config struct { type Config struct {
Respondd struct { Respondd struct {
Enable bool `toml:"enable"` Enable bool `toml:"enable"`
Synchronize Duration `toml:"synchronize"`
Interface string `toml:"interface"` Interface string `toml:"interface"`
Port int `toml:"port"` Port int `toml:"port"`
CollectInterval Duration `toml:"collect_interval"` CollectInterval Duration `toml:"collect_interval"`