job

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const ProgressIndefinite float64 = -1

ProgressIndefinite is the special percent value to indicate that the percent progress is not known.

Variables

This section is empty.

Functions

func IsCancelled

func IsCancelled(ctx context.Context) bool

IsCancelled returns true if cancel has been called on the context.

Types

type Job

type Job struct {
	ID     int
	Status Status
	// details of the current operations of the job
	Details     []string
	Description string
	// Progress in terms of 0 - 1.
	Progress  float64
	StartTime *time.Time
	EndTime   *time.Time
	AddTime   time.Time
	// contains filtered or unexported fields
}

Job represents the status of a queued or running job.

type JobExec

type JobExec interface {
	Execute(ctx context.Context, progress *Progress)
}

JobExec represents the implementation of a Job to be executed.

func MakeJobExec

func MakeJobExec(fn func(ctx context.Context, progress *Progress)) JobExec

MakeJobExec returns a simple JobExec implementation using the provided function.

type Manager

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

Manager maintains a queue of jobs. Jobs are executed one at a time.

func NewManager

func NewManager() *Manager

NewManager initialises and returns a new Manager.

func (*Manager) Add

func (m *Manager) Add(ctx context.Context, description string, e JobExec) int

Add queues a job.

func (*Manager) CancelAll

func (m *Manager) CancelAll()

CancelAll cancels all of the jobs in the queue. This is the same as calling CancelJob on all jobs in the queue.

func (*Manager) CancelJob

func (m *Manager) CancelJob(id int)

CancelJob cancels the job with the provided id. Jobs that have been started are notified that they are stopping. Jobs that have not yet started are removed from the queue. If no job exists with the provided id, then there is no effect. Likewise, if the job is already cancelled, there is no effect.

func (*Manager) GetJob

func (m *Manager) GetJob(id int) *Job

GetJob returns a copy of the Job for the provided id. Returns nil if the job does not exist.

func (*Manager) GetQueue

func (m *Manager) GetQueue() []Job

GetQueue returns a copy of the current job queue.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, description string, e JobExec) int

Start adds a job and starts it immediately, concurrently with any other jobs.

func (*Manager) Stop

func (m *Manager) Stop()

Stop is used to stop the dispatcher thread. Once Stop is called, no more Jobs will be processed.

func (*Manager) Subscribe

func (m *Manager) Subscribe(ctx context.Context) *ManagerSubscription

Subscribe subscribes to changes to jobs in the manager queue.

type ManagerSubscription

type ManagerSubscription struct {
	// new jobs are sent to this channel
	NewJob <-chan Job
	// removed jobs are sent to this channel
	RemovedJob <-chan Job
	// updated jobs are sent to this channel
	UpdatedJob <-chan Job
	// contains filtered or unexported fields
}

ManagerSubscription is a collection of channels that will receive updates from the job manager.

type Progress

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

Progress is used by JobExec to communicate updates to the job's progress to the JobManager.

func (*Progress) ExecuteTask

func (p *Progress) ExecuteTask(description string, fn func())

ExecuteTask executes a task as part of a job. The description is used to populate the Details slice in the parent Job.

func (*Progress) Increment

func (p *Progress) Increment()

Increment increments the number of processed work units, if this does not exceed the total units. This is used to calculate the percentage.

func (*Progress) Indefinite

func (p *Progress) Indefinite()

Indefinite sets the progress to an indefinite amount.

func (*Progress) SetPercent

func (p *Progress) SetPercent(percent float64)

SetPercent sets the progress percent directly. This value will be overwritten if Indefinite, SetTotal, Increment or SetProcessed is called. Constrains the percent value between 0 and 1, inclusive.

func (*Progress) SetProcessed

func (p *Progress) SetProcessed(processed int)

SetProcessed sets the number of work units completed. This is used to calculate the progress percentage.

func (*Progress) SetTotal

func (p *Progress) SetTotal(total int)

SetTotal sets the total number of work units. This is used to calculate the progress percentage.

type Status

type Status string

Status is the status of a Job

const (
	// StatusReady means that the Job is not yet started.
	StatusReady Status = "READY"
	// StatusRunning means that the job is currently running.
	StatusRunning Status = "RUNNING"
	// StatusStopping means that the job is cancelled but is still running.
	StatusStopping Status = "STOPPING"
	// StatusFinished means that the job was completed.
	StatusFinished Status = "FINISHED"
	// StatusCancelled means that the job was cancelled and is now stopped.
	StatusCancelled Status = "CANCELLED"
)

Jump to

Keyboard shortcuts

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