-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
81 lines (67 loc) · 1.4 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
package maa
type Job struct {
id int64
finalStatus Status
statusFunc func(id int64) Status
waitFunc func(id int64) Status
}
func NewJob(id int64, statusFunc func(id int64) Status, waitFunc func(id int64) Status) *Job {
return &Job{
id: id,
statusFunc: statusFunc,
waitFunc: waitFunc,
}
}
func (j *Job) Status() Status {
if j.finalStatus.Invalid() {
return j.statusFunc(j.id)
}
return j.finalStatus
}
func (j *Job) Invalid() bool {
return j.Status().Invalid()
}
func (j *Job) Pending() bool {
return j.Status().Pending()
}
func (j *Job) Running() bool {
return j.Status().Running()
}
func (j *Job) Success() bool {
return j.Status().Success()
}
func (j *Job) Failure() bool {
return j.Status().Failure()
}
func (j *Job) Done() bool {
return j.Status().Done()
}
func (j *Job) Wait() *Job {
if j.finalStatus.Invalid() {
j.finalStatus = j.waitFunc(j.id)
}
return j
}
type TaskJob struct {
*Job
getTaskDetailFunc func(id int64) *TaskDetail
}
func NewTaskJob(
id int64,
statusFunc func(id int64) Status,
waitFunc func(id int64) Status,
getTaskDetailFunc func(id int64) *TaskDetail,
) *TaskJob {
job := NewJob(id, statusFunc, waitFunc)
return &TaskJob{
Job: job,
getTaskDetailFunc: getTaskDetailFunc,
}
}
func (j *TaskJob) Wait() *TaskJob {
j.Job.Wait()
return j
}
func (j *TaskJob) GetDetail() *TaskDetail {
return j.getTaskDetailFunc(j.id)
}