forked from gizo-network/gizo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.go
204 lines (181 loc) · 4.35 KB
/
chain.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package chain
import (
"strconv"
"sync"
"time"
"github.com/gizo-network/gizo/helpers"
"github.com/gizo-network/gizo/cache"
"github.com/kpango/glg"
"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"
)
//Chain - jobs executed one after the other
type Chain struct {
jobs []job.Request
bc *core.BlockChain
pq *queue.JobPriorityQueue
jc *cache.JobCache
result []job.Request
logger *glg.Glg
length int
status string
cancel chan struct{}
}
//NewChain returns chain
func NewChain(j []job.Request, bc *core.BlockChain, pq *queue.JobPriorityQueue, jc *cache.JobCache) (*Chain, error) {
length := 0
for _, jr := range j {
length += len(jr.GetExec())
}
if length > job.MaxExecs {
return nil, job.ErrJobsLenRange
}
c := &Chain{
jobs: j,
bc: bc,
pq: pq,
jc: jc,
length: length,
cancel: make(chan struct{}),
logger: helpers.Logger(),
}
return c, nil
}
//Cancel terminates exec
func (c *Chain) Cancel() {
c.cancel <- struct{}{}
}
//GetCancelChan returns cancel channel
func (c Chain) GetCancelChan() chan struct{} {
return c.cancel
}
//GetJobs returns jobs
func (c Chain) GetJobs() []job.Request {
return c.jobs
}
func (c *Chain) setJobs(j []job.Request) {
c.jobs = j
}
//GetStatus returns status
func (c Chain) GetStatus() string {
return c.status
}
func (c *Chain) setStatus(s string) {
c.status = s
}
func (c Chain) getLength() int {
return c.length
}
func (c *Chain) setBC(bc *core.BlockChain) {
c.bc = bc
}
func (c Chain) getPQ() *queue.JobPriorityQueue {
return c.pq
}
func (c Chain) getBC() *core.BlockChain {
return c.bc
}
func (c Chain) getJC() *cache.JobCache {
return c.jc
}
func (c *Chain) setResults(res []job.Request) {
c.result = res
}
//Result returns result
func (c Chain) Result() []job.Request {
return c.result
}
//Dispatch executes the chain
func (c *Chain) Dispatch() {
c.setStatus(job.RUNNING)
var results []qItem.Item // used to hold results
res := make(chan qItem.Item)
cancelled := false
closeCancel := make(chan struct{})
var wg sync.WaitGroup
//! watch cancel channel
wg.Add(1)
go func() {
select {
case <-c.cancel:
cancelled = true
for _, jr := range c.GetJobs() {
for _, exec := range jr.GetExec() {
if exec.GetStatus() == job.RUNNING || exec.GetStatus() == job.RETRYING {
exec.Cancel()
}
if exec.GetResult() == nil {
exec.SetStatus(job.CANCELLED)
}
}
}
break
case <-closeCancel:
break
}
wg.Done()
}()
var jobIDs []string
for _, jr := range c.GetJobs() {
c.setStatus("Queueing execs of job - " + jr.GetID())
jobIDs = append(jobIDs, jr.GetID())
var j *job.Job
var err error
j, err = c.getJC().Get(jr.GetID())
if j == nil {
j, err = c.getBC().FindJob(jr.GetID())
}
if err != nil {
for _, exec := range jr.GetExec() {
exec.SetErr("Unable to find job - " + jr.GetID())
}
} else {
for i := 0; i < len(jr.GetExec()); i++ {
task, err := j.GetTask()
if err != nil {
jr.GetExec()[i].SetErr(err)
}
if cancelled == true {
results = append(results, qItem.NewItem(job.Job{
ID: j.GetID(),
Hash: j.GetHash(),
Name: j.GetName(),
Task: task,
Signature: j.GetSignature(),
SubmissionTime: j.GetSubmissionTime(),
Private: j.GetPrivate(),
}, jr.GetExec()[i], res, c.GetCancelChan()))
} else {
if jr.GetExec()[i].GetExecutionTime() != 0 {
c.logger.Info("Chain: Queuing in " + strconv.FormatFloat(time.Unix(jr.GetExec()[i].GetExecutionTime(), 0).Sub(time.Now()).Seconds(), 'f', -1, 64) + " nanoseconds")
time.Sleep(time.Nanosecond * time.Duration(time.Unix(jr.GetExec()[i].GetExecutionTime(), 0).Sub(time.Now()).Nanoseconds()))
}
c.getPQ().Push(*j, jr.GetExec()[i], res, c.GetCancelChan()) //? queues first job
results = append(results, <-res)
}
}
}
}
close(res)
var grouped []job.Request
for _, jID := range jobIDs {
var req job.Request
req.SetID(jID)
for _, item := range results {
if item.GetID() == jID {
req.AppendExec(item.GetExec())
}
}
grouped = append(grouped, req)
}
if cancelled == false {
closeCancel <- struct{}{}
c.setStatus(job.FINISHED)
} else {
c.setStatus(job.CANCELLED)
}
wg.Wait()
c.setResults(grouped)
}