process

package
v84.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2025 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package process provides a mechanism for running processes under a different user account to the current process

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatMemoryString added in v84.2.0

func FormatMemoryString(bytes uint64) string

FormatMemoryString formats a memory size in bytes into a human-readable string. The function returns a string with the value formatted to two decimal places and appended with the appropriate unit: "B" for bytes, "KiB" for kibibyte, "MiB" for mebibyte, or "GiB" for gibibyte.

func MonitorResources

func MonitorResources(
	absoluteHighMemoryThreshold,
	relativeHighMemoryThreshold uint64,
	allowedHighMemoryDuration time.Duration,
	disableOOMProtection bool,
	warn func(string, ...any),
	abort func(),
) func(chan *ResourceUsage, chan struct{})

MonitorResources returns a function that monitors the system's memory usage at 500ms intervals while a command is executing. It tracks peak memory used, total memory available, and calculates the average memory used. If memory usage exceeds relativeHighMemoryThreshold and available memory is less than absoluteHighMemoryThreshold for longer than allowedHighMemoryDurationSecs seconds, it calls the provided abort function. After the abort function is called, the monitoring stops. The function sends the collected ResourceUsage data through usageChan when monitoring stops. The monitoring can also be stopped by sending a signal through usageMeasurementsDone.

Types

type Command

type Command struct {
	// ResourceMonitor is a function that monitors the system's resource usage.
	// It should send the resource usage data to the first channel of type
	// *ResourceUsage and stop measuring usage when the second channel of
	// type struct{} is closed.
	ResourceMonitor func(chan *ResourceUsage, chan struct{})

	*exec.Cmd
	// contains filtered or unexported fields
}

func (*Command) DirectOutput

func (c *Command) DirectOutput(writer io.Writer)

func (*Command) Execute

func (c *Command) Execute() (r *Result)

func (*Command) Kill

func (c *Command) Kill() (killOutput string, err error)

func (*Command) SetEnv

func (c *Command) SetEnv(envVar, value string)

func (*Command) String

func (c *Command) String() string

type ResourceUsage

type ResourceUsage struct {
	AverageAvailableSystemMemory uint64
	AverageSystemMemoryUsed      uint64
	PeakSystemMemoryUsed         uint64
	TotalSystemMemory            uint64
}

type Result

type Result struct {
	SystemError error
	ExitError   *exec.ExitError
	Duration    time.Duration
	Aborted     bool
	KernelTime  time.Duration
	UserTime    time.Duration
	Usage       *ResourceUsage
}

func (*Result) CrashCause

func (r *Result) CrashCause() error

Unlike on Windows, if there is a system error, we don't crash the worker, since this can be caused by task that tries to execute a non-existing command or a file that isn't executable. On Windows all commands are wrapped in a command shell execution, where not being able to execute a shell should cause the worker to panic.

func (*Result) Crashed

func (r *Result) Crashed() bool

Unlike on Windows, if there is a system error, we don't crash the worker, since this can be caused by task that tries to execute a non-existing command or a file that isn't executable. On Windows all commands are wrapped in a command shell execution, where not being able to execute a shell should cause the worker to panic.

See https://bugzil.la/1479415

func (*Result) ExitCode

func (r *Result) ExitCode() int

ExitCode returns the exit code, or

-1 if the process has not exited
-2 if the process crashed
-3 it could not be established what happened
-4 if process was aborted

func (*Result) Failed

func (r *Result) Failed() bool

Unlike on Windows, a system error is grounds for a task failure, rather than a task exception, since it can be caused by e.g. a task trying to execute a command that doesn't exist, or trying to execute a file that isn't executable. Therefore a system error, or an exit error (process ran but returned non-zero exit code) or a task abortion are all task failures.

See https://bugzil.la/1479415

func (*Result) FailureCause

func (r *Result) FailureCause() error

func (*Result) String

func (r *Result) String() string

func (*Result) Succeeded

func (r *Result) Succeeded() bool

func (*Result) Verdict

func (r *Result) Verdict() string

Jump to

Keyboard shortcuts

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