Documentation
¶
Index ¶
- Constants
- Variables
- func DecoupleFromParent(cmd *exec.Cmd)
- func FindProcess(pid Pid_t, expectedStartTime time.Time) (*os.Process, error)
- func ForkFromParent(cmd *exec.Cmd)
- func HasExpectedIdentityTime(proc *ps.Process, expectedIdentityTime time.Time) bool
- func IsEarlyProcessExitError(err error) bool
- func PidT_ToInt(val Pid_t) (int, error)
- func PidT_ToUint32(val Pid_t) (uint32, error)
- func ProcessIdentityTime(pid Pid_t) time.Time
- func RunToCompletion(ctx context.Context, executor Executor, cmd *exec.Cmd) (int32, error)
- func RunWithTimeout(ctx context.Context, executor Executor, cmd *exec.Cmd) (int32, error)
- func StartTimeForProcess(pid Pid_t) time.Time
- type ChannelProcessExitHandler
- type ErrProcessNotFound
- type Executor
- type OSExecutor
- func (e *OSExecutor) Dispose()
- func (e *OSExecutor) StartAndForget(cmd *exec.Cmd, flags ProcessCreationFlag) (Pid_t, time.Time, error)
- func (e *OSExecutor) StartProcess(ctx context.Context, cmd *exec.Cmd, handler ProcessExitHandler, ...) (Pid_t, time.Time, func(), error)
- func (e *OSExecutor) StopProcess(pid Pid_t, processStartTime time.Time) error
- type Pid_t
- type ProcessCreationFlag
- type ProcessExitHandler
- type ProcessExitHandlerFunc
- type ProcessExitInfo
- type ProcessTreeItem
- type WaitKey
- type Waitable
- type WaitableProcess
Constants ¶
const ( // A valid exit code of a process is a non-negative number. We use UnknownExitCode to indicate that we have not obtained the exit code yet. UnknownExitCode int32 = -1 // Unknown PID code is used when replica is not started (or fails to start) UnknownPID Pid_t = -1 )
const ProcessIdentityTimeMaximumDifference = 2 * time.Millisecond
We serialize timestamps with millisecond precision, so a maximum couple of milliseconds of difference works well.
Variables ¶
var ( This func() (ProcessTreeItem, error) // Essentially the same as ps.ErrorProcessNotRunning, but we do not want to // expose the ps package outside of this package. ErrorProcessNotFound = errors.New("process does not exist") )
var (
ErrDisposed = errors.New("the process executor has been disposed")
)
var (
ErrTimedOutWaitingForProcessToStop = errors.New("timed out waiting for process to stop")
)
Functions ¶
func DecoupleFromParent ¶
Use separate process group so this process exit will not affect the children.
func FindProcess ¶
Returns the process with the given PID. If the expectedStartTime is not zero, the process start time is checked to match the expected start time.
func ForkFromParent ¶
Use separate process group so this process exit will not affect the children. This is the same as DecoupleFromParent on Unix systems.
func HasExpectedIdentityTime ¶
func IsEarlyProcessExitError ¶
Checks if the error is associated with early exit of a process, which is often expected.
func PidT_ToInt ¶
func PidT_ToUint32 ¶
func ProcessIdentityTime ¶
Gets the raw start time for the process, used to verify process identity. This time may not match the wall clock time returned by StartTimeForProcess() on all OS platforms and should not be used for display purposes, but is stable across system clock changes.
func RunToCompletion ¶
Runs the command as a child process to completion. Returns exit code, or error if the process could not be started/tracked for some reason.
The context parameter is used to request cancellation of the process, but the call to RunToCompletion() will not return until the process exits and all its output is captured. Do not assume the call will end quickly if the context is cancelled.
func RunWithTimeout ¶
Runs the command as a child process to completion, unless the passed context is cancelled, or its deadline is exceeded.
func StartTimeForProcess ¶
Returns the creation time as a time.Time for a process. This time is intended for display purposes and may differ from the raw start time used to verify process identity and the value returned can change due to clock adjustments etc.
Types ¶
type ChannelProcessExitHandler ¶
type ChannelProcessExitHandler struct {
// contains filtered or unexported fields
}
A simple process exit handler that writes the finished process status to a channel
func NewChannelProcessExitHandler ¶
func NewChannelProcessExitHandler(c chan ProcessExitInfo) *ChannelProcessExitHandler
func (*ChannelProcessExitHandler) OnProcessExited ¶
func (eh *ChannelProcessExitHandler) OnProcessExited(pid Pid_t, exitCode int32, err error)
type ErrProcessNotFound ¶
func (ErrProcessNotFound) Error ¶
func (e ErrProcessNotFound) Error() string
type Executor ¶
type Executor interface {
// Starts the process described by given command instance.
// When the passed context is cancelled, the process is automatically terminated.
// Returns the process PID, process start time, and a function that enables process exit notifications
// delivered to the exit handler.
StartProcess(
ctx context.Context,
cmd *exec.Cmd,
exitHandler ProcessExitHandler,
creationFlags ProcessCreationFlag,
) (pid Pid_t, startTime time.Time, startWaitForProcessExit func(), err error)
// Stops the process with a given PID.
// The processStartTime, if provided (time.IsZero() returns false), is used to further validate the process to be stopped.
// (to protect against stopping a wrong process, if the PID was reused).
StopProcess(pid Pid_t, processStartTime time.Time) error
// Starts a process that does not need to be tracked (the caller is not interested in its exit code),
// minimizing resource usage. An error is returned if the process could not be started.
StartAndForget(cmd *exec.Cmd, creationFlags ProcessCreationFlag) (pid Pid_t, startTime time.Time, err error)
// Disposes the executor. Processes started with CreationFlagEnsureKillOnDispose will be terminated.
// Other processes will be waited on (so that they do not become zombies), but not terminated.
Dispose()
}
func NewOSExecutor ¶
type OSExecutor ¶
type OSExecutor struct {
// contains filtered or unexported fields
}
func (*OSExecutor) StartAndForget ¶
func (e *OSExecutor) StartAndForget(cmd *exec.Cmd, flags ProcessCreationFlag) (Pid_t, time.Time, error)
func (*OSExecutor) StartProcess ¶
func (e *OSExecutor) StartProcess( ctx context.Context, cmd *exec.Cmd, handler ProcessExitHandler, flags ProcessCreationFlag, ) (Pid_t, time.Time, func(), error)
func (*OSExecutor) StopProcess ¶
func (e *OSExecutor) StopProcess(pid Pid_t, processStartTime time.Time) error
type Pid_t ¶
type Pid_t int64
Pid_t is a type used to represent process IDs. The domain is all unsigned 32-bit integers, but we use a signed type to align with Kubernetes API conventions.
func Int64_ToPidT ¶
func StringToPidT ¶
func Uint32_ToPidT ¶
type ProcessCreationFlag ¶
type ProcessCreationFlag uint32
const ( CreationFlagsNone ProcessCreationFlag = 0 // Ensures that the process is killed when process executor is disposed. // This has currently no effect on Mac/Linux, but on Windows the process started with this flag // will be added to the "killer job" object that terminates all processes assigned to it. CreationFlagEnsureKillOnDispose = 0x1 )
type ProcessExitHandler ¶
type ProcessExitHandler interface {
// Indicates that process with a given PID has finished execution
// If err is nil, the process exit code was properly captured and the exitCode value is valid.
// Note that if the process was terminated by a signal, the exit code will be UnknownExitCode (-1).
// if err is not nil, there was a problem tracking the process and the exitCode value is not valid
OnProcessExited(pid Pid_t, exitCode int32, err error)
}
type ProcessExitHandlerFunc ¶
Make it easy to supply a function as a process exit handler.
func (ProcessExitHandlerFunc) OnProcessExited ¶
func (f ProcessExitHandlerFunc) OnProcessExited(pid Pid_t, exitCode int32, err error)
type ProcessExitInfo ¶
func NewProcessExitInfo ¶
func NewProcessExitInfo() ProcessExitInfo
type ProcessTreeItem ¶
type ProcessTreeItem struct {
Pid Pid_t
IdentityTime time.Time // Used to distinguish between different instances of processes with the same PID, may not be a valid wall-clock time.
}
func GetProcessTree ¶
func GetProcessTree(rootP ProcessTreeItem) ([]ProcessTreeItem, error)
Returns the list of ID for a given process and its children The list is ordered starting with the root of the hierarchy, then the children, then the grandchildren etc.
type Waitable ¶
type Waitable interface {
Wait() error
Info() string
Flags() ProcessCreationFlag
}
type WaitableProcess ¶
type WaitableProcess struct {
WaitPollInterval time.Duration
// contains filtered or unexported fields
}
func FindWaitableProcess ¶
func FindWaitableProcess(pid Pid_t, processStartTime time.Time) (*WaitableProcess, error)
func (*WaitableProcess) Kill ¶
func (p *WaitableProcess) Kill() error