job

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 10, 2018 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WorkerPools map[Type]*workerPool

WorkerPools is a map contains workerpools for different types of jobs.

Functions

func Dispatch

func Dispatch()

Dispatch will listen to the jobQueue of job service and try to pick a free worker from the worker pool and assign the job to it.

func GetJobLogPath

func GetJobLogPath(base string, jobID int64) string

GetJobLogPath returns the absolute path in which the job log file is located.

func InitWorkerPools

func InitWorkerPools() error

InitWorkerPools create worker pools for different types of jobs.

func NewLogger

func NewLogger(j Job) (*log.Logger, error)

NewLogger create a logger for a speicified job

func Reschedule

func Reschedule(j Job)

Reschedule is called by statemachine to retry a job

func Schedule

func Schedule(j Job)

Schedule put a job id into job queue.

Types

type ImgPuller

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

ImgPuller was for testing

func (ImgPuller) Enter

func (ip ImgPuller) Enter() (string, error)

Enter ...

func (ImgPuller) Exit

func (ip ImgPuller) Exit() error

Exit ...

type ImgPusher

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

ImgPusher is a statehandler for testing

func (ImgPusher) Enter

func (ip ImgPusher) Enter() (string, error)

Enter ...

func (ImgPusher) Exit

func (ip ImgPusher) Exit() error

Exit ...

type Job

type Job interface {
	//ID returns the id of the job
	ID() int64
	Type() Type
	LogPath() string
	UpdateStatus(status string) error
	GetStatus() (string, error)
	Init() error
}

Job is abstraction for image replication and image scan jobs.

type RepJob

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

RepJob implements Job interface, represents a replication job.

func NewRepJob

func NewRepJob(id int64) *RepJob

NewRepJob returns a pointer to RepJob which implements the Job interface. Given API only gets the id, it will call this func to get a instance that can be manuevered by state machine.

func (*RepJob) GetStatus

func (rj *RepJob) GetStatus() (string, error)

GetStatus returns the status of the job

func (*RepJob) ID

func (rj *RepJob) ID() int64

ID returns the ID of the replication job

func (*RepJob) Init

func (rj *RepJob) Init() error

Init prepares parm for the replication job

func (*RepJob) LogPath

func (rj *RepJob) LogPath() string

LogPath returns the absolute path of the particular replication job.

func (*RepJob) String

func (rj *RepJob) String() string

String ...

func (*RepJob) Type

func (rj *RepJob) Type() Type

Type returns the type of the replication job, it should always be ReplicationType

func (*RepJob) UpdateStatus

func (rj *RepJob) UpdateStatus(status string) error

UpdateStatus ...

type RepJobParm

type RepJobParm struct {
	LocalRegURL    string
	TargetURL      string
	TargetUsername string
	TargetPassword string
	Repository     string
	Tags           []string
	Operation      string
	Insecure       bool
}

RepJobParm wraps the parm of a replication job

type Retry

type Retry struct {
	Job Job
}

Retry handles a special "retrying" in which case it will update the status in DB and reschedule the job via scheduler

func (Retry) Enter

func (jr Retry) Enter() (string, error)

Enter ...

func (Retry) Exit

func (jr Retry) Exit() error

Exit ...

type SM

type SM struct {
	CurrentJob    Job
	CurrentState  string
	PreviousState string
	//The states that don't have to exist in transition map, such as "Error", "Canceled"
	ForcedStates map[string]struct{}
	Transitions  map[string]map[string]struct{}
	Handlers     map[string]StateHandler

	Logger *log.Logger
	// contains filtered or unexported fields
}

SM is the state machine to handle job, it handles one job at a time.

func (*SM) AddTransition

func (sm *SM) AddTransition(from string, to string, h StateHandler)

AddTransition add a transition to the transition table of state machine, the handler is the handler of target state "to"

func (*SM) EnterState

func (sm *SM) EnterState(s string) (string, error)

