Introduce Group config option

This commit is contained in:
Brian Picciano 2024-07-19 15:49:13 +02:00
parent 7f5c354d04
commit f5fce902e8
3 changed files with 42 additions and 9 deletions

View File

@ -35,6 +35,14 @@ processes:
# process to exit before sending it a SIGKILL (aka a kill -9). # process to exit before sending it a SIGKILL (aka a kill -9).
sigKillWait: 10s sigKillWait: 10s
# group can be used to control the order and grouping of processes as they
# shut down.
#
# Processes will not be shut down until all processes with a higher group
# number are already shut down. Processes with the same group number will be
# shut down simultaneously.
group: 1
# This process will not immediately exit when pmux tells it to do so, but pmux # This process will not immediately exit when pmux tells it to do so, but pmux
# will SIGKILL it after sigKillWait has elapsed. # will SIGKILL it after sigKillWait has elapsed.
stubborn-pinger: stubborn-pinger:

View File

@ -68,9 +68,25 @@ func (p *Pmux) Restart(name string) {
func (p *Pmux) Stop() { func (p *Pmux) Stop() {
var wg sync.WaitGroup var wg sync.WaitGroup
p.sysLogger.Println("killing child processes") var minGroup, maxGroup int
for _, proc := range p.processes {
if maxGroup < proc.cfg.Group {
maxGroup = proc.cfg.Group
}
if minGroup > proc.cfg.Group {
minGroup = proc.cfg.Group
}
}
for group := maxGroup; group >= minGroup; group-- {
p.sysLogger.Printf("killing child processes (group %d)", group)
for _, proc := range p.processes { for _, proc := range p.processes {
proc := proc proc := proc
if proc.cfg.Group != group {
continue
}
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -78,7 +94,8 @@ func (p *Pmux) Stop() {
proc.Stop() proc.Stop()
}() }()
} }
wg.Wait() wg.Wait()
}
p.sysLogger.Println("exited gracefully, ciao!") p.sysLogger.Println("exited gracefully, ciao!")
} }

View File

@ -49,6 +49,14 @@ type ProcessConfig struct {
// this function returns. If used with RunProcessOnce and an error is // this function returns. If used with RunProcessOnce and an error is
// returned, RunProcessOnce will return that error. // returned, RunProcessOnce will return that error.
StartAfterFunc func(context.Context) error StartAfterFunc func(context.Context) error
// Group can be used to control the order and grouping of processes as they
// shut down.
//
// Processes will not be shut down until all processes with a higher group
// number are already shut down. Processes with the same group number will
// be shut down simultaneously.
Group int
} }
func (cfg ProcessConfig) withDefaults() ProcessConfig { func (cfg ProcessConfig) withDefaults() ProcessConfig {