forked from xuperchain/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_manager.go
302 lines (257 loc) · 7.24 KB
/
job_manager.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package jobs
import (
"fmt"
"path/filepath"
"strings"
compilers "github.com/hyperledger/burrow/deploy/compile"
"github.com/hyperledger/burrow/deploy/def"
"github.com/hyperledger/burrow/deploy/util"
log "github.com/sirupsen/logrus"
)
type trackJob struct {
payload def.Payload
job *def.Job
contractName string
compilerResp *compilers.Response
err error
done chan struct{}
}
func concurrentJobRunner(jobs chan *trackJob) {
for {
track, ok := <-jobs
if !ok {
break
}
(*track).compilerResp, (*track).err = compilers.Compile(track.contractName, false, nil)
close(track.done)
}
}
func DoJobs(do *def.Packages) error {
jobs := make(chan *trackJob, do.Jobs*2)
defer close(jobs)
return RunJobs(do, jobs)
}
func RunJobs(do *def.Packages, jobs chan *trackJob) error {
// Dial the chain if needed
err, needed := burrowConnectionNeeded(do)
if err != nil {
return err
}
if needed {
err = do.Dial()
if err != nil {
return err
}
}
// ADD DefaultAddr and DefaultSet to jobs array....
// These work in reverse order and the addendums to the
// the ordering from the loading process is lifo
if len(do.DefaultSets) >= 1 {
defaultSetJobs(do)
}
if do.Address != "" {
defaultAddrJob(do)
}
err = do.Validate()
if err != nil {
return fmt.Errorf("error validating Burrow deploy file at %s: %v", do.YAMLPath, err)
}
intermediateJobs := make([]*trackJob, 0, len(do.Package.Jobs))
for i := 0; i < do.Jobs; i++ {
go concurrentJobRunner(jobs)
}
for _, job := range do.Package.Jobs {
payload, err := job.Payload()
if err != nil {
return fmt.Errorf("could not get Job payload: %v", payload)
}
track := trackJob{job: job, payload: payload}
intermediateJobs = append(intermediateJobs, &track)
// Do compilation first
switch payload.(type) {
case *def.Build:
track.done = make(chan struct{})
track.contractName = job.Build.Contract
jobs <- &track
case *def.Deploy:
if filepath.Ext(job.Deploy.Contract) == ".sol" {
track.done = make(chan struct{})
track.contractName = job.Deploy.Contract
jobs <- &track
}
}
}
for _, m := range intermediateJobs {
job := m.job
err = util.PreProcessFields(m.payload, do)
if err != nil {
return err
}
// Revalidate with possible replacements
err = m.payload.Validate()
if err != nil {
return fmt.Errorf("error validating job %s after pre-processing variables: %v", job.Name, err)
}
if m.done != nil {
<-m.done
if m.err != nil {
return m.err
}
}
switch m.payload.(type) {
// Meta Job
case *def.Meta:
announce(job.Name, "Meta")
do.CurrentOutput = fmt.Sprintf("%s.output.json", job.Name)
job.Result, err = MetaJob(job.Meta, do, jobs)
// Governance
case *def.UpdateAccount:
announce(job.Name, "UpdateAccount")
job.Result, job.Variables, err = UpdateAccountJob(job.UpdateAccount, do)
// Util jobs
case *def.Account:
announce(job.Name, "Account")
job.Result, err = SetAccountJob(job.Account, do)
case *def.Set:
announce(job.Name, "Set")
job.Result, err = SetValJob(job.Set, do)
// Transaction jobs
case *def.Send:
announce(job.Name, "Sent")
job.Result, err = SendJob(job.Send, do)
case *def.RegisterName:
announce(job.Name, "RegisterName")
job.Result, err = RegisterNameJob(job.RegisterName, do)
case *def.Permission:
announce(job.Name, "Permission")
job.Result, err = PermissionJob(job.Permission, do)
// Contracts jobs
case *def.Deploy:
announce(job.Name, "Deploy")
job.Result, err = DeployJob(job.Deploy, do, m.compilerResp)
case *def.Call:
announce(job.Name, "Call")
job.Result, job.Variables, err = CallJob(job.Call, do)
case *def.Build:
announce(job.Name, "Build")
job.Result, err = BuildJob(job.Build, do, m.compilerResp)
// State jobs
case *def.RestoreState:
announce(job.Name, "RestoreState")
job.Result, err = RestoreStateJob(job.RestoreState, do)
case *def.DumpState:
announce(job.Name, "DumpState")
job.Result, err = DumpStateJob(job.DumpState, do)
// Test jobs
case *def.QueryAccount:
announce(job.Name, "QueryAccount")
job.Result, err = QueryAccountJob(job.QueryAccount, do)
case *def.QueryContract:
announce(job.Name, "QueryContract")
job.Result, job.Variables, err = QueryContractJob(job.QueryContract, do)
case *def.QueryName:
announce(job.Name, "QueryName")
job.Result, err = QueryNameJob(job.QueryName, do)
case *def.QueryVals:
announce(job.Name, "QueryVals")
job.Result, err = QueryValsJob(job.QueryVals, do)
case *def.Assert:
announce(job.Name, "Assert")
job.Result, err = AssertJob(job.Assert, do)
default:
log.Error("")
return fmt.Errorf("the Job specified in deploy.yaml and parsed as '%v' is not recognised as a valid job",
job)
}
if len(job.Variables) != 0 {
for _, theJob := range job.Variables {
log.WithField("=>", fmt.Sprintf("%s,%s", theJob.Name, theJob.Value)).Info("Job Vars")
}
}
if err != nil {
return err
}
}
postProcess(do)
return nil
}
func announce(job, typ string) {
log.Warn("*****Executing Job*****\n")
log.WithField("=>", job).Warn("Job Name")
log.WithField("=>", typ).Info("Type")
log.Warn("\n")
}
func defaultAddrJob(do *def.Packages) {
oldJobs := do.Package.Jobs
newJob := &def.Job{
Name: "defaultAddr",
Account: &def.Account{
Address: do.Address,
},
}
do.Package.Jobs = append([]*def.Job{newJob}, oldJobs...)
}
func defaultSetJobs(do *def.Packages) {
oldJobs := do.Package.Jobs
newJobs := []*def.Job{}
for _, setr := range do.DefaultSets {
blowdUp := strings.Split(setr, "=")
if blowdUp[0] != "" {
newJobs = append(newJobs, &def.Job{
Name: blowdUp[0],
Set: &def.Set{
Value: blowdUp[1],
},
})
}
}
do.Package.Jobs = append(newJobs, oldJobs...)
}
func postProcess(do *def.Packages) error {
// Formulate the results map
results := make(map[string]interface{})
for _, job := range do.Package.Jobs {
results[job.Name] = job.Result
}
// check do.YAMLPath and do.DefaultOutput
var yaml string
yamlName := strings.LastIndexByte(do.YAMLPath, '.')
if yamlName >= 0 {
yaml = do.YAMLPath[:yamlName]
} else {
return fmt.Errorf("invalid jobs file path (%s)", do.YAMLPath)
}
// if do.YAMLPath is not default and do.DefaultOutput is default, over-ride do.DefaultOutput
if yaml != "deploy" && do.DefaultOutput == "deploy.output.json" {
do.DefaultOutput = fmt.Sprintf("%s.output.json", yaml)
}
// if CurrentOutput set, we're in a meta job
if do.CurrentOutput != "" {
log.Warn(fmt.Sprintf("Writing meta output of [%s] to current directory", do.CurrentOutput))
return WriteJobResultJSON(results, do.CurrentOutput)
}
// Write the output
log.Warn(fmt.Sprintf("Writing [%s] to current directory", do.DefaultOutput))
return WriteJobResultJSON(results, do.DefaultOutput)
}
func burrowConnectionNeeded(do *def.Packages) (error, bool) {
// Dial the chain if needed
for _, job := range do.Package.Jobs {
payload, err := job.Payload()
if err != nil {
return fmt.Errorf("could not get Job payload: %v", payload), false
}
switch payload.(type) {
case *def.Meta:
// A meta jobs will call runJobs again, so it does not need a connection for itself
continue
case *def.Build:
continue
case *def.Set:
continue
default:
return nil, true
}
}
return nil, false
}