jobrunner

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidToken is returned when caller of the ProcessMessage function
	// does not pass a valid token written by Runner in its Token channel.
	ErrInvalidToken = errors.New("invalid token")

	// ErrCheckWithSameID is returned when the runner is about to run a check
	// with and ID equal to the ID of an already running check.
	ErrCheckWithSameID = errors.New("check with a same ID is already running")

	// DefaultMaxMessageProcessedTimes defines the maximun number of times the
	// processor tries to processe a checks message before it declares the check
	// as failed.
	DefaultMaxMessageProcessedTimes = 200
)

Functions

This section is empty.

Types

type AbortedChecks

type AbortedChecks interface {
	IsAborted(ID string) (bool, error)
}

AbortedChecks defines the shape of the component needed by a Runner in order to know if a check is aborted before it is exected.

type CheckStateUpdater

type CheckStateUpdater interface {
	UpdateState(stateupdater.CheckState) error
	UploadCheckData(checkID, kind string, startedAt time.Time, content []byte) (string, error)
	CheckStatusTerminal(ID string) bool
	FlushCheckStatus(ID string) error
	UpdateCheckStatusTerminal(stateupdater.CheckState)
}

type Job

type Job struct {
	CheckID      string            `json:"check_id"`      // Required
	StartTime    time.Time         `json:"start_time"`    // Required
	Image        string            `json:"image"`         // Required
	Target       string            `json:"target"`        // Required
	Timeout      int               `json:"timeout"`       // Required
	AssetType    string            `json:"assettype"`     // Optional
	Options      string            `json:"options"`       // Optional
	RequiredVars []string          `json:"required_vars"` // Optional
	Metadata     map[string]string `json:"metadata"`      // Optional
	RunTime      int64
}

Job stores the information necessary to create a new check job. This is the information written in the queue where the agents read the messages from.

type Runner

type Runner struct {
	Backend backend.Backend
	// Tokens contains the currently free tokens of a runner. Any
	// caller of the Run function must take a token from this channel before
	// actually calling "Run" in order to ensure there are no more than
	// maxTokens jobs running at the same time.
	Tokens       chan interface{}
	Logger       log.Logger
	CheckUpdater CheckStateUpdater
	// contains filtered or unexported fields
}

Runner runs the checks associated to a concreate message by receiving calls to it ProcessMessage function.

func New

func New(logger log.Logger, backend backend.Backend, checkUpdater CheckStateUpdater,
	aborted AbortedChecks, cfg RunnerConfig) *Runner

New creates a Runner initialized with the given log, backend and maximun number of tokens. The maximum number of tokens is the maximun number jobs that the Runner can execute at the same time.

func (*Runner) AbortAllChecks

func (cr *Runner) AbortAllChecks(ID string)

AbortAllChecks aborts all the checks that are running.

func (*Runner) AbortCheck

func (cr *Runner) AbortCheck(ID string)

AbortCheck aborts a check if it is running.

func (*Runner) ChecksRunning

func (cr *Runner) ChecksRunning() int

ChecksRunning returns the current number of checks running.

func (*Runner) FreeTokens

func (cr *Runner) FreeTokens() chan interface{}

FreeTokens returns a channel that can be used to get a free token to call the ProcessMessage method.

func (*Runner) ProcessMessage

func (cr *Runner) ProcessMessage(msg queue.Message, token interface{}) <-chan bool

ProcessMessage executes the job specified in a message given a free token that must be obtained from the Tokens channel. The func does not actually do anything with the token, the parameter is present just to make obvious that there must be free tokens on the channel before calling this method. When the message if processed the channel returned will indicate if the message must be deleted or not.

type RunnerConfig

type RunnerConfig struct {
	MaxTokens              int
	DefaultTimeout         int
	MaxProcessMessageTimes int
}

RunnerConfig contains config parameters for a Runner.

Jump to

Keyboard shortcuts

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