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
}
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)
}

View File

@ -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 <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'],
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 <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,
// },
// },
// },
// },
// },
},
}