telegraf/internal/process/process.go

166 lines
3.1 KiB
Go
Raw Normal View History

2020-06-05 07:09:22 +08:00
package process
import (
"context"
2020-06-27 04:38:07 +08:00
"errors"
2020-06-05 07:09:22 +08:00
"fmt"
"io"
"io/ioutil"
"os/exec"
"sync"
"time"
2020-06-27 04:38:07 +08:00
"github.com/influxdata/telegraf"
2020-06-05 07:09:22 +08:00
)
// Process is a long-running process manager that will restart processes if they stop.
type Process struct {
Cmd *exec.Cmd
Stdin io.WriteCloser
Stdout io.ReadCloser
Stderr io.ReadCloser
ReadStdoutFn func(io.Reader)
ReadStderrFn func(io.Reader)
RestartDelay time.Duration
2020-06-27 04:38:07 +08:00
Log telegraf.Logger
2020-06-05 07:09:22 +08:00
cancel context.CancelFunc
mainLoopWg sync.WaitGroup
}
// New creates a new process wrapper
func New(command []string) (*Process, error) {
2020-06-27 04:38:07 +08:00
if len(command) == 0 {
return nil, errors.New("no command")
}
2020-06-05 07:09:22 +08:00
p := &Process{
RestartDelay: 5 * time.Second,
}
if len(command) > 1 {
p.Cmd = exec.Command(command[0], command[1:]...)
} else {
p.Cmd = exec.Command(command[0])
}
var err error
p.Stdin, err = p.Cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("error opening stdin pipe: %w", err)
}
p.Stdout, err = p.Cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("error opening stdout pipe: %w", err)
}
p.Stderr, err = p.Cmd.StderrPipe()
if err != nil {
return nil, fmt.Errorf("error opening stderr pipe: %w", err)
}
return p, nil
}
// Start the process
func (p *Process) Start() error {
ctx, cancel := context.WithCancel(context.Background())
p.cancel = cancel
if err := p.cmdStart(); err != nil {
return err
}
2020-06-27 04:38:07 +08:00
p.mainLoopWg.Add(1)
2020-06-05 07:09:22 +08:00
go func() {
if err := p.cmdLoop(ctx); err != nil {
2020-06-27 04:38:07 +08:00
p.Log.Errorf("Process quit with message: %v", err)
2020-06-05 07:09:22 +08:00
}
p.mainLoopWg.Done()
}()
return nil
}
func (p *Process) Stop() {
if p.cancel != nil {
p.cancel()
}
p.mainLoopWg.Wait()
}
func (p *Process) cmdStart() error {
2020-06-27 04:38:07 +08:00
p.Log.Infof("Starting process: %s %s", p.Cmd.Path, p.Cmd.Args)
2020-06-05 07:09:22 +08:00
if err := p.Cmd.Start(); err != nil {
2020-06-27 04:38:07 +08:00
return fmt.Errorf("error starting process: %s", err)
2020-06-05 07:09:22 +08:00
}
return nil
}
// cmdLoop watches an already running process, restarting it when appropriate.
func (p *Process) cmdLoop(ctx context.Context) error {
go func() {
<-ctx.Done()
if p.Stdin != nil {
p.Stdin.Close()
gracefulStop(p.Cmd, 5*time.Second)
}
}()
for {
err := p.cmdWait()
if isQuitting(ctx) {
2020-06-27 04:38:07 +08:00
p.Log.Infof("Process %s shut down", p.Cmd.Path)
2020-06-05 07:09:22 +08:00
return nil
}
2020-06-27 04:38:07 +08:00
p.Log.Errorf("Process %s exited: %v", p.Cmd.Path, err)
p.Log.Infof("Restarting in %s...", time.Duration(p.RestartDelay))
2020-06-05 07:09:22 +08:00
select {
case <-ctx.Done():
return nil
case <-time.After(time.Duration(p.RestartDelay)):
// Continue the loop and restart the process
if err := p.cmdStart(); err != nil {
return err
}
}
}
}
func (p *Process) cmdWait() error {
var wg sync.WaitGroup
if p.ReadStdoutFn == nil {
p.ReadStdoutFn = defaultReadPipe
}
if p.ReadStderrFn == nil {
p.ReadStderrFn = defaultReadPipe
}
wg.Add(1)
go func() {
p.ReadStdoutFn(p.Stdout)
wg.Done()
}()
wg.Add(1)
go func() {
p.ReadStderrFn(p.Stderr)
wg.Done()
}()
wg.Wait()
return p.Cmd.Wait()
}
func isQuitting(ctx context.Context) bool {
return ctx.Err() != nil
}
func defaultReadPipe(r io.Reader) {
io.Copy(ioutil.Discard, r)
}