2022-06-19 01:52:04 +00:00
|
|
|
// Package pmuxlib implements the process management aspects of the pmux
|
|
|
|
// process.
|
|
|
|
package pmuxlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-25 18:54:05 +00:00
|
|
|
"io"
|
2022-06-19 01:52:04 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
TimeFormat string `yaml:"timeFormat"`
|
|
|
|
Processes []ProcessConfig `yaml:"processes"`
|
|
|
|
}
|
|
|
|
|
2022-06-30 19:31:32 +00:00
|
|
|
// Run runs the given configuration as if this was a real pmux process. It will
|
|
|
|
// block until the context is canceled and all child processes have been cleaned
|
|
|
|
// up.
|
2022-10-25 18:54:05 +00:00
|
|
|
func Run(
|
|
|
|
ctx context.Context,
|
|
|
|
stdout, stderr io.Writer,
|
|
|
|
cfg Config,
|
|
|
|
) {
|
2022-06-19 01:52:04 +00:00
|
|
|
|
2022-10-25 18:54:05 +00:00
|
|
|
stdoutLogger := newLogger(stdout, logSepStdout, cfg.TimeFormat)
|
2022-06-19 01:52:04 +00:00
|
|
|
defer stdoutLogger.Close()
|
|
|
|
|
2022-10-25 18:54:05 +00:00
|
|
|
stderrLogger := newLogger(stderr, logSepStderr, cfg.TimeFormat)
|
2022-06-19 01:52:04 +00:00
|
|
|
defer stderrLogger.Close()
|
|
|
|
|
|
|
|
sysLogger := stderrLogger.withSep(logSepSys)
|
|
|
|
defer sysLogger.Println("exited gracefully, ciao!")
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer wg.Wait()
|
|
|
|
|
|
|
|
for _, cfgProc := range cfg.Processes {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(procCfg ProcessConfig) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
stdoutLogger := stdoutLogger.withPName(procCfg.Name)
|
|
|
|
stderrLogger := stderrLogger.withPName(procCfg.Name)
|
|
|
|
sysLogger := sysLogger.withPName(procCfg.Name)
|
|
|
|
|
|
|
|
sysLogger.Println("starting process")
|
|
|
|
defer sysLogger.Println("stopped process handler")
|
|
|
|
|
|
|
|
RunProcess(
|
|
|
|
ctx, stdoutLogger, stderrLogger, sysLogger, procCfg,
|
|
|
|
)
|
|
|
|
|
|
|
|
}(cfgProc)
|
|
|
|
}
|
|
|
|
}
|