-
-
Notifications
You must be signed in to change notification settings - Fork 738
/
job.go
98 lines (84 loc) · 2.26 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package job
import (
"context"
"time"
)
// JobExec represents the implementation of a Job to be executed.
type JobExec interface {
Execute(ctx context.Context, progress *Progress)
}
type jobExecImpl struct {
fn func(ctx context.Context, progress *Progress)
}
func (j *jobExecImpl) Execute(ctx context.Context, progress *Progress) {
j.fn(ctx, progress)
}
// MakeJobExec returns a simple JobExec implementation using the provided
// function.
func MakeJobExec(fn func(ctx context.Context, progress *Progress)) JobExec {
return &jobExecImpl{
fn: fn,
}
}
// Status is the status of a Job
type Status string
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"
// StatusFailed means that the job failed.
StatusFailed Status = "FAILED"
)
// Job represents the status of a queued or running 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
outerCtx context.Context
exec JobExec
cancelFunc context.CancelFunc
}
// 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.
func (j *Job) TimeElapsed() time.Duration {
var end time.Time
if j.EndTime != nil {
end = time.Now()
} else {
end = *j.EndTime
}
return end.Sub(*j.StartTime)
}
func (j *Job) cancel() {
if j.Status == StatusReady {
j.Status = StatusCancelled
} else if j.Status == StatusRunning {
j.Status = StatusStopping
}
if j.cancelFunc != nil {
j.cancelFunc()
}
}
// IsCancelled returns true if cancel has been called on the context.
func IsCancelled(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}