Exit 0 when all is fine

Fix #45
This commit is contained in:
ppom 2023-11-23 12:00:00 +01:00
parent 831f3d0247
commit 1bb24b443c
2 changed files with 38 additions and 30 deletions

View File

@ -39,7 +39,8 @@ func cmdStdout(commandline []string) chan *string {
return lines return lines
} }
func runCommands(commands [][]string, moment string) { func runCommands(commands [][]string, moment string) bool {
ok := true
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 cmd.WaitDelay = time.Minute
@ -48,13 +49,16 @@ func runCommands(commands [][]string, moment string) {
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err) logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err)
ok = false
} else { } else {
err := cmd.Wait() err := cmd.Wait()
if err != nil { if err != nil {
logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err) logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err)
ok = false
} }
} }
} }
return ok
} }
func (p *Pattern) notAnIgnore(match *string) bool { func (p *Pattern) notAnIgnore(match *string) bool {
@ -341,7 +345,7 @@ func Daemon(confFilename string) {
actions = make(ActionsMap) actions = make(ActionsMap)
matches = make(MatchesMap) matches = make(MatchesMap)
runCommands(conf.Start, "start") _ = runCommands(conf.Start, "start")
go DatabaseManager(conf) go DatabaseManager(conf)
go MatchesManager() go MatchesManager()
@ -368,16 +372,16 @@ func Daemon(confFilename string) {
logger.Printf(logger.ERROR, "%s stream finished", finishedStream.name) logger.Printf(logger.ERROR, "%s stream finished", finishedStream.name)
nbStreamsInExecution-- nbStreamsInExecution--
if nbStreamsInExecution == 0 { if nbStreamsInExecution == 0 {
quit(conf) quit(conf, false)
} }
case <-sigs: case <-sigs:
logger.Printf(logger.INFO, "Received SIGINT/SIGTERM, exiting") 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 // send stop to StreamManager·s
close(stopStreams) close(stopStreams)
logger.Println(logger.INFO, "Waiting for Streams to finish...") 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...") logger.Println(logger.INFO, "Waiting for Actions to finish...")
wgActions.Wait() wgActions.Wait()
// run stop commands // run stop commands
runCommands(conf.Stop, "stop") stopOk := runCommands(conf.Stop, "stop")
// delete pipe // delete pipe
err := os.Remove(*SocketPath) err := os.Remove(*SocketPath)
if err != nil { if err != nil {
logger.Println(logger.ERROR, "Failed to remove socket:", err) logger.Println(logger.ERROR, "Failed to remove socket:", err)
} }
os.Exit(3) if !stopOk || !graceful {
os.Exit(1)
}
os.Exit(0)
} }

View File

@ -7,38 +7,19 @@
start: [ start: [
['err'], ['err'],
['sleep', '10'], ['sleep', '1'],
], ],
stop: [ stop: [
['sleep', '1'], ['sleep', '1'],
['false'], // ['false'],
['true'], ['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 | tr ' ' '\n' | while read i; do sleep 2; echo found $(($i % 10)); done"],
filters: { // 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"],
findIP: {
regex: ['^found <num>$'],
retry: 3,
retryperiod: '30s',
actions: {
damn: {
cmd: ['echo', '<num>'],
},
undamn: {
cmd: ['echo', 'undamn', '<num>'],
after: '30s',
onexit: true,
},
},
},
},
},
tailDown2: {
cmd: ['sh', '-c', 'echo coucou; sleep 2m'],
filters: { filters: {
findIP: { findIP: {
regex: ['^found <num>$'], regex: ['^found <num>$'],
@ -57,5 +38,25 @@
}, },
}, },
}, },
// tailDown2: {
// cmd: ['sh', '-c', 'echo coucou; sleep 2m'],
// filters: {
// findIP: {
// regex: ['^found <num>$'],
// retry: 3,
// retryperiod: '30s',
// actions: {
// damn: {
// cmd: ['echo', '<num>'],
// },
// undamn: {
// cmd: ['echo', 'undamn', '<num>'],
// after: '30s',
// onexit: true,
// },
// },
// },
// },
// },
}, },
} }