several fixes

- wait for start/stop commands to finish running
- better logs
This commit is contained in:
ppom 2023-10-22 12:00:00 +02:00
parent ac2621911f
commit b7eeb51e82
4 changed files with 36 additions and 11 deletions

View File

@ -42,8 +42,17 @@ func cmdStdout(commandline []string) chan *string {
func runCommands(commands [][]string, moment string) { func runCommands(commands [][]string, moment string) {
for _, command := range commands { for _, command := range commands {
cmd := exec.Command(command[0], command[1:]...) cmd := exec.Command(command[0], command[1:]...)
cmd.WaitDelay = time.Minute
logger.Printf(logger.INFO, "%v command: run %v\n", moment, command)
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
logger.Printf(logger.ERROR, "couldn't execute %v command: %v", moment, err) logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err)
} else {
err := cmd.Wait()
if err != nil {
logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err)
}
} }
} }
} }

View File

@ -115,7 +115,7 @@ var exampleConf string
func Main() { func Main() {
if len(os.Args) <= 1 { if len(os.Args) <= 1 {
logger.Fatalln("No argument provided") logger.Fatalln("No argument provided. Try `reaction help`")
basicUsage() basicUsage()
os.Exit(1) os.Exit(1)
} else if os.Args[1] == "-h" || os.Args[1] == "--help" { } else if os.Args[1] == "-h" || os.Args[1] == "--help" {

View File

@ -45,11 +45,11 @@ func (c *Conf) setup() {
stream.name = streamName stream.name = streamName
if strings.Contains(stream.name, ".") { if strings.Contains(stream.name, ".") {
logger.Fatalln("Bad configuration: character '.' is not allowed in stream names", stream.name) logger.Fatalf("Bad configuration: character '.' is not allowed in stream names: '%v'", stream.name)
} }
if len(stream.Filters) == 0 { if len(stream.Filters) == 0 {
logger.Fatalln("Bad configuration: no filters configured in", stream.name) logger.Fatalf("Bad configuration: no filters configured in %v", stream.name)
} }
for filterName := range stream.Filters { for filterName := range stream.Filters {
@ -58,23 +58,23 @@ func (c *Conf) setup() {
filter.name = filterName filter.name = filterName
if strings.Contains(filter.name, ".") { if strings.Contains(filter.name, ".") {
logger.Fatalln("Bad configuration: character '.' is not allowed in filter names", filter.name) logger.Fatalf("Bad configuration: character '.' is not allowed in filter names: '%v'", filter.name)
} }
// Parse Duration // Parse Duration
if filter.RetryPeriod == "" { if filter.RetryPeriod == "" {
if filter.Retry > 1 { if filter.Retry > 1 {
logger.Fatalln("Bad configuration: retry but no retryduration in", stream.name, ".", filter.name) logger.Fatalf("Bad configuration: retry but no retryperiod in %v.%v", stream.name, filter.name)
} }
} else { } else {
retryDuration, err := time.ParseDuration(filter.RetryPeriod) retryDuration, err := time.ParseDuration(filter.RetryPeriod)
if err != nil { if err != nil {
logger.Fatalln("Bad configuration: Failed to parse retry time in", stream.name, ".", filter.name, ":", err) logger.Fatalf("Bad configuration: Failed to parse retry time in %v.%v: %v", stream.name, filter.name, err)
} }
filter.retryDuration = retryDuration filter.retryDuration = retryDuration
} }
if len(filter.Regex) == 0 { if len(filter.Regex) == 0 {
logger.Fatalln("Bad configuration: no regexes configured in", stream.name, ".", filter.name) logger.Fatalf("Bad configuration: no regexes configured in %v.%v", stream.name, filter.name)
} }
// Compute Regexes // Compute Regexes
// Look for Patterns inside Regexes // Look for Patterns inside Regexes
@ -140,16 +140,21 @@ func parseConf(filename string) *Conf {
var conf Conf var conf Conf
if filename[len(filename)-4:] == ".yml" || filename[len(filename)-5:] == ".yaml" { if filename[len(filename)-4:] == ".yml" || filename[len(filename)-5:] == ".yaml" {
logger.Println(logger.DEBUG, "yaml")
err = jsonnet.NewYAMLToJSONDecoder(data).Decode(&conf) err = jsonnet.NewYAMLToJSONDecoder(data).Decode(&conf)
if err != nil {
logger.Fatalln("Failed to parse yaml configuration file:", err)
}
} else { } else {
logger.Println(logger.DEBUG, "json")
var jsondata string var jsondata string
jsondata, err = jsonnet.MakeVM().EvaluateFile(filename) jsondata, err = jsonnet.MakeVM().EvaluateFile(filename)
if err == nil { if err == nil {
err = json.Unmarshal([]byte(jsondata), &conf) err = json.Unmarshal([]byte(jsondata), &conf)
} }
}
if err != nil { if err != nil {
logger.Fatalln("Failed to parse configuration file:", err) logger.Fatalln("Failed to parse json configuration file:", err)
}
} }
conf.setup() conf.setup()

View File

@ -5,6 +5,17 @@
}, },
}, },
start: [
['err'],
['sleep', '10'],
],
stop: [
['sleep', '1'],
['false'],
['true'],
],
streams: { streams: {
tailDown1: { tailDown1: {
cmd: ['sh', '-c', "echo 1 2 3 4 5 1 2 3 4 5 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 | tr ' ' '\n' | while read i; do sleep 2; echo found $(($i % 10)); done"], cmd: ['sh', '-c', "echo 1 2 3 4 5 1 2 3 4 5 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 | tr ' ' '\n' | while read i; do sleep 2; echo found $(($i % 10)); done"],