pm

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2019 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StandardStreamBufferSize = 100 //buffer size for each of stdout and stderr
	GenericStreamBufferSize  = 10  //we only keep last 100 message of all types.
)
View Source
const (
	AggreagteAverage    = "A"
	AggreagteDifference = "D"
)
View Source
const (
	//CommandSystem is the first and built in `core.system` command
	CommandSystem = "core.system"
)

Variables

View Source
var (
	MaxJobs           int
	UnknownCommandErr = errors.New("unkonw command")
	DuplicateIDErr    = errors.New("duplicate job id")
)

Functions

func AddHandle added in v1.2.0

func AddHandle(handler Handler)

AddHandle add handler to various process events

func Aggregate added in v1.2.0

func Aggregate(op, key string, value float64, id string, tags ...Tag)

func BadRequestError added in v1.2.0

func BadRequestError(cause interface{}) error

func Error added in v1.2.0

func Error(code uint32, cause interface{}) error

func Internal added in v1.5.0

func Internal(cmd string, args M, out interface{}) error

Internal run builtin command by name

func InternalError added in v1.2.0

func InternalError(cause interface{}) error

func Jobs added in v1.2.0

func Jobs() map[string]Job

Processes returs a list of running processes

func Kill added in v1.2.0

func Kill(cmdID string) error

Kill kills a r by the cmd ID

func Killall added in v1.2.0

func Killall()

Killall kills all running processes.

func MustArguments added in v1.2.0

func MustArguments(args interface{}) *json.RawMessage

MustArguments serialize an object to *json.RawMessage

func New added in v1.2.0

func New()

New initialize singleton process manager

func NotAcceptableError added in v1.2.0

func NotAcceptableError(cause interface{}) error

func NotFoundError added in v1.2.0

func NotFoundError(cause interface{}) error

func PreconditionFailedError added in v1.2.0

func PreconditionFailedError(cause interface{}) error

func Register added in v1.2.0

func Register(name string, factory ProcessFactory)

Register registers a command process factory

func RegisterBuiltIn added in v1.2.0

func RegisterBuiltIn(name string, runnable Runnable)

RegisterBuiltIn registers a built in function

func RegisterBuiltInWithCtx added in v1.2.0

func RegisterBuiltInWithCtx(name string, runnable RunnableWithCtx)

RegisterBuiltInWithCtx registers a built in function that accepts a command and a context

func RegisterExtension added in v1.2.0

func RegisterExtension(cmd string, exe string, workdir string, cmdargs []string, env map[string]string) error

RegisterExtension registers a new command (extension) so it can be executed via commands

func RunSlice added in v1.2.0

func RunSlice(slice settings.StartupSlice)

RunSlice runs a slice of processes honoring dependencies. It won't just start in order, but will also make sure a service won't start until it's dependencies are running.

func ServiceUnavailableError added in v1.2.0

func ServiceUnavailableError(cause interface{}) error

func SetUnprivileged added in v1.2.0

func SetUnprivileged()

SetUnprivileged switch to unprivileged mode (no way back) all process that runs after calling this will has some of their capabilities dropped

func Shutdown added in v1.5.0

func Shutdown(except ...string)

Shutdown kills all running processes.

func Start added in v1.2.0

func Start()

Start starts the process manager.

Types

type BackOff added in v1.5.0

type BackOff struct {
	Delay    time.Duration
	Multiply float64
	Max      time.Duration
	// contains filtered or unexported fields
}

BackOff a back off strategy

func (*BackOff) Duration added in v1.5.0

func (b *BackOff) Duration() (duration time.Duration)

Duration get the next backoff duration according to policy

type Channel added in v1.2.0

type Channel interface {
	io.ReadWriteCloser
}

Channel is a 2 way communication channel that is mainly used to talk to the main containerd process `coreX`

type Command added in v1.2.0

