process

package
v0.0.0-...-446cb24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package process 是一个进程管理器,类似于supervisor,能够监控进程的运行状态,自动重启失败进程。

manager := NewManager(3*time.Second, func(logType OutputType, line string, process *Process) {
	color.Green("%s[%s] => %s\n", process.GetName(), logType, line)
})
manager.AddProgram("test", "/bin/sleep 1", 5, "")
manager.AddProgram("prometheus", "/bin/echo Hello", 1, "")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

manager.Watch(ctx)

Index

Constants

View Source
const (
	// LogTypeStderr stderr output
	LogTypeStderr = OutputType("stderr")
	// LogTypeStdout stdout output
	LogTypeStdout = OutputType("stdout")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is process manager

func NewManager

func NewManager(closeTimeout time.Duration, processOutputFunc OutputFunc) *Manager

NewManager create a new process manager

func (*Manager) AddProgram

func (manager *Manager) AddProgram(name string, command string, procNum int, username string)

AddProgram add a new program to manager

func (*Manager) Programs

func (manager *Manager) Programs() map[string]*Program

Programs return all programs

func (*Manager) Watch

func (manager *Manager) Watch(ctx context.Context)

Watch start watch process

type OutputFunc

type OutputFunc func(logType OutputType, line string, process *Process)

OutputFunc process output handler

type OutputType

type OutputType string

OutputType command output type

type Process

type Process struct {
	*exec.Cmd
	// contains filtered or unexported fields
}

Process is a program instance

func NewProcess

func NewProcess(name string, command string, args []string, username string) *Process

NewProcess create a new process

func (*Process) GetArgs

func (process *Process) GetArgs() []string

GetArgs get command args

func (*Process) GetCommand

func (process *Process) GetCommand() string

GetCommand get command

func (*Process) GetLastErrorMessage

func (process *Process) GetLastErrorMessage() string

GetLastErrorMessage get last error message

func (*Process) GetName

func (process *Process) GetName() string

GetName get process name

func (*Process) GetPID

func (process *Process) GetPID() int

GetPID get process pid

func (*Process) GetUser

func (process *Process) GetUser() string

GetUser get process user

func (*Process) SetLastErrorMessage

func (process *Process) SetLastErrorMessage(msg string)

SetLastErrorMessage update last error message

func (*Process) SetPID

func (process *Process) SetPID(pid int)

SetPID update process pid

type Program

type Program struct {
	Name    string `json:"name,omitempty"`
	Command string `json:"command,omitempty"`
	User    string `json:"user,omitempty"`
	ProcNum int    `json:"proc_num,omitempty"`
	// contains filtered or unexported fields
}

Program is the program we want to execute

func NewProgram

func NewProgram(name, command, username string, procNum int) *Program

NewProgram create a new Program

func (*Program) Processes

func (program *Program) Processes() []*Process

Processes get all processes for the program

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL