process

package
v0.0.0-...-31c1c1e Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package process is a wrapper of exec.Cmd for controlling a ffmpeg process. It could be used to run other executables but it is tailored to the specifics of ffmpeg, e.g. only stderr is captured, and some exit codes != 0 plus certain signals are still considered as a non-error exit condition.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Binary         string                // Path to the ffmpeg binary
	Args           []string              // List of arguments for the binary
	Reconnect      bool                  // Whether to restart the process if it exited
	ReconnectDelay time.Duration         // Duration to wait before restarting the process
	StaleTimeout   time.Duration         // Kill the process after this duration if it doesn't produce any output
	LimitCPU       float64               // Kill the process if the CPU usage in percent is above this value
	LimitMemory    uint64                // Kill the process if the memory consumption in bytes is above this value
	LimitDuration  time.Duration         // Kill the process if the limits are exceeded for this duration
	Parser         Parser                // A parser for the output of the process
	OnStart        func()                // A callback which is called after the process started
	OnExit         func()                // A callback which is called after the process exited
	OnStateChange  func(from, to string) // A callback which is called after a state changed
	Logger         log.Logger
}

Config is the configuration of a process

type LimitFunc

type LimitFunc func(cpu float64, memory uint64)

type Limiter

type Limiter interface {
	// Start starts the limiter with a psutil.Process.
	Start(process psutil.Process) error

	// Stop stops the limiter. The limiter can be reused by calling Start() again
	Stop()

	// Current returns the current CPU and memory values
	Current() (cpu float64, memory uint64)

	// Limits returns the defined CPU and memory limits. Values < 0 means no limit
	Limits() (cpu float64, memory uint64)
}

func NewLimiter

func NewLimiter(config LimiterConfig) Limiter

NewLimiter returns a new Limiter

type LimiterConfig

type LimiterConfig struct {
	CPU     float64       // Max. CPU usage in percent
	Memory  uint64        // Max. memory usage in bytes
	WaitFor time.Duration // Duration one of the limits has to be above the limit until OnLimit gets triggered
	OnLimit LimitFunc     // Function to be triggered if limits are exceeded
}

type Line

type Line struct {
	Timestamp time.Time
	Data      string
}

Line represents a line from the output with its timestamp. The line doesn't include any newline character.

type Parser

type Parser interface {
	// Parse parses the given line and returns an indicator
	// for progress (e.g. based on the contents of the line,
	// or previous line, ...)
	Parse(line string) uint64

	// Reset resets any collected statistics or temporary data.
	// This is called before the process starts and after the
	// process stopped. The stats are meant to be collected
	// during the runtime of the process.
	ResetStats()

	// ResetLog resets any collected logs. This is called
	// before the process starts.
	ResetLog()

	// Log returns a slice of collected log lines
	Log() []Line
}

Parser is an interface that is accepted by a process for parsing the process' output.

func NewNullParser

func NewNullParser() Parser

NewNullParser returns a dummy parser that isn't doing anything and always returns progress.

type Process

type Process interface {
	// Status returns the current status of this process
	Status() Status

	// Start starts the process. If the process stops by itself
	// it will restart automatically if it is defined to do so.
	Start() error

	// Stop stops the process and will not let it restart
	// automatically.
	Stop() error

	// Kill stops the process such that it will restart
	// automatically if it is defined to do so.
	Kill() error

	// IsRunning returns whether the process is currently
	// running or not.
	IsRunning() bool
}

Process represents a process and ways to control it and to extract information.

func New

func New(config Config) (Process, error)

New creates a new process wrapper

type States

type States struct {
	Finished  uint64
	Starting  uint64
	Running   uint64
	Finishing uint64
	Failed    uint64
	Killed    uint64
}

type Status

type Status struct {
	// State is the current state of the process. See stateType for the known states.
	State string

	// States is the cumulative history of states the process had.
	States States

	// Order is the wanted condition of process, either "start" or "stop"
	Order string

	// Duration is the time since the last change of the state
	Duration time.Duration

	// Time is the time of the last change of the state
	Time time.Time

	// Used CPU in percent
	CPU float64

	// Used memory in bytes
	Memory uint64
}

Status represents the current status of a process

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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