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"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"bitbucket.org/ausocean/av/revid"
|
"bitbucket.org/ausocean/av/revid"
|
||||||
|
@ -42,6 +43,14 @@ import (
|
||||||
linuxproc "github.com/c9s/goprocinfo/linux"
|
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
|
// Indexes for configFlags
|
||||||
const (
|
const (
|
||||||
inputPtr = iota
|
inputPtr = iota
|
||||||
|
@ -71,13 +80,10 @@ const (
|
||||||
|
|
||||||
// Other misc consts
|
// Other misc consts
|
||||||
const (
|
const (
|
||||||
progName = "revid-cli"
|
netSendRetryTime = 5 * time.Second
|
||||||
netSendRetryTime = 5
|
defaultRunDuration = 24 * time.Hour
|
||||||
sleepTime = 2 * 43200
|
revidStopTime = 5 * time.Second
|
||||||
defaultRunDuration = 2 * 43200
|
prepTime = 20 * time.Second
|
||||||
revidStopTime = 5
|
|
||||||
prepTime = 20
|
|
||||||
loggerVerbosity = 3
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -88,7 +94,6 @@ const (
|
||||||
|
|
||||||
// Globals
|
// Globals
|
||||||
var (
|
var (
|
||||||
ns netsender.Netsender
|
|
||||||
revidInst revid.Revid
|
revidInst revid.Revid
|
||||||
config revid.Config
|
config revid.Config
|
||||||
)
|
)
|
||||||
|
@ -127,7 +132,7 @@ func main() {
|
||||||
// Do we want a netsender session
|
// Do we want a netsender session
|
||||||
netSenderFlagPtr := flag.Bool("NetSender", false, "Are we checking vars through netsender?")
|
netSenderFlagPtr := flag.Bool("NetSender", false, "Are we checking vars through netsender?")
|
||||||
// User might also want to define how long revid runs for
|
// 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()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -242,26 +247,30 @@ func main() {
|
||||||
config.Timeout = *configFlags[timeoutPtr]
|
config.Timeout = *configFlags[timeoutPtr]
|
||||||
config.IntraRefreshPeriod = *configFlags[intraRefreshPeriodPtr]
|
config.IntraRefreshPeriod = *configFlags[intraRefreshPeriodPtr]
|
||||||
|
|
||||||
|
var ns netsender.Netsender
|
||||||
var vs int
|
var vs int
|
||||||
if *netSenderFlagPtr {
|
if *netSenderFlagPtr {
|
||||||
// initialize NetSender and use NetSender's logger
|
// initialize NetSender and use NetSender's logger
|
||||||
config.Logger = netsender.GetLogger()
|
config.Logger = netsender.GetLogger()
|
||||||
ns = netsender.NewNetsender(false, revidReportActions)
|
|
||||||
|
// FIXME(kortschak): Handle this error appropriately.
|
||||||
|
ns, _ = netsender.NewNetsender(false, revidReportActions)
|
||||||
|
|
||||||
vs = ns.GetVarSum()
|
vs = ns.GetVarSum()
|
||||||
} else {
|
} else {
|
||||||
// alternatively, instantiate our own logger
|
// alternatively, instantiate our own logger
|
||||||
config.Logger = smartlogger.New(loggerVerbosity, smartlogger.File, "/var/log/netsender/")
|
config.Logger = smartlogger.New(loggerVerbosity, smartlogger.File, "/var/log/netsender/")
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Duration(prepTime) * time.Second)
|
time.Sleep(prepTime)
|
||||||
startRevid()
|
startRevid()
|
||||||
paused := false
|
paused := false
|
||||||
|
|
||||||
// loop in NetSender mode
|
// loop in NetSender mode
|
||||||
for *netSenderFlagPtr {
|
for *netSenderFlagPtr {
|
||||||
if err := netSend(); err != nil {
|
if err := sendTo(ns); err != nil {
|
||||||
config.Logger.Log(progName, "Error", err.Error())
|
config.Logger.Log(progName, "Error", err.Error())
|
||||||
time.Sleep(time.Duration(netSendRetryTime) * time.Second)
|
time.Sleep(netSendRetryTime)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +279,7 @@ func main() {
|
||||||
vars, err := ns.GetVars()
|
vars, err := ns.GetVars()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
config.Logger.Log(progName, "Error", err.Error())
|
config.Logger.Log(progName, "Error", err.Error())
|
||||||
time.Sleep(time.Duration(netSendRetryTime) * time.Second)
|
time.Sleep(netSendRetryTime)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
vs = ns.GetVarSum()
|
vs = ns.GetVarSum()
|
||||||
|
@ -293,28 +302,26 @@ func main() {
|
||||||
|
|
||||||
// If we're not running a netsender session then we run revid for the amount
|
// 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
|
// of time the user defined or the default 2 days
|
||||||
time.Sleep(time.Duration(*runDurationPtr) * time.Second)
|
time.Sleep(*runDurationPtr)
|
||||||
stopRevid()
|
stopRevid()
|
||||||
}
|
}
|
||||||
|
|
||||||
// netSend implements the NetSender client, and is called every monPeriod seconds.
|
// sendTo handles NetReceiver configuration and sends requested data to the cloud.
|
||||||
// It handles NetReceiver configuration and sends requested data to the cloud.
|
func sendTo(ns netsender.Netsender) error {
|
||||||
func netSend() error {
|
|
||||||
if !ns.IsConfigured() {
|
if !ns.IsConfigured() {
|
||||||
if err := ns.Config(); err != nil {
|
err := ns.Config()
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs := netsender.SplitCSV(ns.GetConfigParam("ip"))
|
inputs := strings.Split(ns.GetConfigParam("ip"), ",")
|
||||||
if _, reconfig, err := ns.Send(netsender.RequestPoll, inputs); err != nil {
|
_, reconfig, err := ns.Send(netsender.RequestPoll, inputs)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
}
|
||||||
if reconfig {
|
if reconfig {
|
||||||
if err := ns.Config(); err != nil {
|
return ns.Config()
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -325,12 +332,26 @@ func startRevid() {
|
||||||
revidInst.Start()
|
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() {
|
func stopRevid() {
|
||||||
revidInst.Stop()
|
revidInst.Stop()
|
||||||
|
|
||||||
// FIXME(kortschak): Is this waiting on completion of work?
|
// FIXME(kortschak): Is this waiting on completion of work?
|
||||||
// Use a wait group and Wait method if it is.
|
// 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) {
|
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")
|
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