command

package
v16.1.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2023 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowedEnvironment

func AllowedEnvironment(envs []string) []string

AllowedEnvironment filters the given slice of environment variables and returns all variables which are allowed per the variables defined above. This is useful for constructing a base environment in which a command can be run.

func ExitStatus

func ExitStatus(err error) (int, bool)

ExitStatus will return the exit-code from an error returned by Wait().

func InitContextStats

func InitContextStats(ctx context.Context) context.Context

InitContextStats returns a new context with `Stats` added to the given context.

Types

type Command

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

Command encapsulates a running exec.Cmd. The embedded exec.Cmd is terminated and reaped automatically when the context.Context that created it is canceled.

func New

func New(ctx context.Context, nameAndArgs []string, opts ...Option) (*Command, error)

New creates a Command from the given executable name and arguments On success, the Command contains a running subprocess. When ctx is canceled the embedded process will be terminated and reaped automatically.

func (*Command) Args

func (c *Command) Args() []string

Args is an accessor for the command arguments

func (*Command) Env

func (c *Command) Env() []string

Env is an accessor for the environment variables

func (*Command) Pid

func (c *Command) Pid() int

Pid is an accessor for the pid

func (*Command) Read

func (c *Command) Read(p []byte) (int, error)

Read calls Read() on the stdout pipe of the command.

func (*Command) Wait

func (c *Command) Wait() error

Wait calls Wait() on the exec.Cmd instance inside the command. This blocks until the command has finished and reports the command exit status via the error return value. Use ExitStatus to get the integer exit status from the error returned by Wait().

Wait returns a wrapped context error if the process was reaped due to the context being done.

func (*Command) Write

func (c *Command) Write(p []byte) (int, error)

Write calls Write() on the stdin pipe of the command.

type Option

type Option func(cfg *config)

Option is an option that can be passed to `New()` for controlling how the command is being created.

func WithCgroup

func WithCgroup(cgroupsManager cgroups.Manager, opts ...cgroups.AddCommandOption) Option

WithCgroup adds the spawned command to a Cgroup. The bucket used will be derived from the command's arguments and/or from the repository.

func WithCommandGitVersion

func WithCommandGitVersion(gitCmdVersion string) Option

WithCommandGitVersion overrides the "git_version" label used in metrics.

func WithCommandName

func WithCommandName(commandName, subcommandName string) Option

WithCommandName overrides the "cmd" and "subcmd" label used in metrics.

func WithDir

func WithDir(dir string) Option

WithDir will set up the command to be ran in the specific directory.

func WithEnvironment

func WithEnvironment(environment []string) Option

WithEnvironment sets up environment variables that shall be set for the command.

func WithFinalizer

func WithFinalizer(finalizer func(context.Context, *Command)) Option

WithFinalizer sets up the finalizer to be run when the command is being wrapped up. It will be called after `Wait()` has returned.

func WithSetupStdin

func WithSetupStdin() Option

WithSetupStdin instructs New() to configure the stdin pipe of the command it is creating. This allows you call Write() on the command as if it is an ordinary io.Writer, sending data directly to the stdin of the process.

func WithStderr

func WithStderr(stderr io.Writer) Option

WithStderr sets up the command to write standard error to the given writer.

func WithStdin

func WithStdin(stdin io.Reader) Option

WithStdin sets up the command to read from the given reader.

func WithStdout

func WithStdout(stdout io.Writer) Option

WithStdout sets up the command to write standard output to the given writer.

type SpawnConfig

type SpawnConfig struct {
	// This default value (10 seconds) is very high. Spawning should take
	// milliseconds or less. If we hit 10 seconds, something is wrong, and
	// failing the request will create breathing room. Can be modified at
	// runtime with the GITALY_COMMAND_SPAWN_TIMEOUT environment variable.
	Timeout time.Duration `split_words:"true" default:"10s"`

	// MaxSpawnParallel limits the number of goroutines that can spawn a
	// process at the same time. These parallel spawns will contend for a
	// single lock (syscall.ForkLock) in exec.Cmd.Start(). Can be modified at
	// runtime with the GITALY_COMMAND_SPAWN_MAX_PARALLEL variable.
	//
	// Note that this does not limit the total number of child processes that
	// can be attached to Gitaly at the same time. It only limits the rate at
	// which we can create new child processes.
	MaxParallel int `split_words:"true" default:"10"`
}

SpawnConfig holds configuration for command spawning timeouts and parallelism.

type Stats

type Stats struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Stats records statistics about a command that was spawned.

func StatsFromContext

func StatsFromContext(ctx context.Context) *Stats

StatsFromContext gets the `Stats` from the given context.

func (*Stats) Fields

func (stats *Stats) Fields() logrus.Fields

Fields returns all the stats as logrus.Fields

func (*Stats) RecordMax

func (stats *Stats) RecordMax(key string, value int)

RecordMax will store the max value for a given key.

func (*Stats) RecordMetadata

func (stats *Stats) RecordMetadata(key string, value string)

RecordMetadata records metadata for the given key.

func (*Stats) RecordSum

func (stats *Stats) RecordSum(key string, value int)

RecordSum sums up all the values for a given key.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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