type Command struct {
	//Unique ID of the command, sets the job id
	ID string `json:"id"`
	//Command is the command name
	Command string `json:"command"`
	//Arguments, handled by the process
	Arguments *json.RawMessage `json:"arguments"`
	//Queue if set, commands with same queue are run synchronusly
	Queue string `json:"queue"`
	//StatsInterval fine tune when process statistics should be collected
	StatsInterval int `json:"stats_interval,omitempty"`
	//MaxTime max running time of the process, or it will get terminated
	MaxTime int `json:"max_time,omitempty"`
	//MaxRestart how many times the process manager should restart this process, if it failes
	MaxRestart int `json:"max_restart,omitempty"`
	//RecurringPeriod for recurring commands, defines how long it should wait between each run
	RecurringPeriod int `json:"recurring_period,omitempty"`
	//Stream if set to true, real time output of the process will get streamed over the output
	//channel
	Stream bool `json:"stream"`
	//LogLevels sets which log levels are to be logged
	LogLevels []int `json:"log_levels,omitempty"`
	//Tags custom user tags to be attached to the job
	Tags Tags `json:"tags"`

	//For internal use only, flags that can be set from inside the internal API
	Flags JobFlags `json:"-"`
}

Command is the main way to communicate witht he process manager A Command.command is matched against a list of know process factories that build the corresponding process to handle the rest of the command arguments.

func LoadCmd added in v1.2.0

func LoadCmd(str []byte) (*Command, error)

LoadCmd loads cmd from json string.

func (*Command) String added in v1.2.0

func (cmd *Command) String() string

String represents cmd as a string

type ContainerCommandArguments added in v1.2.0

type ContainerCommandArguments struct {
	Name        string            `json:"name"`
	Dir         string            `json:"dir"`
	Args        []string          `json:"args"`
	Env         map[string]string `json:"env"`
	HostNetwork bool              `json:"host_network"`
	Chroot      string            `json:"chroot"`
	Log         string            `json:"log"`
}

ContainerCommandArguments arguments for container command

func (*ContainerCommandArguments) String added in v1.2.0

func (c *ContainerCommandArguments) String() string

type ContainerProcess added in v1.2.0

type ContainerProcess interface {
	Process
	Channel() Channel
}

ContainerProcess interface

type Context added in v1.2.0

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

func (*Context) Log added in v1.2.0

func (c *Context) Log(text string, level ...uint16)

func (*Context) Message added in v1.2.0

func (c *Context) Message(msg *stream.Message)

type DelayHook

type DelayHook struct {
	NOOPHook

	Delay  time.Duration
	Action func()
	// contains filtered or unexported fields
}

DelayHook called after a certain amount of time passes from process start

func (*DelayHook) Tick

func (h *DelayHook) Tick(delay time.Duration)

type ExitHook

type ExitHook struct {
	NOOPHook

	Action func(bool)
	// contains filtered or unexported fields
}

ExitHook is called when the process exits

func (*ExitHook) Exit

func (h *ExitHook) Exit(state JobState)

type GetPID added in v1.2.0

type GetPID func() (int, error)

GetPID returns a PID of a process

type Handler added in v1.2.0

type Handler interface{}

Handler defines an interface to receiver the process manager events A handler can be any object that implements one or many handle methods below

type Job added in v1.2.0

type Job interface {
	Command() *Command
	Signal(sig syscall.Signal) error
	Process() Process
	Wait() *JobResult
	WaitContext(ctx context.Context) *JobResult
	StartTime() int64
	Subscribe(stream.MessageHandler)
	Unschedule()
}

func JobOf added in v1.2.0

func JobOf(id string) (Job, bool)

func Run added in v1.2.0

func Run(cmd *Command, hooks ...RunnerHook) (Job, error)

Run runs a command immediately (no pre-processors)

func RunFactory added in v1.2.0

func RunFactory(cmd *Command, factory ProcessFactory, hooks ...RunnerHook) (Job, error)

RunFactory run a command by creating a process by calling the factory with that command. accepts optional hooks to certain process events.

type JobFlags added in v1.2.0

type JobFlags struct {
	Protected bool
	NoOutput  bool
	NoSetPGID bool //set new process group id for job
}

JobFlags to control job behavior but only from the internal API Clients can't set the JobFlags, unlike the other public flags on the Command struct body.

type JobResult added in v1.2.0

