agent

package
v0.0.0-...-a204096 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2016 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultCmdTimeout specifies the duration after which agent sends
	// an IdleTimeout signal if a task's command does not run to completion.
	DefaultCmdTimeout = 2 * time.Hour

	// DefaultExecTimeoutSecs specifies in seconds the maximum time a task is allowed to run
	// for, even if it is not idle. This default is used if exec_timeout_secs is not specified
	// in the project file.
	DefaultExecTimeoutSecs = 60 * 60 * 6 // six hours

	// DefaultIdleTimeout specifies the duration after which agent sends an
	// IdleTimeout signal if a task produces no logs.
	DefaultIdleTimeout = 20 * time.Minute
	// DefaultCallbackCmdTimeout specifies the duration after when the "post" or
	// "timeout" command sets should be shut down.
	DefaultCallbackCmdTimeout = 15 * time.Minute
	// DefaultHeartbeatInterval is interval after which agent sends a heartbeat
	// to API server.
	DefaultHeartbeatInterval = 30 * time.Second
	// DefaultStatsInterval is the interval after which agent sends system stats
	// to API server
	DefaultStatsInterval = time.Minute
)
View Source
const FilenameTimestamp = "2006-01-02_15_04_05"

Variables

View Source
var (
	// InitialSetupTimeout indicates the time allowed for the agent to collect
	// relevant information - for running a task - from the API server.
	InitialSetupTimeout = 20 * time.Minute
	// InitialSetupCommand is a placeholder command for the period during which
	// the agent requests information for running a task
	InitialSetupCommand = model.PluginCommandConf{
		DisplayName: "initial task setup",
		Type:        model.SystemCommandType,
	}
)
View Source
var InterruptedCmdError = errors.New("Command interrupted")

InterruptedCmdError is returned by commands that were stopped before they could complete.

Functions

func DumpStackOnSIGQUIT

func DumpStackOnSIGQUIT(curAgent **Agent)

DumpStackOnSIGQUIT listens for a SIGQUIT signal and writes stack dump to the given io.Writer when one is received. Blocks, so spawn it as a goroutine.

func ExitAgent

func ExitAgent(logger *comm.StreamLogger, exitCode int, pidFile string)

ExitAgent removes the pid file and exits the process with the given exit code.

Types

type Agent

type Agent struct {

	// TaskCommunicator handles all communication with the API server -
	// marking task started/ended, sending test results, logs, heartbeats, etc
	comm.TaskCommunicator

	// ExecTracker keeps track of the agent's current stage of execution.
	ExecTracker

	// KillChan is a channel which once closed, causes any in-progress commands to abort.
	KillChan chan bool

	// APILogger is a slogger.Appender which sends log messages
	// to the API server.
	APILogger *comm.APILogger

	// Registry manages plugins available for the agent.
	Registry plugin.Registry
	// contains filtered or unexported fields
}

Agent controls the various components and background processes needed throughout the lifetime of the execution of the task.

func New

func New(apiServerURL, taskId, taskSecret, logFile, cert, pidFilePath string) (*Agent, error)

New creates a new agent to run a given task.

func (*Agent) CheckIn

func (agt *Agent) CheckIn(command model.PluginCommandConf, duration time.Duration)

CheckIn updates the agent's execution stage and current timeout duration, and resets its timer back to zero.

func (*Agent) CreatePidFile

func (agt *Agent) CreatePidFile(pidFilePath string) error

CreatePidFile checks that the pid file does not already exist with a different pid and creates one

func (*Agent) GetCurrentCommand

func (agt *Agent) GetCurrentCommand() model.PluginCommandConf

GetCurrentCommand returns the current command being executed by the agent.

func (*Agent) GetTaskConfig

func (agt *Agent) GetTaskConfig() (*model.TaskConfig, error)

GetTaskConfig fetches task configuration data required to run the task from the API server.

func (*Agent) RunCommands

func (agt *Agent) RunCommands(commands []model.PluginCommandConf, returnOnError bool, stop chan bool) error

RunCommands takes a slice of commands and executes then sequentially. If returnOnError is set, it returns immediately if one of the commands fails. All plugins listen on the stop channel and must terminate immediately when a value is received.

func (*Agent) RunTask

func (agt *Agent) RunTask() (*apimodels.TaskEndResponse, error)

RunTask manages the process of running a task. It returns a response indicating the end result of the task.

func (*Agent) RunTaskCommands

func (agt *Agent) RunTaskCommands() (*apimodels.TaskEndResponse, error)

RunTaskCommands runs all commands for the task currently assigend to the agent.

func (*Agent) StartBackgroundActions

func (agt *Agent) StartBackgroundActions(signalHandler TerminateHandler)

StartBackgroundActions spawns goroutines that monitor various parts of the execution - heartbeats, timeouts, logging, etc.

type AgentCommand

type AgentCommand struct {
	*comm.StreamLogger
	ScriptLine string
	Expansions *command.Expansions
	KillChan   chan bool
}

AgentCommand encapsulates a running local command and streams logs back to the API server.

func (*AgentCommand) Run

func (ac *AgentCommand) Run(workingDir string) error

Run will execute the command in workingDir, by applying the expansions to the script and then invoking it with sh -c, and logging all of the command's stdout/stderr using the Logger. It will block until the command either finishes, or is aborted prematurely via the kill channel.

type ExecTracker

type ExecTracker interface {
	// Returns the current command being executed.
	CurrentCommand() *model.PluginCommandConf
	// Sets the current command being executed as well as a timeout for the command.
	CheckIn(command model.PluginCommandConf, timeout time.Duration)
}

ExecTracker exposes functions to update and get the current execution stage of the agent.

type SignalHandler

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

SignalHandler is an implementation of TerminateHandler which runs the post-run script when a task finishes, and reports its results back to the API server.

func (*SignalHandler) HandleSignals

func (sh *SignalHandler) HandleSignals(agt *Agent)

HandleSignals listens on its signal channel and properly handles any signal received.

type StatsCollector

type StatsCollector struct {
	Cmds []string
	// indicates the sampling frequency
	Interval time.Duration
	// contains filtered or unexported fields
}

StatsCollector samples machine statistics and logs them back to the API server at regular intervals.

func NewSimpleStatsCollector

func NewSimpleStatsCollector(logger *slogger.Logger, interval time.Duration,
	stop <-chan struct{}, cmds ...string) *StatsCollector

NewSimpleStatsCollector creates a StatsCollector that runs the given commands at the given interval and sends the results to the given logger.

func (*StatsCollector) LogStats

func (sc *StatsCollector) LogStats(exp *command.Expansions)

type TerminateHandler

type TerminateHandler interface {
	HandleSignals(*Agent)
}

TerminateHandler is an interface which defines how the agent should respond to signals resulting in the end of the task (heartbeat fail, timeout, etc)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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