From 1bb24b443c1e6b40babcec395a1128ee9af61e94 Mon Sep 17 00:00:00 2001 From: ppom <> Date: Thu, 23 Nov 2023 12:00:00 +0100 Subject: [PATCH] Exit 0 when all is fine Fix #45 --- app/daemon.go | 21 +++++++++++++------- config/test.jsonnet | 47 +++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/app/daemon.go b/app/daemon.go index caccbb1..31c9e6c 100644 --- a/app/daemon.go +++ b/app/daemon.go @@ -39,7 +39,8 @@ func cmdStdout(commandline []string) chan *string { return lines } -func runCommands(commands [][]string, moment string) { +func runCommands(commands [][]string, moment string) bool { + ok := true for _, command := range commands { cmd := exec.Command(command[0], command[1:]...) cmd.WaitDelay = time.Minute @@ -48,13 +49,16 @@ func runCommands(commands [][]string, moment string) { if err := cmd.Start(); err != nil { logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err) + ok = false } else { err := cmd.Wait() if err != nil { logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err) + ok = false } } } + return ok } func (p *Pattern) notAnIgnore(match *string) bool { @@ -341,7 +345,7 @@ func Daemon(confFilename string) { actions = make(ActionsMap) matches = make(MatchesMap) - runCommands(conf.Start, "start") + _ = runCommands(conf.Start, "start") go DatabaseManager(conf) go MatchesManager() @@ -368,16 +372,16 @@ func Daemon(confFilename string) { logger.Printf(logger.ERROR, "%s stream finished", finishedStream.name) nbStreamsInExecution-- if nbStreamsInExecution == 0 { - quit(conf) + quit(conf, false) } case <-sigs: logger.Printf(logger.INFO, "Received SIGINT/SIGTERM, exiting") - quit(conf) + quit(conf, true) } } } -func quit(conf *Conf) { +func quit(conf *Conf, graceful bool) { // send stop to StreamManager·s close(stopStreams) logger.Println(logger.INFO, "Waiting for Streams to finish...") @@ -390,12 +394,15 @@ func quit(conf *Conf) { logger.Println(logger.INFO, "Waiting for Actions to finish...") wgActions.Wait() // run stop commands - runCommands(conf.Stop, "stop") + stopOk := runCommands(conf.Stop, "stop") // delete pipe err := os.Remove(*SocketPath) if err != nil { logger.Println(logger.ERROR, "Failed to remove socket:", err) } - os.Exit(3) + if !stopOk || !graceful { + os.Exit(1) + } + os.Exit(0) } diff --git a/config/test.jsonnet b/config/test.jsonnet index 468c831..b5fc18e 100644 --- a/config/test.jsonnet +++ b/config/test.jsonnet @@ -7,38 +7,19 @@ start: [ ['err'], - ['sleep', '10'], + ['sleep', '1'], ], stop: [ ['sleep', '1'], - ['false'], + // ['false'], ['true'], ], streams: { 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"], - filters: { - findIP: { - regex: ['^found $'], - retry: 3, - retryperiod: '30s', - actions: { - damn: { - cmd: ['echo', ''], - }, - undamn: { - cmd: ['echo', 'undamn', ''], - after: '30s', - onexit: true, - }, - }, - }, - }, - }, - tailDown2: { - cmd: ['sh', '-c', 'echo coucou; sleep 2m'], + cmd: ['sh', '-c', "echo 1 2 3 4 5 | 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"], filters: { findIP: { regex: ['^found $'], @@ -57,5 +38,25 @@ }, }, }, + // tailDown2: { + // cmd: ['sh', '-c', 'echo coucou; sleep 2m'], + // filters: { + // findIP: { + // regex: ['^found $'], + // retry: 3, + // retryperiod: '30s', + // actions: { + // damn: { + // cmd: ['echo', ''], + // }, + // undamn: { + // cmd: ['echo', 'undamn', ''], + // after: '30s', + // onexit: true, + // }, + // }, + // }, + // }, + // }, }, }