type JobResult struct {
	ID        string   `json:"id"`
	Command   string   `json:"command"`
	Data      string   `json:"data"`
	Streams   Streams  `json:"streams,omitempty"`
	Critical  string   `json:"critical,omitempty"`
	Level     uint16   `json:"level"`
	State     JobState `json:"state"`
	Code      uint32   `json:"code"`
	StartTime int64    `json:"starttime"`
	Time      int64    `json:"time"`
	Tags      Tags     `json:"tags"`
	Container uint64   `json:"container"`
}

JobResult represents a result of a job

func NewJobResult added in v1.2.0

func NewJobResult(cmd *Command) *JobResult

NewJobResult creates a new job result from command

func System added in v1.2.0

func System(bin string, args ...string) (*JobResult, error)

System is a wrapper around core.system

type JobState added in v1.2.0

type JobState string

JobState of a job

const (
	//StateSuccess successs exit status
	StateSuccess JobState = "SUCCESS"
	//StateError error exist status
	StateError JobState = "ERROR"
	//StateTimeout timeout exit status
	StateTimeout JobState = "TIMEOUT"
	//StateKilled killed exit status
	StateKilled JobState = "KILLED"
	//StateUnknownCmd unknown cmd exit status
	StateUnknownCmd JobState = "UNKNOWN_CMD"
	//StateDuplicateID dublicate id exit status
	StateDuplicateID JobState = "DUPILICATE_ID"
)

type M added in v1.2.0

type M map[string]interface{}

M short hand for map[string]interface{}

type MatchHook

type MatchHook struct {
	NOOPHook
	Match  string
	Action func(msg *stream.Message)
	// contains filtered or unexported fields
}

MatchHook is called if a message matches a given pattern

func (*MatchHook) Message

func (h *MatchHook) Message(msg *stream.Message)

type MessageHandler

type MessageHandler interface {
	Message(*Command, *stream.Message)
}

MessageHandler gets called on the receive of each single message from all commands

type NOOPHook

type NOOPHook struct{}

NOOPHook empty handler

func (*NOOPHook) Exit

func (h *NOOPHook) Exit(state JobState)

func (*NOOPHook) Message

func (h *NOOPHook) Message(msg *stream.Message)

func (*NOOPHook) PID

func (h *NOOPHook) PID(pid int)

func (*NOOPHook) Tick

func (h *NOOPHook) Tick(delay time.Duration)

type PIDHook

type PIDHook struct {
	NOOPHook

	Action func(pid int)
	// contains filtered or unexported fields
}

PIDHook is called if a process got a PID

func (*PIDHook) PID

func (h *PIDHook) PID(pid int)

type PIDTable added in v1.2.0

type PIDTable interface {
	//PIDTable atomic registration of PID. MUST grantee that that no wait4 will happen
	//on any of the child process until the register operation is done.
	RegisterPID(g GetPID) (int, error)
	//WaitPID waits for a certain ID until it exits
	WaitPID(pid int) syscall.WaitStatus
}

PIDTable a table that keeps track of running process ids

type PIDer added in v1.5.0

type PIDer interface {
	Process
	GetPID() int32
}

PIDer a process that can return a PID

type PreHandler added in v1.2.0

type PreHandler interface {
	Pre(cmd *Command)
}

PreHandler is called with the commands before exectution

type Process added in v1.2.0

type Process interface {
	Command() *Command
	Run() (<-chan *stream.Message, error)
}

Process interface

func NewContainerProcess added in v1.2.0

func NewContainerProcess(table PIDTable, cmd *Command) Process

NewContainerProcess creates a new contained process, used soley from the container subsystem. Clients can't create container process directly they instead has to go throught he container subsystem which does most of the heavy lifting.

func NewSystemProcess added in v1.2.0

func NewSystemProcess(table PIDTable, cmd *Command) Process

type ProcessFactory added in v1.2.0

type ProcessFactory func(PIDTable, *Command) Process

ProcessFactory interface

func GetProcessFactory

func GetProcessFactory(cmd *Command) ProcessFactory

GetProcessFactory gets a process factory from command name

func NewInternalProcess added in v1.2.2

func NewInternalProcess(runnable Runnable) ProcessFactory

NewInternalProcess factory to build Runnable processes

