mirror of https://bitbucket.org/ausocean/av.git
Merged in gardening/cmd-revid (pull request #26)
cmd/revid-cli: minor clean-ups
This commit is contained in:
commit
5bc18e7ed9
|
@ -33,6 +33,7 @@ import (
|
|||
"fmt"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bitbucket.org/ausocean/av/revid"
|
||||
|
@ -42,6 +43,14 @@ import (
|
|||
linuxproc "github.com/c9s/goprocinfo/linux"
|
||||
)
|
||||
|
||||
const (
|
||||
// progName is the program name for logging purposes.
|
||||
progName = "revid-cli"
|
||||
|
||||
// Logging is set to INFO level.
|
||||
loggerVerbosity = 3
|
||||
)
|
||||
|
||||
// Indexes for configFlags
|
||||
const (
|
||||
inputPtr = iota
|
||||
|
@ -71,13 +80,10 @@ const (
|
|||
|
||||
// Other misc consts
|
||||
const (
|
||||
progName = "revid-cli"
|
||||
netSendRetryTime = 5
|
||||
sleepTime = 2 * 43200
|
||||
defaultRunDuration = 2 * 43200
|
||||
revidStopTime = 5
|
||||
prepTime = 20
|
||||
loggerVerbosity = 3
|
||||
netSendRetryTime = 5 * time.Second
|
||||
defaultRunDuration = 24 * time.Hour
|
||||
revidStopTime = 5 * time.Second
|
||||
prepTime = 20 * time.Second
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -88,7 +94,6 @@ const (
|
|||
|
||||
// Globals
|
||||
var (
|
||||
ns netsender.Netsender
|
||||
revidInst revid.Revid
|
||||
config revid.Config
|
||||
)
|
||||
|
@ -127,7 +132,7 @@ func main() {
|
|||
// Do we want a netsender session
|
||||
netSenderFlagPtr := flag.Bool("NetSender", false, "Are we checking vars through netsender?")
|
||||
// User might also want to define how long revid runs for
|
||||
runDurationPtr := flag.Int("runDuration", defaultRunDuration, "How long do you want revid to run for?")
|
||||
runDurationPtr := flag.Duration("runDuration", defaultRunDuration, "How long do you want revid to run for?")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
|
@ -242,26 +247,30 @@ func main() {
|
|||
config.Timeout = *configFlags[timeoutPtr]
|
||||
config.IntraRefreshPeriod = *configFlags[intraRefreshPeriodPtr]
|
||||
|
||||
var ns netsender.Netsender
|
||||
var vs int
|
||||
if *netSenderFlagPtr {
|
||||
// initialize NetSender and use NetSender's logger
|
||||
config.Logger = netsender.GetLogger()
|
||||
ns = netsender.NewNetsender(false, revidReportActions)
|
||||
|
||||
// FIXME(kortschak): Handle this error appropriately.
|
||||
ns, _ = netsender.NewNetsender(false, revidReportActions)
|
||||
|
||||
vs = ns.GetVarSum()
|
||||
} else {
|
||||
// alternatively, instantiate our own logger
|
||||
config.Logger = smartlogger.New(loggerVerbosity, smartlogger.File, "/var/log/netsender/")
|
||||
}
|
||||
|
||||
time.Sleep(time.Duration(prepTime) * time.Second)
|
||||
time.Sleep(prepTime)
|
||||
startRevid()
|
||||
paused := false
|
||||
|
||||
// loop in NetSender mode
|
||||
for *netSenderFlagPtr {
|
||||
if err := netSend(); err != nil {
|
||||
if err := sendTo(ns); err != nil {
|
||||
config.Logger.Log(progName, "Error", err.Error())
|
||||
time.Sleep(time.Duration(netSendRetryTime) * time.Second)
|
||||
time.Sleep(netSendRetryTime)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -270,7 +279,7 @@ func main() {
|
|||
vars, err := ns.GetVars()
|
||||
if err != nil {
|
||||
config.Logger.Log(progName, "Error", err.Error())
|
||||
time.Sleep(time.Duration(netSendRetryTime) * time.Second)
|
||||
time.Sleep(netSendRetryTime)
|
||||
continue
|
||||
}
|
||||
vs = ns.GetVarSum()
|
||||
|
@ -293,28 +302,26 @@ func main() {
|
|||
|
||||
// If we're not running a netsender session then we run revid for the amount
|
||||
// of time the user defined or the default 2 days
|
||||
time.Sleep(time.Duration(*runDurationPtr) * time.Second)
|
||||
time.Sleep(*runDurationPtr)
|
||||
stopRevid()
|
||||
}
|
||||
|
||||
// netSend implements the NetSender client, and is called every monPeriod seconds.
|
||||
// It handles NetReceiver configuration and sends requested data to the cloud.
|
||||
func netSend() error {
|
||||
// sendTo handles NetReceiver configuration and sends requested data to the cloud.
|
||||
func sendTo(ns netsender.Netsender) error {
|
||||
if !ns.IsConfigured() {
|
||||
if err := ns.Config(); err != nil {
|
||||
err := ns.Config()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
inputs := netsender.SplitCSV(ns.GetConfigParam("ip"))
|
||||
if _, reconfig, err := ns.Send(netsender.RequestPoll, inputs); err != nil {
|
||||
inputs := strings.Split(ns.GetConfigParam("ip"), ",")
|
||||
_, reconfig, err := ns.Send(netsender.RequestPoll, inputs)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if reconfig {
|
||||
if err := ns.Config(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if reconfig {
|
||||
return ns.Config()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -325,12 +332,26 @@ func startRevid() {
|
|||
revidInst.Start()
|
||||
}
|
||||
|
||||
func createRevidInstance() {
|
||||
// Try to create the revid instance with the given config
|
||||
var err error
|
||||
for revidInst, err = revid.NewRevid(config); err != nil; {
|
||||
// If the config does have a logger, use it to output error, otherwise
|
||||
// just output to std output
|
||||
if config.Logger != nil {
|
||||
config.Logger.Log(progName, "FATAL ERROR", err.Error())
|
||||
} else {
|
||||
fmt.Printf("FATAL ERROR: %v", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func stopRevid() {
|
||||
revidInst.Stop()
|
||||
|
||||
// FIXME(kortschak): Is this waiting on completion of work?
|
||||
// Use a wait group and Wait method if it is.
|
||||
time.Sleep(time.Duration(revidStopTime) * time.Second)
|
||||
time.Sleep(revidStopTime)
|
||||
}
|
||||
|
||||
func updateRevid(vars map[string]string, stop bool) {
|
||||
|
@ -460,17 +481,3 @@ func revidReportActions(pin int) (int, error) {
|
|||
return -1, errors.New("External pin" + strconv.Itoa(pin) + " not defined")
|
||||
}
|
||||
}
|
||||
|
||||
func createRevidInstance() {
|
||||
// Try to create the revid instance with the given config
|
||||
var err error
|
||||
for revidInst, err = revid.NewRevid(config); err != nil; {
|
||||
// If the config does have a logger, use it to output error, otherwise
|
||||
// just output to std output
|
||||
if config.Logger != nil {
|
||||
config.Logger.Log(progName, "FATAL ERROR", err.Error())
|
||||
} else {
|
||||
fmt.Printf("FATAL ERROR: %v", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue