2017-09-17 03:26:19 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2019-01-17 13:26:16 +01:00
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-05-20 14:46:29 +02:00
|
|
|
allDatabase "github.com/FreifunkBremen/yanic/database/all"
|
|
|
|
allOutput "github.com/FreifunkBremen/yanic/output/all"
|
2017-09-17 03:26:19 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/respond"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
"github.com/FreifunkBremen/yanic/webserver"
|
2022-07-09 20:20:48 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/webserver/prometheus"
|
2017-09-17 03:26:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// serveCmd represents the serve command
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "Runs the yanic server",
|
2017-10-02 00:44:21 +02:00
|
|
|
Example: "yanic serve --config /etc/yanic.toml",
|
2017-09-17 03:26:19 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
config := loadConfig()
|
|
|
|
|
2018-01-03 15:41:40 +01:00
|
|
|
err := allDatabase.Start(config.Database)
|
2017-09-17 03:26:19 +02:00
|
|
|
if err != nil {
|
2019-01-17 13:26:16 +01:00
|
|
|
log.Panicf("could not connect to database: %s", err)
|
2017-09-17 03:26:19 +02:00
|
|
|
}
|
2018-01-03 15:41:40 +01:00
|
|
|
defer allDatabase.Close()
|
2017-09-17 03:26:19 +02:00
|
|
|
|
2018-01-07 21:00:56 +01:00
|
|
|
nodes = runtime.NewNodes(&config.Nodes)
|
2017-09-17 03:26:19 +02:00
|
|
|
nodes.Start()
|
2017-05-20 14:46:29 +02:00
|
|
|
|
2018-01-03 15:41:40 +01:00
|
|
|
err = allOutput.Start(nodes, config.Nodes)
|
2017-05-20 14:46:29 +02:00
|
|
|
if err != nil {
|
2019-01-17 13:26:16 +01:00
|
|
|
log.Panicf("error on init outputs: %s", err)
|
2017-05-20 14:46:29 +02:00
|
|
|
}
|
2018-01-03 15:41:40 +01:00
|
|
|
defer allOutput.Close()
|
2017-09-17 03:26:19 +02:00
|
|
|
|
|
|
|
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))
|
2019-01-17 13:26:16 +01:00
|
|
|
log.Infof("delaying %0.1f seconds", delay.Seconds())
|
2017-09-17 03:26:19 +02:00
|
|
|
time.Sleep(delay)
|
|
|
|
}
|
|
|
|
|
2019-11-17 10:33:20 +01:00
|
|
|
collector = respond.NewCollector(allDatabase.Conn, nodes, &config.Respondd)
|
2017-09-17 03:26:19 +02:00
|
|
|
collector.Start(config.Respondd.CollectInterval.Duration)
|
|
|
|
defer collector.Close()
|
|
|
|
}
|
|
|
|
|
2022-07-09 20:20:48 +02:00
|
|
|
if config.Webserver.Enable {
|
|
|
|
log.Infof("starting webserver on %s", config.Webserver.Bind)
|
|
|
|
srv := webserver.New(config.Webserver)
|
|
|
|
go webserver.Start(srv)
|
|
|
|
if config.Webserver.Prometheus.Enable {
|
|
|
|
if config.Respondd.Enable {
|
|
|
|
prometheus.CreateExporter(config.Webserver.Prometheus, srv, collector, nodes)
|
|
|
|
} else {
|
|
|
|
log.Panic("to use prometheus exporter, please enable [respondd].")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer srv.Close()
|
|
|
|
} else if config.Webserver.Prometheus.Enable {
|
|
|
|
log.Panic("to use prometheus exporter, please enable [webserver].")
|
|
|
|
}
|
|
|
|
|
2017-09-17 03:26:19 +02:00
|
|
|
// Wait for INT/TERM
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
2019-01-17 13:26:16 +01:00
|
|
|
log.Infof("received %s", sig)
|
2017-09-17 03:26:19 +02:00
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(serveCmd)
|
|
|
|
serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.toml", "Path to configuration file")
|
|
|
|
}
|