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