2023-03-25 18:27:01 +01:00
|
|
|
package app
|
2023-03-19 23:10:18 +01:00
|
|
|
|
|
|
|
import (
|
2023-03-20 23:25:57 +01:00
|
|
|
"bufio"
|
2023-03-24 17:52:00 +01:00
|
|
|
"os"
|
2023-03-19 23:10:18 +01:00
|
|
|
"os/exec"
|
2023-04-27 10:42:19 +02:00
|
|
|
"os/signal"
|
2023-03-24 17:36:41 +01:00
|
|
|
"strings"
|
2023-03-25 18:04:44 +01:00
|
|
|
"sync"
|
2023-10-12 12:00:00 +02:00
|
|
|
"syscall"
|
2023-03-24 17:36:41 +01:00
|
|
|
"time"
|
2023-10-12 12:00:00 +02:00
|
|
|
|
|
|
|
"framagit.org/ppom/reaction/logger"
|
2023-03-19 23:10:18 +01:00
|
|
|
)
|
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
// Executes a command and channel-send its stdout
|
2023-03-25 18:04:44 +01:00
|
|
|
func cmdStdout(commandline []string) chan *string {
|
|
|
|
lines := make(chan *string)
|
2023-03-19 23:10:18 +01:00
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
go func() {
|
|
|
|
cmd := exec.Command(commandline[0], commandline[1:]...)
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("couldn't open stdout on command:", err)
|
|
|
|
}
|
2023-03-24 00:27:51 +01:00
|
|
|
if err := cmd.Start(); err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("couldn't start command:", err)
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
defer stdout.Close()
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
for scanner.Scan() {
|
2023-03-25 18:04:44 +01:00
|
|
|
line := scanner.Text()
|
|
|
|
lines <- &line
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.DEBUG, "stdout:", line)
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
close(lines)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return lines
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 12:00:00 +01:00
|
|
|
func runCommands(commands [][]string, moment string) bool {
|
|
|
|
ok := true
|
2023-10-18 12:00:00 +02:00
|
|
|
for _, command := range commands {
|
|
|
|
cmd := exec.Command(command[0], command[1:]...)
|
2023-10-22 12:00:00 +02:00
|
|
|
cmd.WaitDelay = time.Minute
|
|
|
|
|
|
|
|
logger.Printf(logger.INFO, "%v command: run %v\n", moment, command)
|
|
|
|
|
2023-10-18 12:00:00 +02:00
|
|
|
if err := cmd.Start(); err != nil {
|
2023-10-22 12:00:00 +02:00
|
|
|
logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err)
|
2023-11-23 12:00:00 +01:00
|
|
|
ok = false
|
2023-10-22 12:00:00 +02:00
|
|
|
} else {
|
|
|
|
err := cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf(logger.ERROR, "%v command: run %v: %v", moment, command, err)
|
2023-11-23 12:00:00 +01:00
|
|
|
ok = false
|
2023-10-22 12:00:00 +02:00
|
|
|
}
|
2023-10-18 12:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-23 12:00:00 +01:00
|
|
|
return ok
|
2023-10-18 12:00:00 +02:00
|
|
|
}
|
|
|
|
|
2023-08-21 23:33:56 +02:00
|
|
|
func (p *Pattern) notAnIgnore(match *string) bool {
|
2024-02-02 15:21:29 +01:00
|
|
|
for _, regex := range p.compiledIgnoreRegex {
|
|
|
|
if regex.MatchString(*match) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 23:33:56 +02:00
|
|
|
for _, ignore := range p.Ignore {
|
|
|
|
if ignore == *match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
// Whether one of the filter's regexes is matched on a line
|
2023-03-25 18:04:44 +01:00
|
|
|
func (f *Filter) match(line *string) string {
|
2023-03-24 00:27:51 +01:00
|
|
|
for _, regex := range f.compiledRegex {
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-03-25 18:04:44 +01:00
|
|
|
if matches := regex.FindStringSubmatch(*line); matches != nil {
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2024-01-09 12:00:00 +01:00
|
|
|
if f.pattern != nil {
|
|
|
|
match := matches[regex.SubexpIndex(f.pattern.name)]
|
|
|
|
|
|
|
|
if f.pattern.notAnIgnore(&match) {
|
|
|
|
logger.Printf(logger.INFO, "%s.%s: match [%v]\n", f.stream.name, f.name, match)
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Printf(logger.INFO, "%s.%s: match [.]\n", f.stream.name, f.name)
|
|
|
|
// No pattern, so this match will never actually be used
|
|
|
|
return "."
|
2023-08-21 23:33:56 +02:00
|
|
|
}
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
2023-03-20 23:25:57 +01:00
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
return ""
|
2023-03-20 23:25:57 +01:00
|
|
|
}
|
|
|
|
|
2023-09-22 18:09:31 +02:00
|
|
|
func (f *Filter) sendActions(match string, at time.Time) {
|
2023-03-24 00:27:51 +01:00
|
|
|
for _, a := range f.Actions {
|
2023-09-22 18:09:31 +02:00
|
|
|
actionsC <- PAT{match, a, at.Add(a.afterDuration)}
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
2023-03-20 23:25:57 +01:00
|
|
|
|
2023-09-22 18:09:31 +02:00
|
|
|
func (a *Action) exec(match string) {
|
2024-01-05 12:00:00 +01:00
|
|
|
defer wgActions.Done()
|
2023-04-27 11:58:57 +02:00
|
|
|
|
2024-01-09 12:00:00 +01:00
|
|
|
var computedCommand []string
|
|
|
|
|
|
|
|
if a.filter.pattern != nil {
|
2024-01-10 12:00:00 +01:00
|
|
|
computedCommand = make([]string, 0, len(a.Cmd))
|
2024-01-09 12:00:00 +01:00
|
|
|
|
|
|
|
for _, item := range a.Cmd {
|
|
|
|
computedCommand = append(computedCommand, strings.ReplaceAll(item, a.filter.pattern.nameWithBraces, match))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
computedCommand = a.Cmd
|
2024-01-05 12:00:00 +01:00
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2024-01-05 12:00:00 +01:00
|
|
|
logger.Printf(logger.INFO, "%s.%s.%s: run %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand)
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2024-01-05 12:00:00 +01:00
|
|
|
cmd := exec.Command(computedCommand[0], computedCommand[1:]...)
|
2023-03-24 18:06:57 +01:00
|
|
|
|
2024-01-05 12:00:00 +01:00
|
|
|
if ret := cmd.Run(); ret != nil {
|
|
|
|
logger.Printf(logger.ERROR, "%s.%s.%s: run %s, code %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand, ret)
|
|
|
|
}
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
|
2024-01-05 12:00:00 +01:00
|
|
|
func ActionsManager(concurrency int) {
|
|
|
|
// concurrency init
|
|
|
|
execActionsC := make(chan PA)
|
|
|
|
if concurrency > 0 {
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
go func() {
|
|
|
|
var pa PA
|
|
|
|
for {
|
|
|
|
pa = <-execActionsC
|
|
|
|
pa.a.exec(pa.p)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
var pa PA
|
|
|
|
for {
|
|
|
|
pa = <-execActionsC
|
|
|
|
go func(pa PA) {
|
|
|
|
pa.a.exec(pa.p)
|
|
|
|
}(pa)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
execAction := func(a *Action, p string) {
|
|
|
|
wgActions.Add(1)
|
|
|
|
execActionsC <- PA{p, a}
|
|
|
|
}
|
|
|
|
|
|
|
|
// main
|
2023-09-22 18:09:31 +02:00
|
|
|
pendingActionsC := make(chan PAT)
|
|
|
|
for {
|
|
|
|
select {
|
2023-10-01 12:00:00 +02:00
|
|
|
case pat := <-actionsC:
|
|
|
|
pa := PA{pat.p, pat.a}
|
|
|
|
pattern, action, then := pat.p, pat.a, pat.t
|
|
|
|
now := time.Now()
|
|
|
|
// check if must be executed now
|
2023-09-23 00:14:20 +02:00
|
|
|
if then.Compare(now) <= 0 {
|
2024-01-05 12:00:00 +01:00
|
|
|
execAction(action, pattern)
|
2023-09-22 18:09:31 +02:00
|
|
|
} else {
|
2023-09-24 16:01:56 +02:00
|
|
|
actionsLock.Lock()
|
2023-10-01 12:00:00 +02:00
|
|
|
if actions[pa] == nil {
|
|
|
|
actions[pa] = make(map[time.Time]struct{})
|
|
|
|
}
|
2024-01-05 12:00:00 +01:00
|
|
|
actions[pa][then] = struct{}{}
|
2023-09-24 16:01:56 +02:00
|
|
|
actionsLock.Unlock()
|
2023-10-01 12:00:00 +02:00
|
|
|
go func(insidePat PAT, insideNow time.Time) {
|
|
|
|
time.Sleep(insidePat.t.Sub(insideNow))
|
|
|
|
pendingActionsC <- insidePat
|
2023-09-23 00:14:20 +02:00
|
|
|
}(pat, now)
|
2023-09-22 18:09:31 +02:00
|
|
|
}
|
2023-10-01 12:00:00 +02:00
|
|
|
case pat := <-pendingActionsC:
|
|
|
|
pa := PA{pat.p, pat.a}
|
|
|
|
pattern, action, then := pat.p, pat.a, pat.t
|
2023-09-24 16:01:56 +02:00
|
|
|
actionsLock.Lock()
|
2023-10-01 12:00:00 +02:00
|
|
|
if actions[pa] != nil {
|
2024-01-05 12:00:00 +01:00
|
|
|
delete(actions[pa], then)
|
2023-10-01 12:00:00 +02:00
|
|
|
}
|
|
|
|
actionsLock.Unlock()
|
2024-01-05 12:00:00 +01:00
|
|
|
execAction(action, pattern)
|
2023-10-01 12:00:00 +02:00
|
|
|
case fo := <-flushToActionsC:
|
2023-10-04 12:00:00 +02:00
|
|
|
ret := make(ActionsMap)
|
2023-10-01 12:00:00 +02:00
|
|
|
actionsLock.Lock()
|
|
|
|
for pa := range actions {
|
|
|
|
if pa.p == fo.p {
|
|
|
|
for range actions[pa] {
|
2024-01-05 12:00:00 +01:00
|
|
|
execAction(pa.a, pa.p)
|
2023-10-01 12:00:00 +02:00
|
|
|
}
|
2023-10-04 12:00:00 +02:00
|
|
|
ret[pa] = actions[pa]
|
2023-10-01 12:00:00 +02:00
|
|
|
delete(actions, pa)
|
|
|
|
}
|
|
|
|
}
|
2023-09-24 16:01:56 +02:00
|
|
|
actionsLock.Unlock()
|
2023-10-01 12:00:00 +02:00
|
|
|
fo.ret <- ret
|
2023-09-22 18:09:31 +02:00
|
|
|
case _, _ = <-stopActions:
|
2023-09-24 16:01:56 +02:00
|
|
|
actionsLock.Lock()
|
2024-01-05 12:00:00 +01:00
|
|
|
for pa := range actions {
|
|
|
|
if pa.a.OnExit {
|
|
|
|
for range actions[pa] {
|
|
|
|
execAction(pa.a, pa.p)
|
|
|
|
}
|
2023-09-22 18:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-24 16:01:56 +02:00
|
|
|
actionsLock.Unlock()
|
2023-09-22 18:09:31 +02:00
|
|
|
wgActions.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
func MatchesManager() {
|
2023-10-01 12:00:00 +02:00
|
|
|
var fo FlushMatchOrder
|
2023-09-09 23:38:53 +02:00
|
|
|
var pft PFT
|
|
|
|
end := false
|
|
|
|
|
|
|
|
for !end {
|
|
|
|
select {
|
2023-10-01 12:00:00 +02:00
|
|
|
case fo = <-flushToMatchesC:
|
|
|
|
matchesManagerHandleFlush(fo)
|
|
|
|
case fo, ok := <-startupMatchesC:
|
2023-09-09 23:38:53 +02:00
|
|
|
if !ok {
|
|
|
|
end = true
|
|
|
|
} else {
|
2023-10-01 12:00:00 +02:00
|
|
|
_ = matchesManagerHandleMatch(fo)
|
2023-09-09 23:38:53 +02:00
|
|
|
}
|
2023-03-25 19:12:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
for {
|
|
|
|
select {
|
2023-10-01 12:00:00 +02:00
|
|
|
case fo = <-flushToMatchesC:
|
|
|
|
matchesManagerHandleFlush(fo)
|
2023-09-09 23:38:53 +02:00
|
|
|
case pft = <-matchesC:
|
2023-04-26 17:18:55 +02:00
|
|
|
|
2023-10-21 12:00:00 +02:00
|
|
|
entry := LogEntry{pft.t, 0, pft.p, pft.f.stream.name, pft.f.name, 0, false}
|
2023-03-25 19:12:11 +01:00
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
entry.Exec = matchesManagerHandleMatch(pft)
|
2023-03-25 19:12:11 +01:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
logsC <- entry
|
2023-09-09 20:36:41 +02:00
|
|
|
}
|
2023-09-09 23:38:53 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-26 17:18:55 +02:00
|
|
|
|
2023-10-01 12:00:00 +02:00
|
|
|
func matchesManagerHandleFlush(fo FlushMatchOrder) {
|
2023-10-04 12:00:00 +02:00
|
|
|
ret := make(MatchesMap)
|
2023-10-01 12:00:00 +02:00
|
|
|
matchesLock.Lock()
|
|
|
|
for pf := range matches {
|
|
|
|
if fo.p == pf.p {
|
|
|
|
if fo.ret != nil {
|
2023-10-04 12:00:00 +02:00
|
|
|
ret[pf] = matches[pf]
|
2023-10-01 12:00:00 +02:00
|
|
|
}
|
|
|
|
delete(matches, pf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
matchesLock.Unlock()
|
|
|
|
if fo.ret != nil {
|
|
|
|
fo.ret <- ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
func matchesManagerHandleMatch(pft PFT) bool {
|
2023-10-01 12:00:00 +02:00
|
|
|
matchesLock.Lock()
|
|
|
|
defer matchesLock.Unlock()
|
|
|
|
|
|
|
|
filter, pattern, then := pft.f, pft.p, pft.t
|
2023-09-24 16:44:16 +02:00
|
|
|
pf := PF{pft.p, pft.f}
|
2023-09-09 23:38:53 +02:00
|
|
|
|
|
|
|
if filter.Retry > 1 {
|
|
|
|
// make sure map exists
|
2023-09-24 16:44:16 +02:00
|
|
|
if matches[pf] == nil {
|
|
|
|
matches[pf] = make(map[time.Time]struct{})
|
2023-09-09 23:38:53 +02:00
|
|
|
}
|
|
|
|
// add new match
|
2023-09-24 16:44:16 +02:00
|
|
|
matches[pf][then] = struct{}{}
|
2023-09-24 17:28:37 +02:00
|
|
|
// remove match when expired
|
|
|
|
go func(pf PF, then time.Time) {
|
2023-10-01 12:00:00 +02:00
|
|
|
time.Sleep(then.Sub(time.Now()) + filter.retryDuration)
|
2023-09-24 17:28:37 +02:00
|
|
|
matchesLock.Lock()
|
|
|
|
if matches[pf] != nil {
|
|
|
|
// FIXME replace this and all similar occurences
|
|
|
|
// by clear() when switching to go 1.21
|
|
|
|
delete(matches[pf], then)
|
|
|
|
}
|
|
|
|
matchesLock.Unlock()
|
|
|
|
}(pf, then)
|
2023-09-09 23:38:53 +02:00
|
|
|
}
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-09-24 16:44:16 +02:00
|
|
|
if filter.Retry <= 1 || len(matches[pf]) >= filter.Retry {
|
|
|
|
delete(matches, pf)
|
2023-10-01 12:00:00 +02:00
|
|
|
filter.sendActions(pattern, then)
|
2023-09-09 23:38:53 +02:00
|
|
|
return true
|
2023-09-09 20:36:41 +02:00
|
|
|
}
|
2023-09-09 23:38:53 +02:00
|
|
|
return false
|
2023-03-25 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
func StreamManager(s *Stream, endedSignal chan *Stream) {
|
2023-09-22 18:09:31 +02:00
|
|
|
defer wgStreams.Done()
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Printf(logger.INFO, "%s: start %s\n", s.name, s.Cmd)
|
2023-03-24 00:27:51 +01:00
|
|
|
|
|
|
|
lines := cmdStdout(s.Cmd)
|
2023-04-27 11:58:57 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case line, ok := <-lines:
|
|
|
|
if !ok {
|
|
|
|
endedSignal <- s
|
|
|
|
return
|
|
|
|
}
|
2023-09-09 20:36:41 +02:00
|
|
|
for _, filter := range s.Filters {
|
2023-09-09 23:38:53 +02:00
|
|
|
if match := filter.match(line); match != "" {
|
|
|
|
matchesC <- PFT{match, filter, time.Now()}
|
|
|
|
}
|
2023-04-27 11:58:57 +02:00
|
|
|
}
|
2023-09-22 18:09:31 +02:00
|
|
|
case _, _ = <-stopStreams:
|
|
|
|
return
|
2023-04-27 11:58:57 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
var actions ActionsMap
|
|
|
|
var matches MatchesMap
|
|
|
|
var actionsLock sync.Mutex
|
|
|
|
var matchesLock sync.Mutex
|
|
|
|
|
2023-04-27 11:58:57 +02:00
|
|
|
var stopStreams chan bool
|
2023-09-22 18:09:31 +02:00
|
|
|
var stopActions chan bool
|
2023-03-25 18:04:44 +01:00
|
|
|
var wgActions sync.WaitGroup
|
2023-09-22 18:09:31 +02:00
|
|
|
var wgStreams sync.WaitGroup
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-09-25 20:42:42 +02:00
|
|
|
/*
|
2023-10-01 12:00:00 +02:00
|
|
|
<StreamCmds>
|
2023-09-25 20:42:42 +02:00
|
|
|
↓
|
|
|
|
StreamManager onstartup:matches
|
|
|
|
↓ ↓ ↑
|
|
|
|
matches→ MatchesManager →logs→ DatabaseManager ←·
|
|
|
|
↑ ↓ ↑
|
|
|
|
↑ actions→ ActionsManager ↑
|
|
|
|
↑ ↑ ↑
|
|
|
|
SocketManager →flushes→→→→→→→→→→·→→→→→→→→→→→→→→→→·
|
|
|
|
↑
|
2023-10-01 12:00:00 +02:00
|
|
|
<Clients>
|
2023-09-25 20:42:42 +02:00
|
|
|
*/
|
2023-09-09 23:38:53 +02:00
|
|
|
|
|
|
|
// DatabaseManager → MatchesManager
|
|
|
|
var startupMatchesC chan PFT
|
2023-09-22 18:09:31 +02:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
// StreamManager → MatchesManager
|
|
|
|
var matchesC chan PFT
|
2023-09-22 18:09:31 +02:00
|
|
|
|
2023-09-25 20:42:42 +02:00
|
|
|
// MatchesManager → DatabaseManager
|
|
|
|
var logsC chan LogEntry
|
2023-09-22 18:09:31 +02:00
|
|
|
|
2023-09-25 20:42:42 +02:00
|
|
|
// MatchesManager → ActionsManager
|
2023-09-22 18:09:31 +02:00
|
|
|
var actionsC chan PAT
|
2023-04-26 17:18:55 +02:00
|
|
|
|
2023-09-25 20:42:42 +02:00
|
|
|
// SocketManager, DatabaseManager → MatchesManager
|
2023-10-01 12:00:00 +02:00
|
|
|
var flushToMatchesC chan FlushMatchOrder
|
2023-09-25 20:42:42 +02:00
|
|
|
|
|
|
|
// SocketManager → ActionsManager
|
2023-10-01 12:00:00 +02:00
|
|
|
var flushToActionsC chan FlushActionOrder
|
2023-09-25 20:42:42 +02:00
|
|
|
|
|
|
|
// SocketManager → DatabaseManager
|
|
|
|
var flushToDatabaseC chan LogEntry
|
|
|
|
|
2023-09-03 12:13:18 +02:00
|
|
|
func Daemon(confFilename string) {
|
|
|
|
conf := parseConf(confFilename)
|
2023-09-06 02:00:33 +02:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
startupMatchesC = make(chan PFT)
|
2023-09-25 20:42:42 +02:00
|
|
|
matchesC = make(chan PFT)
|
|
|
|
logsC = make(chan LogEntry)
|
2023-09-22 18:09:31 +02:00
|
|
|
actionsC = make(chan PAT)
|
2023-10-01 12:00:00 +02:00
|
|
|
flushToMatchesC = make(chan FlushMatchOrder)
|
|
|
|
flushToActionsC = make(chan FlushActionOrder)
|
2023-09-25 20:42:42 +02:00
|
|
|
flushToDatabaseC = make(chan LogEntry)
|
2023-09-22 18:09:31 +02:00
|
|
|
stopActions = make(chan bool)
|
|
|
|
stopStreams = make(chan bool)
|
2023-09-24 16:01:56 +02:00
|
|
|
actions = make(ActionsMap)
|
|
|
|
matches = make(MatchesMap)
|
2023-09-09 23:38:53 +02:00
|
|
|
|
2023-11-23 12:00:00 +01:00
|
|
|
_ = runCommands(conf.Start, "start")
|
2023-10-18 12:00:00 +02:00
|
|
|
|
2023-09-09 20:42:47 +02:00
|
|
|
go DatabaseManager(conf)
|
2023-09-09 23:38:53 +02:00
|
|
|
go MatchesManager()
|
2024-01-05 12:00:00 +01:00
|
|
|
go ActionsManager(conf.Concurrency)
|
2023-05-01 18:21:31 +02:00
|
|
|
|
|
|
|
// Ready to start
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2023-04-26 17:18:55 +02:00
|
|
|
|
2023-03-25 18:04:44 +01:00
|
|
|
endSignals := make(chan *Stream)
|
2023-09-09 19:32:23 +02:00
|
|
|
nbStreamsInExecution := len(conf.Streams)
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
for _, stream := range conf.Streams {
|
2023-09-22 18:09:31 +02:00
|
|
|
wgStreams.Add(1)
|
2023-09-09 23:38:53 +02:00
|
|
|
go StreamManager(stream, endSignals)
|
2023-03-25 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
2023-10-01 12:00:00 +02:00
|
|
|
go SocketManager(conf.Streams)
|
2023-05-03 20:03:22 +02:00
|
|
|
|
2023-04-27 10:42:19 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case finishedStream := <-endSignals:
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Printf(logger.ERROR, "%s stream finished", finishedStream.name)
|
2023-09-09 19:32:23 +02:00
|
|
|
nbStreamsInExecution--
|
|
|
|
if nbStreamsInExecution == 0 {
|
2023-11-23 12:00:00 +01:00
|
|
|
quit(conf, false)
|
2023-04-27 10:42:19 +02:00
|
|
|
}
|
|
|
|
case <-sigs:
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Printf(logger.INFO, "Received SIGINT/SIGTERM, exiting")
|
2023-11-23 12:00:00 +01:00
|
|
|
quit(conf, true)
|
2023-04-27 10:42:19 +02:00
|
|
|
}
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
2023-04-27 10:42:19 +02:00
|
|
|
}
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-11-23 12:00:00 +01:00
|
|
|
func quit(conf *Conf, graceful bool) {
|
2023-09-22 18:09:31 +02:00
|
|
|
// send stop to StreamManager·s
|
2023-04-27 11:58:57 +02:00
|
|
|
close(stopStreams)
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.INFO, "Waiting for Streams to finish...")
|
2023-09-22 18:09:31 +02:00
|
|
|
wgStreams.Wait()
|
|
|
|
// ActionsManager calls wgActions.Done() when it has launched all pending actions
|
|
|
|
wgActions.Add(1)
|
|
|
|
// send stop to ActionsManager
|
|
|
|
close(stopActions)
|
2023-04-27 11:58:57 +02:00
|
|
|
// stop all actions
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.INFO, "Waiting for Actions to finish...")
|
2023-03-25 18:04:44 +01:00
|
|
|
wgActions.Wait()
|
2023-10-18 12:00:00 +02:00
|
|
|
// run stop commands
|
2023-11-23 12:00:00 +01:00
|
|
|
stopOk := runCommands(conf.Stop, "stop")
|
2023-05-03 20:03:22 +02:00
|
|
|
// delete pipe
|
2023-09-03 12:13:18 +02:00
|
|
|
err := os.Remove(*SocketPath)
|
2023-05-05 15:33:00 +02:00
|
|
|
if err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.ERROR, "Failed to remove socket:", err)
|
2023-05-05 15:33:00 +02:00
|
|
|
}
|
2023-04-27 11:58:57 +02:00
|
|
|
|
2023-11-23 12:00:00 +01:00
|
|
|
if !stopOk || !graceful {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|