Documentation ¶
Overview ¶
Package pm provides a process manager based on github.com/telnet2/pm/cmd. The `pm` manages the execution of multiple processes in the order of dependencies.
Index ¶
- func CtxConfDir(ctx context.Context) string
- func CtxLogDir(ctx context.Context) string
- func ExpandEnvs(r *Runnable)
- func GoFunc(fun func())
- func WithConfDir(ctx context.Context, confDir string) context.Context
- func WithLogDir(ctx context.Context, logDir string) context.Context
- type ConfigFile
- type DummyChecker
- type Duration
- type EventMap
- type HttpChecker
- type HttpReadyExpect
- type HttpReadySpec
- type LogChecker
- type MongoDbChecker
- type MongoDbReadySpec
- type MySqlChecker
- type MySqlReadySpec
- type ProcEvent
- type ProcEventPublisher
- func (p *ProcEventPublisher) Close()
- func (p *ProcEventPublisher) Evict(sub chan *ProcEvent)
- func (p *ProcEventPublisher) Len() int
- func (p *ProcEventPublisher) Publish(v *ProcEvent)
- func (p *ProcEventPublisher) Subscribe() chan *ProcEvent
- func (p *ProcEventPublisher) SubscribeTopic(topic _ProcEventTopicFunc) chan *ProcEvent
- func (p *ProcEventPublisher) SubscribeTopicWithBuffer(topic _ProcEventTopicFunc, buffer int) chan *ProcEvent
- type ProcMan
- func (pm *ProcMan) Add(run *Runnable) error
- func (pm *ProcMan) AddConfig(configFile *ConfigFile) error
- func (pm *ProcMan) IsAllDone() bool
- func (pm *ProcMan) Restart(ctx context.Context, id string) error
- func (pm *ProcMan) Shutdown(err error)
- func (pm *ProcMan) Start(ctx context.Context) error
- func (pm *ProcMan) Stop(id string) error
- func (pm *ProcMan) SubscribeEvent(id string, events map[string]struct{}) chan *ProcEvent
- func (pm *ProcMan) SubscribeLog(id string, stdout bool) chan string
- func (pm *ProcMan) UnsubscribeEvent(sub chan *ProcEvent)
- func (pm *ProcMan) WaitDone()
- type ReadyChecker
- type ReadySpec
- type Runnable
- type ShellChecker
- type ShellReadySpec
- type TcpChecker
- type TcpReadySpec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CtxConfDir ¶
Types ¶
type ConfigFile ¶
func LoadYamlFile ¶
func LoadYamlFile(ctx context.Context, f string) (*ConfigFile, error)
LoadYamlFile loads a config from a given yaml file.
func (*ConfigFile) String ¶
func (cf *ConfigFile) String() string
func (*ConfigFile) WriteFile ¶
func (cf *ConfigFile) WriteFile(yamlFile string) error
WriteFile writes *ConfigFile into a file
type DummyChecker ¶
type DummyChecker struct { }
DummyChecker do nothing
func (*DummyChecker) WaitForReady ¶
func (*DummyChecker) WaitForReady(context.Context, *Runnable) error
type Duration ¶
func (*Duration) UnmarshalJSON ¶
UnmarshalJSON helps to decode a string-type duration.
func (*Duration) UnmarshalYAML ¶
UnmarshalYAML helps to decode a string-type duration.
type HttpChecker ¶
type HttpChecker struct {
// contains filtered or unexported fields
}
func (*HttpChecker) Init ¶
func (rc *HttpChecker) Init(ctx context.Context, _ *Runnable) error
Init does nothing.
func (*HttpChecker) WaitForReady ¶
func (rc *HttpChecker) WaitForReady(ctx context.Context, _ *Runnable) error
WaitForReady makes an http call to the url and matches the response with the expectation.
type HttpReadyExpect ¶
type HttpReadySpec ¶
type HttpReadySpec struct { URL string `yaml:"url" json:"url,omitempty" validate:"required" envexp:""` Interval time.Duration `yaml:"interval,omitempty" json:"interval,omitempty" default:"3s"` // polling interval (default 3s) Expect *HttpReadyExpect `yaml:"expect" json:"expect,omitempty"` }
HttpReadySpec defines how to determine the readiness of the url. If `expect` field is not specified, 200 OK is considered to be ready.
type LogChecker ¶
type LogChecker struct {
// contains filtered or unexported fields
}
Implements ReadyChecker
func (*LogChecker) Init ¶
func (rc *LogChecker) Init(ctx context.Context, r *Runnable) error
Init is called before the runnable starts.
func (*LogChecker) WaitForReady ¶
func (rc *LogChecker) WaitForReady(ctx context.Context, _ *Runnable) error
WaitForReady waits until a given runnable is ready.
type MongoDbChecker ¶
type MongoDbChecker struct {
// contains filtered or unexported fields
}
func (*MongoDbChecker) Init ¶
func (rc *MongoDbChecker) Init(ctx context.Context, r *Runnable) error
Init does nothing.
func (*MongoDbChecker) WaitForReady ¶
func (rc *MongoDbChecker) WaitForReady(ctx context.Context, r *Runnable) error
WaitForReady makes an http call to the url and matches the response with the expectation.
type MongoDbReadySpec ¶
type MongoDbReadySpec struct { URI string `yaml:"uri" json:"uri" default:"mongo://mongo:mongo@localhost:27017/admin" envexp:""` Interval time.Duration `yaml:"interval,omitempty" json:"interval,omitempty" default:"3s"` // polling interval (default 3s) }
MongoDbReadySpec defines how to determine the readiness of a mysql connection.
type MySqlChecker ¶
type MySqlChecker struct {
// contains filtered or unexported fields
}
func (*MySqlChecker) Init ¶
func (rc *MySqlChecker) Init(ctx context.Context, r *Runnable) error
Init does nothing.
func (*MySqlChecker) WaitForReady ¶
func (rc *MySqlChecker) WaitForReady(ctx context.Context, r *Runnable) error
WaitForReady makes an http call to the url and matches the response with the expectation.
type MySqlReadySpec ¶
type MySqlReadySpec struct { Network string `yaml:"network,omitempty" json:"network,omitempty" default:"tcp"` Addr string `yaml:"addr" json:"addr" envexp:""` // Suppor TCP only Database string `yaml:"database" json:"database" envexp:""` User string `yaml:"user" json:"user" default:"root" envexp:""` Password string `yaml:"password,omitempty" json:"password,omitempty" envexp:""` Table string `yaml:"table,omitempty" json:"table,omitempty" envexp:""` Interval time.Duration `yaml:"interval,omitempty" json:"interval,omitempty" default:"3s"` // polling interval (default 3s) }
MySqlReadySpec defines how to determine the readiness of a mysql connection.
type ProcEventPublisher ¶
type ProcEventPublisher struct {
// contains filtered or unexported fields
}
ProcEventPublisher is basic pub/sub structure. Allows to send events and subscribe to them. Can be safely used from multiple goroutines.
func NewProcEventPublisher ¶
func NewProcEventPublisher(publishTimeout time.Duration, buffer int) *ProcEventPublisher
NewProcEventPublisher creates a new pub/sub publisher to broadcast messages. The duration is used as the send timeout as to not block the publisher publishing messages to other clients if one client is slow or unresponsive. The buffer is used when creating new channels for subscribers.
func (*ProcEventPublisher) Close ¶
func (p *ProcEventPublisher) Close()
Close closes the channels to all subscribers registered with the publisher.
func (*ProcEventPublisher) Evict ¶
func (p *ProcEventPublisher) Evict(sub chan *ProcEvent)
Evict removes the specified subscriber from receiving any more messages.
func (*ProcEventPublisher) Len ¶
func (p *ProcEventPublisher) Len() int
Len returns the number of subscribers for the publisher
func (*ProcEventPublisher) Publish ¶
func (p *ProcEventPublisher) Publish(v *ProcEvent)
Publish sends the data in v to all subscribers currently registered with the publisher.
func (*ProcEventPublisher) Subscribe ¶
func (p *ProcEventPublisher) Subscribe() chan *ProcEvent
Subscribe adds a new subscriber to the publisher returning the channel.
func (*ProcEventPublisher) SubscribeTopic ¶
func (p *ProcEventPublisher) SubscribeTopic(topic _ProcEventTopicFunc) chan *ProcEvent
SubscribeTopic adds a new subscriber that filters messages sent by a topic.
func (*ProcEventPublisher) SubscribeTopicWithBuffer ¶
func (p *ProcEventPublisher) SubscribeTopicWithBuffer(topic _ProcEventTopicFunc, buffer int) chan *ProcEvent
SubscribeTopicWithBuffer adds a new subscriber that filters messages sent by a topic. The returned channel has a buffer of the specified size.
type ProcMan ¶
type ProcMan struct { RunGraph map[string]*Runnable // RunGraph is the map from the id of a runnable to its contents // contains filtered or unexported fields }
ProcMan implements the process manager.
func NewProcMan ¶
func NewProcMan() *ProcMan
func (*ProcMan) AddConfig ¶
func (pm *ProcMan) AddConfig(configFile *ConfigFile) error
AddConfig adds services from a given config file or a struct.
func (*ProcMan) Restart ¶
Restart restarts the service. It is different from Start() in that it only restarts the runnable with a given id. Also, it starts only a given runnable not its dependents.
func (*ProcMan) Start ¶
Start executes all the runnables in the order of its dependencies and waits for its execution.
func (*ProcMan) SubscribeEvent ¶
SubscribeEvent subscribes to the events of the id. If the id is '*', it listens all the events.
func (*ProcMan) SubscribeLog ¶
SubscribeLog subscribes to the log publisher with an `id`. The service `id` must be already added to this ProcMan.
func (*ProcMan) UnsubscribeEvent ¶
UnsubscribeEvent
type ReadyChecker ¶
type ReadyChecker interface { Init(context.Context, *Runnable) error // BeforeStart is called before a given runnable starts. // WaitForReady is called after a given runnable starts. // It shouldn't return if the runnable is not ready. WaitForReady(context.Context, *Runnable) error }
ReadyChecker is reponsible for waiting a runnable is ready. We need to break the dependency to `Runnable`. Otherwise, a ReadyChecker must be created in this package due to circular dependency.
func NewReadyChecker ¶
func NewReadyChecker(id string, spec *ReadySpec) ReadyChecker
NewReadyChecker is a factory returning a concrete ReadyChecker
type ReadySpec ¶
type ReadySpec struct { StdoutLog string `yaml:"stdout_message,omitempty" json:"stdoutMessage,omitempty"` // log is a regex to find from stdout/stderr logs Http *HttpReadySpec `yaml:"http,omitempty" json:"http,omitempty"` // http defines the ready condition via http protocol MySql *MySqlReadySpec `yaml:"mysql,omitempty" json:"mysql,omitempty"` // http defines the ready condition via http protocol MongoDb *MongoDbReadySpec `yaml:"mongo_db,omitempty" json:"mongoDb,omitempty"` // http defines the ready condition via http protocol Shell *ShellReadySpec `yaml:"shell,omitempty" json:"shell,omitempty"` Tcp *TcpReadySpec `yaml:"tcp,omitempty" json:"tcp,omitempty"` Timeout Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" default:"1m"` // timeout (default 1min) }
ReadySpec provides a simple ready checker
type Runnable ¶
type Runnable struct { Id string `yaml:"id" json:"id,omitempty"` // Id must be unique within the ProcMan. Command string `yaml:"command" json:"command,omitempty" envexp:""` // Command to execute via bash CommandEx string `yaml:"command_ex,omitempty" json:"commandEx,omitempty"` // Command without env expansaion. CommandEx has priority. Env []string `yaml:"environment,omitempty" json:"environment,omitempty"` // Env is a list of env vars in the form of NAME=VALUE EnvFile string `yaml:"env_file,omitempty" json:"envFile,omitempty" envexp:""` // EnvFile to load env vars WorkDir string `yaml:"work_dir,omitempty" json:"workDir,omitempty" envexp:""` // Working directory LogDir string `yaml:"log_dir,omitempty" json:"logDir,omitempty" envexp:""` // If LogDir is set, then the ${LogDir}/${id}.stdout.log and ${LogDir}/${id}.stderr.log files are created and stored. DependsOn []string `yaml:"depends_on,omitempty" json:"dependsOn,omitempty"` // DependsOn is a list of runnables to run before this ReadyCondition *ReadySpec `yaml:"ready_condition,omitempty" json:"readyCheck,omitempty"` // ReadyCondition is the condition to determine if the process is ready Cmd *cmd.Cmd `yaml:"-" json:"-"` // Cmd is go's process execution object Ready ReadyChecker `yaml:"-" json:"-"` ConfDir string `yaml:"-" json:"-"` // the origin of this file // contains filtered or unexported fields }
Runnable is a configuration of an executable.
func (*Runnable) GetCommand ¶
GetCommand returns its command to run either from CommandRaw or Command.
func (*Runnable) GetRuntimeEnvs ¶
GetRuntimeEnvs returns env vars of this runnable.
type ShellChecker ¶
type ShellChecker struct {
// contains filtered or unexported fields
}
ShellChecker is a a shell Script based ready checker. A shell command is executed periodically until it returns 0 exit code.
func (*ShellChecker) Init ¶
func (sc *ShellChecker) Init(ctx context.Context, r *Runnable) error
Init is called before the runnable starts.
func (*ShellChecker) WaitForReady ¶
func (rc *ShellChecker) WaitForReady(ctx context.Context, _ *Runnable) error
WaitForReady waits until a given runnable is ready.
type ShellReadySpec ¶
type TcpChecker ¶
type TcpChecker struct {
// contains filtered or unexported fields
}
func (*TcpChecker) Init ¶
func (c *TcpChecker) Init(ctx context.Context, r *Runnable) error
Init is called before the runnable starts.
func (*TcpChecker) RetryCount ¶
func (c *TcpChecker) RetryCount() int
func (*TcpChecker) WaitForReady ¶
func (c *TcpChecker) WaitForReady(ctx context.Context, _ *Runnable) error
WaitForReady waits until the port is open.
type TcpReadySpec ¶
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package cmd runs external commands with concurrent access to output and status.
|
Package cmd runs external commands with concurrent access to output and status. |
Package ps contains utility functions related to OS processes.
|
Package ps contains utility functions related to OS processes. |
Package shutdown provides convenient interface for working with os.Signal.
|
Package shutdown provides convenient interface for working with os.Signal. |