func NewInternalProcessWithCtx added in v1.2.2

func NewInternalProcessWithCtx(runnable RunnableWithCtx) ProcessFactory

type ProcessStats added in v1.2.0

type ProcessStats struct {
	CPU  float64 `json:"cpu"`
	RSS  uint64  `json:"rss"`
	VMS  uint64  `json:"vms"`
	Swap uint64  `json:"swap"`
}

ProcessStats holds process cpu and memory usage

type Queue added in v1.2.0

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

* Queue is used for sequential cmds exectuions

func (*Queue) Channel added in v1.2.0

func (q *Queue) Channel() <-chan *jobImb

Channel return job channel

func (*Queue) Close added in v1.5.0

func (q *Queue) Close()

Close the queue. Queue can't be used after close

func (*Queue) Init added in v1.2.0

func (q *Queue) Init()

Init initializes the queue

func (*Queue) Notify added in v1.2.0

func (q *Queue) Notify(job Job)

Notify tell queue that a job execution has completed

func (*Queue) Push added in v1.2.0

func (q *Queue) Push(job *jobImb) error

Push a job on queue

type ResultHandler

type ResultHandler interface {
	Result(cmd *Command, result *JobResult)
}

ResultHandler receives the command result on exit

type RunError added in v1.2.0

type RunError interface {
	Code() uint32
	Cause() interface{}
}

type Runnable added in v1.2.0

type Runnable func(*Command) (interface{}, error)

Runnable represents a runnable built in function that can be managed by the process manager.

type RunnableWithCtx added in v1.2.0

type RunnableWithCtx func(*Context) (interface{}, error)

type RunnerHook

type RunnerHook interface {
	//Tick is called if certain amount has passed sicne process starting
	Tick(delay time.Duration)
	//Message is called on each received message from the process
	Message(msg *stream.Message)
	//Exit is called on process exit
	Exit(state JobState)
	//PID is executed once the child process is started and got an PID
	PID(pid int)
}

RunnerHook is a per process event handler

type Signaler added in v1.2.0

type Signaler interface {
	Process
	Signal(sig syscall.Signal) error
}

Signaler a process that supports signals

type Stater added in v1.2.0

type Stater interface {
	Process
	Stats() *ProcessStats
}

Stater a process that supports stats query

type StatsHandler added in v0.11.0

type StatsHandler interface {
	Stats(operation string, key string, value float64, id string, tags ...Tag)
}

StatsHandler receives parsed stats messages

type StreamHook added in v1.2.0

type StreamHook struct {
	NOOPHook
	Stdout bytes.Buffer
	Stderr bytes.Buffer
}

StreamHook captures full stdout and stderr of a process.

func (*StreamHook) Message added in v1.2.0

func (h *StreamHook) Message(msg *stream.Message)

type Streams added in v1.2.0

type Streams []string

Streams holds stdout and stderr of a job

func (Streams) Stderr added in v1.2.0

func (s Streams) Stderr() string

Stderr getter for stderr

func (Streams) Stdout added in v1.2.0

func (s Streams) Stdout() string

Stdout getter for stdout

type SystemCommandArguments added in v1.2.0

type SystemCommandArguments struct {
	Name  string            `json:"name"`
	Dir   string            `json:"dir"`
	Args  []string          `json:"args"`
	Env   map[string]string `json:"env"`
	StdIn string            `json:"stdin"`
}

func (*SystemCommandArguments) String added in v1.2.0

func (s *SystemCommandArguments) String() string

type Tag added in v1.2.0

type Tag struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

type Tags added in v1.2.0

type Tags []string

Tags defines a list of keyword tags

type TestingPIDTable added in v1.2.2

type TestingPIDTable struct{}

TestingPIDTable is used for testing to mock the process manager

func (*TestingPIDTable) RegisterPID added in v1.2.2

func (t *TestingPIDTable) RegisterPID(g GetPID) (int, error)

RegisterPID notify the process manager that a process has been started with the given PID

func (*TestingPIDTable) WaitPID added in v1.2.2

func (t *TestingPIDTable) WaitPID(pid int) syscall.WaitStatus

WaitPID waits for a PID until it exits

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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