forked from tleyden/elastic-thought
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datafile_downloader.go
72 lines (59 loc) · 1.77 KB
/
datafile_downloader.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
package elasticthought
import (
"fmt"
"sync"
"github.com/couchbaselabs/logg"
)
// Worker job that downloads a datafile url contents to cbfs
type DatafileDownloader struct {
Configuration Configuration
Datafile Datafile
}
// Run this job
func (d DatafileDownloader) Run(wg *sync.WaitGroup) {
defer wg.Done()
logg.LogTo("DATAFILE_DOWNLOADER", "datafile downloader run()")
db := d.Configuration.DbConnection()
datafile := &d.Datafile
updatedState, err := datafile.UpdateProcessingState(Processing)
if err != nil {
d.recordProcessingError(err)
return
}
if !updatedState {
logg.LogTo("DATAFILE_DOWNLOADER", "%+v already processed. Ignoring.", d)
return
}
// create a new cbfs client
cbfs, err := d.Configuration.NewBlobStoreClient()
if err != nil {
errMsg := fmt.Errorf("Error creating cbfs client: %v", err)
d.recordProcessingError(errMsg)
return
}
// copy url contents to cbfs
logg.LogTo("DATAFILE_DOWNLOADER", "Put to CBFS: %+v %v %v", d, db, cbfs)
cbfsDestPath, err := d.Datafile.CopyToBlobStore(db, cbfs)
if err != nil {
d.recordProcessingError(err)
return
}
// build a url to the cbfs file
cbfsUrl := fmt.Sprintf("%v/%v", d.Configuration.CbfsUrl, cbfsDestPath)
d.Datafile.Url = cbfsUrl
// Update the state of the dataset to be finished
if err := d.Datafile.FinishedSuccessfully(db); err != nil {
errMsg := fmt.Errorf("Error marking datafile %+v finished: %v", d, err)
d.recordProcessingError(errMsg)
return
}
}
// Codereview: de-dupe -- dataset_splitter has same method
func (d DatafileDownloader) recordProcessingError(err error) {
logg.LogError(err)
db := d.Configuration.DbConnection()
if err := d.Datafile.Failed(db, err); err != nil {
errMsg := fmt.Errorf("Error setting datafile as failed: %v", err)
logg.LogError(errMsg)
}
}