Documentation
¶
Index ¶
- Constants
- func IsCancelled(ctx context.Context) bool
- type Job
- type JobExec
- type Manager
- func (m *Manager) Add(ctx context.Context, description string, e JobExec) int
- func (m *Manager) CancelAll()
- func (m *Manager) CancelJob(id int)
- func (m *Manager) GetJob(id int) *Job
- func (m *Manager) GetQueue() []Job
- func (m *Manager) Start(ctx context.Context, description string, e JobExec) int
- func (m *Manager) Stop()
- func (m *Manager) Subscribe(ctx context.Context) *ManagerSubscription
- type ManagerSubscription
- type Progress
- type Status
Constants ¶
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.
func (*Job) TimeElapsed ¶ added in v0.14.0
func (j *Job) TimeElapsed() time.Duration
TimeElapsed returns the total time elapsed for the job. If the EndTime is set, then it uses this to calculate the elapsed time, otherwise it uses time.Now.
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 (*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.
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) AddProcessed ¶ added in v0.11.0
func (p *Progress) AddProcessed(v int)
AddProcessed increments the number of processed work units by the provided amount. This is used to calculate the percentage.
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. This is used to calculate the percentage. If total is set already, then the number of processed work units will not exceed the total.
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.
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"
)