forked from gizo-network/gizo
/
solo.go
155 lines (138 loc) · 3.5 KB
/
solo.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package solo
import (
"strconv"
"sync"
"time"
"github.com/gizo-network/gizo/cache"
"github.com/gizo-network/gizo/core"
"github.com/gizo-network/gizo/job"
"github.com/gizo-network/gizo/job/queue"
"github.com/gizo-network/gizo/job/queue/qItem"
"github.com/kpango/glg"
)
//Solo - single job execution
type Solo struct {
jr job.Request
bc *core.BlockChain
pq *queue.JobPriorityQueue
jc *cache.JobCache
result job.Request
status string
cancel chan struct{}
}
//NewSolo initializes solo
func NewSolo(jr job.Request, bc *core.BlockChain, pq *queue.JobPriorityQueue, jc *cache.JobCache) *Solo {
return &Solo{
jr: jr,
bc: bc,
pq: pq,
jc: jc,
}
}
//Cancel cancels exec
func (s *Solo) Cancel() {
s.cancel <- struct{}{}
}
//GetCancelChan return cancel channel
func (s Solo) GetCancelChan() chan struct{} {
return s.cancel
}
//GetJob returns job request
func (s Solo) GetJob() job.Request {
return s.jr
}
//GetStatus returns status
func (s Solo) GetStatus() string {
return s.status
}
func (s *Solo) setStatus(status string) {
s.status = status
}
func (s *Solo) setBC(bc *core.BlockChain) {
s.bc = bc
}
func (s Solo) getPQ() *queue.JobPriorityQueue {
return s.pq
}
func (s Solo) getJC() *cache.JobCache {
return s.jc
}
func (s Solo) getBC() *core.BlockChain {
return s.bc
}
func (s *Solo) setResult(res job.Request) {
s.result = res
}
//Result returns result
func (s Solo) Result() job.Request {
return s.result
}
//Dispatch executes the solo
func (s *Solo) Dispatch() {
s.setStatus(job.RUNNING)
var result qItem.Item
res := make(chan qItem.Item)
cancelled := false
closeCancel := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
go func() {
select {
case <-s.cancel:
cancelled = true
glg.Warn("Solo: Cancelling job")
if s.GetJob().GetExec()[0].GetStatus() == job.RUNNING || s.GetJob().GetExec()[0].GetStatus() == job.RETRYING {
s.GetJob().GetExec()[0].Cancel()
}
if s.GetJob().GetExec()[0].GetResult() == nil {
s.GetJob().GetExec()[0].SetStatus(job.CANCELLED)
}
break
case <-closeCancel:
break
}
wg.Done()
}()
s.setStatus("Queueing execs of job - " + s.GetJob().GetID())
var j *job.Job
var err error
j, err = s.getJC().Get(s.GetJob().GetID()) //check job cache first
if j == nil {
j, err = s.getBC().FindJob(s.GetJob().GetID())
}
if err != nil {
s.GetJob().GetExec()[0].SetErr("Batch: Unable to find job - " + s.GetJob().GetID())
} else {
if cancelled == true {
task, err := j.GetTask()
if err != nil {
j.GetExecs()[0].SetErr(err)
}
result = qItem.NewItem(job.Job{
ID: j.GetID(),
Hash: j.GetHash(),
Name: j.GetName(),
Task: task,
Signature: j.GetSignature(),
SubmissionTime: j.GetSubmissionTime(),
Private: j.GetPrivate(),
}, s.GetJob().GetExec()[0], res, s.GetCancelChan())
} else {
if s.GetJob().GetExec()[0].GetExecutionTime() != 0 {
glg.Warn("Chord: Queuing in " + strconv.FormatFloat(time.Unix(s.GetJob().GetExec()[0].GetExecutionTime(), 0).Sub(time.Now()).Seconds(), 'f', -1, 64) + " nanoseconds")
time.Sleep(time.Nanosecond * time.Duration(time.Unix(s.GetJob().GetExec()[0].GetExecutionTime(), 0).Sub(time.Now()).Nanoseconds()))
}
s.getPQ().Push(*j, s.GetJob().GetExec()[0], res, s.GetCancelChan())
result = <-res
}
}
close(res)
if cancelled == false {
closeCancel <- struct{}{}
s.setStatus(job.FINISHED)
} else {
s.setStatus(job.CANCELLED)
}
wg.Wait()
s.setResult(*job.NewRequest(result.GetID(), result.GetExec()))
}