cmd/revid-cli: made general changes to make revi-cli more robust i.e. not so trigger happy with fatals

This commit is contained in:
Saxon 2019-03-02 14:54:49 +10:30
parent 9d1d2914a4
commit 01474ee870
1 changed files with 41 additions and 51 deletions

View File

@ -45,14 +45,6 @@ import (
"bitbucket.org/ausocean/utils/logger"
)
const (
// progName is the program name for logging purposes.
progName = "revid-cli"
// Logging is set to INFO level.
defaultLogVerbosity = logger.Info
)
// Revid modes
const (
normal = "Normal"
@ -62,17 +54,19 @@ const (
// Other misc consts
const (
netSendRetryTime = 5 * time.Second
defaultRunDuration = 24 * time.Hour
revidStopTime = 5 * time.Second
defaultLogPath = "/var/log/netsender"
pkg = "revid-cli:"
netSendRetryTime = 5 * time.Second
defaultRunDuration = 24 * time.Hour
revidStopTime = 5 * time.Second
defaultLogPath = "/var/log/netsender"
pkg = "revid-cli:"
defaultLogVerbosity = logger.Info
defaultSleepTime = 60 // Seconds
)
// canProfile is set to false with revid-cli is built with "-tags profile".
var canProfile = true
// The logger that will be used throughout
// The logger that will be used throughout.
var log *logger.Logger
const (
@ -265,16 +259,17 @@ func handleFlags() revid.Config {
}
// initialize then run the main NetSender client
func run(cfg revid.Config) error {
func run(cfg revid.Config) {
log.Log(logger.Info, pkg+"running in NetSender mode")
var vars map[string]string
var rv *revid.Revid
readPin := func(pin *netsender.Pin) error {
switch {
case pin.Name == "X23":
if rv == nil {
pin.Value = 0
}
pin.Value = rv.Bitrate()
case pin.Name[0] == 'X':
return sds.ReadSystem(pin)
@ -286,35 +281,10 @@ func run(cfg revid.Config) error {
ns, err := netsender.New(log, nil, readPin, nil)
if err != nil {
return err
}
rv, err = revid.New(cfg, ns)
if err != nil {
log.Log(logger.Fatal, pkg+"could not initialise revid", "error", err.Error())
}
vars, _ = ns.Vars()
vs := ns.VarSum()
// Update revid to get latest config settings from netreceiver.
err = rv.Update(vars)
if err != nil {
return err
}
// If mode on netreceiver isn't paused then we can start revid.
if ns.Mode() != paused && ns.Mode() != burst {
err = rv.Start()
if err != nil {
return err
}
}
if ns.Mode() == burst {
ns.SetMode(paused, &vs)
log.Log(logger.Fatal, pkg+"could not initialise netsender client")
}
var vs int
for {
err = ns.Run()
if err != nil {
@ -323,10 +293,13 @@ func run(cfg revid.Config) error {
continue
}
// If var sum hasn't change we continue
if vs == ns.VarSum() {
// If var sum hasn't changed we continue.
var vars map[string]string
newVs := ns.VarSum()
if vs == newVs {
goto sleep
}
vs = newVs
vars, err = ns.Vars()
if err != nil {
@ -334,35 +307,52 @@ func run(cfg revid.Config) error {
time.Sleep(netSendRetryTime)
continue
}
vs = ns.VarSum()
if rv == nil {
rv, err = revid.New(cfg, ns)
if err != nil {
log.Log(logger.Error, pkg+"could not initialise revid", "error", err.Error())
goto sleep
}
}
err = rv.Update(vars)
if err != nil {
return err
log.Log(logger.Error, pkg+"Couldn't update revid", "error", err.Error())
goto sleep
}
switch ns.Mode() {
case paused:
rv.Stop()
case normal:
err = rv.Start()
if err != nil {
return err
log.Log(logger.Error, pkg+"could not start revid", "error", err.Error())
ns.SetMode(paused, &vs)
goto sleep
}
case burst:
log.Log(logger.Info, pkg+"Starting burst...")
err = rv.Start()
if err != nil {
return err
log.Log(logger.Error, pkg+"could not start burst", "error", err.Error())
ns.SetMode(paused, &vs)
goto sleep
}
time.Sleep(time.Duration(rv.Config().BurstPeriod) * time.Second)
log.Log(logger.Info, pkg+"Stopping burst...")
rv.Stop()
ns.SetMode(paused, &vs)
}
sleep:
sleepTime, err := strconv.Atoi(ns.Param("mp"))
if err != nil {
return err
log.Log(logger.Error, pkg+"could not get sleep time, using default")
sleepTime = defaultSleepTime
}
time.Sleep(time.Duration(sleepTime) * time.Second)
}