Added mode support for pausing revid and refactored updateRevid().

This commit is contained in:
Alan Noble 2018-05-07 10:46:44 +09:30
parent de633f1061
commit 8b42c57b9e
1 changed files with 63 additions and 24 deletions

View File

@ -69,6 +69,7 @@ const (
// Other misc consts
const (
netSendRetryTime = 5
sleepTime = 2 * 43200
defaultRunDuration = 2 * 43200
noOfConfigFlags = 21
@ -256,12 +257,31 @@ func main() {
}
time.Sleep(time.Duration(prepTime) * time.Second)
createRevidInstance()
revidInst.Start()
startRevid()
paused := false
// Is the netsender flag been used ? if so run this loop
for *netSenderFlagPtr {
periodicNetsenderReport()
varsChanged, err := netSend()
if err != nil {
config.Logger.Log("Error", err.Error())
time.Sleep(time.Duration(netSendRetryTime) * time.Second)
continue
}
if varsChanged {
if vars["mode"] == "Paused" {
if !paused {
config.Logger.Log("Info", "Pausing revid")
stopRevid()
paused = true
}
} else {
updateRevid(!paused)
if paused {
paused = false
}
}
}
sleepTime, _ := strconv.Atoi(netsender.GetConfigParam("monPeriod"))
time.Sleep(time.Duration(sleepTime) * time.Second)
}
@ -269,7 +289,7 @@ 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)
revidInst.Stop()
stopRevid()
}
func netsenderInit() {
@ -279,42 +299,62 @@ func netsenderInit() {
netsender.ExternalReader = revidReportActions
}
//periodicNetsenderReport is called by the main function every monPeriod seconds. It makes sure a config has been recieved, and then
//reports back CPU stats and updates vars
func periodicNetsenderReport() {
// netSend implements the NetSender client, and is called every monPeriod seconds.
// It handles NetReceiver configuration and sends requested data to the cloud.
// Finally, it updates vars and returns true when vars have changed
func netSend() (bool, error) {
var err error
if !netsender.IsConfigured() {
if err := netsender.GetConfig(); err != nil {
config.Logger.Log("Error", err.Error())
if err = netsender.GetConfig(); err != nil {
return false, err
}
}
inputs := netsender.SplitCSV(netsender.GetConfigParam("inputs"))
if _, reconfig, err := netsender.Send(netsender.RequestPoll, inputs); err == nil && reconfig {
var reconfig bool
if _, reconfig, err = netsender.Send(netsender.RequestPoll, inputs); err != nil {
return false, err
}
if reconfig {
if err = netsender.GetConfig(); err != nil {
config.Logger.Log("Error", err.Error())
return false, err
}
}
if cloudVarSum := netsender.GetVarSum(); cloudVarSum != varSum {
if newVars, err := netsender.GetVars(); err != nil {
config.Logger.Log("Error", err.Error())
} else {
varSum = cloudVarSum
for newVar, value := range newVars {
if currentValue, varExists := vars[newVar]; !varExists || currentValue != value {
vars[newVar] = value
}
var newVars map[string]string
if newVars, err = netsender.GetVars(); err != nil {
return false, err
}
varSum = cloudVarSum
for newVar, value := range newVars {
if currentValue, varExists := vars[newVar]; !varExists || currentValue != value {
vars[newVar] = value
}
}
updateRevid()
return true, nil
}
return false, nil
}
func updateRevid() {
// wrappers for stopping and starting revid
func startRevid() {
createRevidInstance()
revidInst.Start()
}
func stopRevid() {
revidInst.Stop()
time.Sleep(time.Duration(revidStopTime) * time.Second)
//look through var map and update revid where needed
}
func updateRevid(stop bool) {
if stop {
stopRevid()
}
//look through var map and update revid where needed
for key, value := range vars {
switch key {
case "FramesPerClip":
@ -382,8 +422,7 @@ func updateRevid() {
}
}
createRevidInstance()
revidInst.Start()
startRevid()
}
func revidReportActions(pin int) (int, error) {