EnterState transit the statemachine from the current state to the state in parameter. It returns the next state the statemachine should tranit to.

func (*SM) Init

func (sm *SM) Init()

Init initialzie the state machine, it will be called once in the lifecycle of state machine.

func (*SM) RemoveTransition

func (sm *SM) RemoveTransition(from string, to string)

RemoveTransition removes a transition from transition table of the state machine

func (*SM) Reset

func (sm *SM) Reset(j Job) error

Reset resets the state machine and after prereq checking, it will start handling the job.

func (*SM) Start

func (sm *SM) Start(s string)

Start kicks off the statemachine to transit from current state to s, and moves on It will search the transit map if the next state is "_continue", and will enter error state if there's more than one possible path when next state is "_continue"

func (*SM) Stop

func (sm *SM) Stop(job Job)

Stop will set the desired state as "stopped" such that when next tranisition happen the state machine will stop handling the current job and the worker can release itself to the workerpool.

type ScanJob

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

ScanJob implements the Job interface, representing a job for scanning image.

func NewScanJob

func NewScanJob(id int64) *ScanJob

NewScanJob creates a instance of ScanJob by id.

func (*ScanJob) GetStatus

func (sj *ScanJob) GetStatus() (string, error)

GetStatus returns the status of the job

func (*ScanJob) ID

func (sj *ScanJob) ID() int64

ID returns the id of the scan

func (*ScanJob) Init

func (sj *ScanJob) Init() error

Init query the DB and populate the information of the image to scan in the parm of this job.

func (*ScanJob) LogPath

func (sj *ScanJob) LogPath() string

LogPath returns the absolute path of the log file for the job, log files for scan job will be put in a sub folder of base log path.

func (*ScanJob) String

func (sj *ScanJob) String() string

String ...

func (*ScanJob) Type

func (sj *ScanJob) Type() Type

Type always return ScanType

func (*ScanJob) UpdateStatus

func (sj *ScanJob) UpdateStatus(status string) error

UpdateStatus ...

type ScanJobParm

type ScanJobParm struct {
	Repository string
	Tag        string
	Digest     string
}

ScanJobParm wraps the parms of a image scan job.

type StateHandler

type StateHandler interface {
	// Enter returns the next state, if it returns empty string the SM will hold the current state or
	// or decide the next state.
	Enter() (string, error)
	//Exit should be idempotent
	Exit() error
}

StateHandler handles transition, it associates with each state, will be called when SM enters and exits a state during a transition.

type StatusUpdater

type StatusUpdater struct {
	Job    Job
	Status string
}

StatusUpdater implements the StateHandler interface which updates the status of a job in DB when the job enters a status.

func (StatusUpdater) Enter

func (su StatusUpdater) Enter() (string, error)

Enter updates the status of a job and returns "_continue" status to tell state machine to move on. If the status is a final status it returns empty string and the state machine will be stopped.

func (StatusUpdater) Exit

func (su StatusUpdater) Exit() error

Exit ...

type Type

type Type int

Type is for job Type

const (
	// ReplicationType is the Type to identify a replication job.
	ReplicationType Type = iota
	// ScanType is the Type to identify a image scanning job.
	ScanType
)

func (Type) String

func (t Type) String() string

type Worker

type Worker struct {
	ID   int
	Type Type
	Jobs chan Job

	SM *SM
	// contains filtered or unexported fields
}

Worker consists of a channel for job from which worker gets the next job to handle, and a pointer to a statemachine, the actual work to handle the job is done via state machine.

func NewWorker

func NewWorker(id int, t Type, wp *workerPool) *Worker

NewWorker returns a pointer to new instance of worker

func (*Worker) Start

func (w *Worker) Start()

Start is a loop worker gets id from its channel and handle it.

func (*Worker) Stop

func (w *Worker) Stop()

Stop ...

func (*Worker) String

func (w *Worker) String() string

String ...

Jump to

Keyboard shortcuts

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