-
Notifications
You must be signed in to change notification settings - Fork 4
/
execution.go
438 lines (401 loc) · 10.3 KB
/
execution.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package batch
import (
"bytes"
"context"
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
force "github.com/ForceCLI/force/lib"
"github.com/clbanning/mxj"
"github.com/octoberswimmer/batchforce/soql"
log "github.com/sirupsen/logrus"
)
type Execution struct {
Session *force.Force
JobOptions []JobOption
Object string
Apex string
Expr string
Converter Converter
Query string
QueryAll bool
CsvFile string
RecordSender RecordSender
DryRun bool
BatchSize int
}
// Arbitrary function that can send ForceRecord's.
// A RecordSender should close the records channel when it's done writing.
type RecordSender func(ctx context.Context, records chan<- force.ForceRecord) error
type Batch []force.ForceRecord
type Converter func(force.ForceRecord) []force.ForceRecord
type JobOption func(*force.JobInfo)
func NewExecution(object string, query string) *Execution {
exec, err := NewQueryExecution(object, query)
if err != nil {
log.Fatalf("query error: %s", err.Error())
}
return exec
}
func NewQueryExecution(object string, query string) (*Execution, error) {
if err := soql.Validate([]byte(query)); err != nil {
return nil, err
}
return &Execution{
Object: object,
Query: query,
BatchSize: 2000,
}, nil
}
func NewCSVExecution(object string, csv string) (*Execution, error) {
return &Execution{
Object: object,
CsvFile: csv,
BatchSize: 2000,
}, nil
}
func NewRecordSenderExecution(object string, sender RecordSender) (*Execution, error) {
return &Execution{
Object: object,
RecordSender: sender,
BatchSize: 2000,
}, nil
}
func (e *Execution) Execute() (force.JobInfo, error) {
return e.ExecuteContext(context.Background())
}
func (e *Execution) ExecuteContext(ctx context.Context) (force.JobInfo, error) {
var apexContext any
var err error
type BulkJobResult struct {
force.JobInfo
err error
}
var result BulkJobResult
if e.Apex != "" {
apexContext, err = e.getApexContext()
if err != nil {
return result.JobInfo, fmt.Errorf("Unable to get apex context: %w", err)
}
}
if e.Expr != "" {
e.Converter = exprConverter(e.Expr, apexContext)
} else if e.Converter == nil {
return result.JobInfo, fmt.Errorf("Expr or Converter must be defined")
}
converter := e.Converter
if e.Query != "" {
converter, err = makeFlatteningConverter(e.Query, converter)
if err != nil {
return result.JobInfo, fmt.Errorf("Unable to get make converter: %w", err)
}
}
session := e.session()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
queried := make(chan force.ForceRecord)
updates := make(chan force.ForceRecord)
jobResult := make(chan BulkJobResult)
// Start converter that takes input data, converts it to zero or more output
// records and sends them to the bulk job
go func() {
err = processRecords(ctx, queried, updates, converter)
if err != nil {
cancel()
log.Warn(err.Error())
}
}()
// Start job that sends records to the Bulk API
go func() {
result, err := e.update(ctx, updates)
cancel()
jobResult <- BulkJobResult{
JobInfo: result,
err: err,
}
}()
switch {
case e.Query != "":
var queryOptions []func(*force.QueryOptions)
if e.QueryAll {
queryOptions = append(queryOptions, func(options *force.QueryOptions) {
options.QueryAll = true
})
}
err = session.CancelableQueryAndSend(ctx, e.Query, queried, queryOptions...)
if err != nil {
cancel()
log.Errorf("Query failed: %s", err)
}
case e.CsvFile != "":
f, err := os.Open(e.CsvFile)
if err != nil {
cancel()
log.Errorf("failed to open file: %s", err)
}
defer f.Close()
err = RecordsFromCsv(ctx, f, queried)
if err != nil {
cancel()
log.Errorf("Failed to read file: %s", err)
}
case e.RecordSender != nil:
err = e.RecordSender(ctx, queried)
if err != nil {
cancel()
log.Errorf("RecordSender failed: %s", err)
}
default:
return result.JobInfo, fmt.Errorf("No input defined")
}
result = <-jobResult
return result.JobInfo, result.err
}
func (e *Execution) JobResults(job *force.JobInfo) (force.BatchResult, error) {
var results force.BatchResult
batches, err := e.Session.GetBatches(job.Id)
if err != nil {
return results, fmt.Errorf("Failed to retrieve result batches: %w", err)
}
for _, b := range batches {
result, err := e.Session.RetrieveBulkBatchResults(job.Id, b.Id)
if err != nil {
return force.BatchResult{}, fmt.Errorf("Failed to retrieve batch: %w", err)
}
results = append(results, result...)
}
return results, nil
}
func (e *Execution) RunContext(ctx context.Context) force.JobInfo {
job, err := e.ExecuteContext(ctx)
if err != nil {
log.Fatalln(err.Error())
}
return job
}
func (e *Execution) RunContextE(ctx context.Context) (force.JobInfo, error) {
job, err := e.ExecuteContext(ctx)
return job, err
}
func (e *Execution) Run() force.JobInfo {
return e.RunContext(context.Background())
}
func (e *Execution) startJob() (force.JobInfo, error) {
job := force.JobInfo{}
job.Operation = "update"
job.ContentType = "JSON"
job.Object = e.Object
for _, option := range e.JobOptions {
option(&job)
}
jobInfo, err := e.Session.CreateBulkJob(job)
if err != nil {
return jobInfo, fmt.Errorf("Failed to create bulk job: %w", err)
}
return jobInfo, nil
}
func (e *Execution) update(ctx context.Context, records <-chan force.ForceRecord) (force.JobInfo, error) {
defer func() {
for range records {
// drain records
}
}()
var job force.JobInfo
var err error
batch := make(Batch, 0, e.BatchSize)
sendBatch := func() error {
updates, err := batch.marshallForBulkJob(job)
if err != nil {
return fmt.Errorf("Failed to serialize batch: %w", err)
}
log.Infof("Adding batch of %d records to job %s", len(batch), job.Id)
_, err = e.Session.AddBatchToJob(string(updates), job)
if err != nil {
return fmt.Errorf("Failed to enqueue batch: %w", err)
}
batch = make(Batch, 0, e.BatchSize)
return nil
}
RECORDS:
for {
select {
case <-ctx.Done():
log.Warn("Context canceled. Not sending more records to bulk job.")
break RECORDS
case record, ok := <-records:
if !ok {
break RECORDS
}
if e.DryRun {
j, err := json.Marshal(record)
if err != nil {
log.Warnf("Invalid update: %s", err.Error())
} else {
log.Info(string(j))
}
continue
}
if job.Id == "" {
job, err = e.startJob()
if err != nil {
return job, err
}
log.Infof("Created job %s", job.Id)
}
batch = append(batch, record)
if len(batch) == e.BatchSize {
err := sendBatch()
if err != nil {
log.Error(err.Error())
break RECORDS
}
}
case <-time.After(5 * time.Second):
log.Info("Waiting for record to add to batch")
}
}
if len(batch) > 0 {
err := sendBatch()
if err != nil {
log.Error(err.Error())
}
}
if job.Id == "" {
log.Info("Bulk job not started")
return job, nil
}
log.Info("Closing bulk job")
job, err = e.Session.CloseBulkJobWithContext(ctx, job.Id)
if err != nil {
return job, fmt.Errorf("Failed to close bulk job: %w", err)
}
for {
select {
case <-ctx.Done():
return job, fmt.Errorf("Cancelling wait for bulk job completion: %w", ctx.Err())
default:
}
job, err = e.Session.GetJobInfo(job.Id)
if err != nil {
return job, fmt.Errorf("Failed to get bulk job status: %w", err)
}
log.Info(fmt.Sprintf("Records Processed: %d | Records Failed: %d", job.NumberRecordsProcessed, job.NumberRecordsFailed))
log.Info(fmt.Sprintf("Batches In Progress: %d | Batches Complete: %d/%d", job.NumberBatchesInProgress, job.NumberBatchesCompleted, job.NumberBatchesTotal))
if job.State == "Aborted" || job.State == "Failed" {
return job, fmt.Errorf("Bulk Job %s", job.State)
}
if job.NumberBatchesCompleted+job.NumberBatchesFailed == job.NumberBatchesTotal {
break
}
time.Sleep(2000 * time.Millisecond)
}
return job, nil
}
func processRecords(ctx context.Context, input <-chan force.ForceRecord, output chan<- force.ForceRecord, converter Converter) (err error) {
defer func() {
close(output)
for range input {
// drain records
}
if r := recover(); r != nil {
err = fmt.Errorf("panic occurred: %s", r)
}
}()
INPUT:
for {
select {
case record, more := <-input:
if !more {
log.Info("Done processing input records")
break INPUT
}
updates := converter(record)
for _, update := range updates {
select {
case <-ctx.Done():
case output <- update:
}
}
case <-ctx.Done():
return fmt.Errorf("Processing canceled: %w", ctx.Err())
case <-time.After(1 * time.Second):
log.Info("Waiting for record to convert")
}
}
return nil
}
func (e *Execution) session() *force.Force {
if e.Session == nil {
session, err := force.ActiveForce()
if err != nil {
log.Fatalf("Failed to get active force session: %s", err.Error())
}
e.Session = session
}
return e.Session
}
func (batch Batch) marshallForBulkJob(job force.JobInfo) (updates []byte, err error) {
switch strings.ToUpper(job.ContentType) {
case "JSON":
updates, err = json.Marshal(batch)
case "XML":
xmlData := new(bytes.Buffer)
xmlData.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">`))
for _, record := range batch {
mv := mxj.Map(record)
err = mv.XmlIndentWriter(xmlData, "", " ", "sObject")
if err != nil {
return
}
}
xmlData.Write([]byte(`</sObjects>`))
updates = xmlData.Bytes()
case "CSV":
return batchToCsv(batch)
default:
err = fmt.Errorf("Unsupported ContentType: " + job.ContentType)
}
return
}
func batchToCsv(batch Batch) ([]byte, error) {
csvData := new(bytes.Buffer)
if len(batch) == 0 {
return csvData.Bytes(), nil
}
csvWriter := csv.NewWriter(csvData)
header := make([]string, 0, len(batch[0]))
for key := range batch[0] {
header = append(header, key)
}
err := csvWriter.Write(header)
if err != nil {
return csvData.Bytes(), err
}
for _, record := range batch {
row := make([]string, len(record))
for i, key := range header {
switch v := record[key].(type) {
case string:
row[i] = v
case int64:
row[i] = strconv.FormatInt(v, 10)
default:
panic(fmt.Sprintf("%+v is %T", v, v))
}
}
err := csvWriter.Write(row)
if err != nil {
return csvData.Bytes(), err
}
}
csvWriter.Flush()
if err := csvWriter.Error(); err != nil {
return csvData.Bytes(), err
}
return csvData.Bytes(), nil
}