Skip to content

Commit

Permalink
Delay startup until a multiple of the period since zero time
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Jun 1, 2017
1 parent 88975d2 commit e83b4f4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
21 changes: 15 additions & 6 deletions cmd/yanic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/database/all"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions config_example.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit e83b4f4

Please sign in to comment.