-
Notifications
You must be signed in to change notification settings - Fork 19
/
job_factory.go
89 lines (68 loc) · 2 KB
/
job_factory.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
package elasticthought
import (
"fmt"
"github.com/couchbaselabs/logg"
)
// Create a new job based on the Job Descriptor
func CreateJob(config Configuration, jobDescriptor JobDescriptor) (Runnable, error) {
// Connect to DB
db := config.DbConnection()
// Fetch doc associated w/ job descriptor
doc := &ElasticThoughtDoc{}
err := db.Retrieve(jobDescriptor.DocIdToProcess, doc)
if err != nil {
return nil, err
}
// based on document type, create the correct Runnable
switch doc.Type {
case DOC_TYPE_DATAFILE:
// create a Datafile doc
datafile := NewDatafile(config)
err = db.Retrieve(doc.Id, &datafile)
if err != nil {
errMsg := fmt.Errorf("Didn't retrieve: %v - %v", doc.Id, err)
logg.LogError(errMsg)
return nil, errMsg
}
logg.LogTo("JOB_SCHEDULER", "retrieved datafile %v from db: %+v", doc.Id, datafile)
return DatafileDownloader{
Configuration: config,
Datafile: *datafile,
}, nil
case DOC_TYPE_DATASET:
// create a Dataset doc
dataset := NewDataset(config)
err = db.Retrieve(doc.Id, &dataset)
if err != nil {
errMsg := fmt.Errorf("Didn't retrieve: %v - %v", doc.Id, err)
logg.LogError(errMsg)
return nil, errMsg
}
logg.LogTo("JOB_SCHEDULER", "retrieved dataset %v from db: %+v", doc.Id, dataset)
return DatasetSplitter{
Configuration: config,
Dataset: *dataset,
}, nil
case DOC_TYPE_TRAINING_JOB:
// create a TrainingJob doc
trainingJob := &TrainingJob{}
err = db.Retrieve(doc.Id, &trainingJob)
if err != nil {
errMsg := fmt.Errorf("Didn't retrieve: %v - %v", doc.Id, err)
logg.LogError(errMsg)
return nil, errMsg
}
trainingJob.Configuration = config
return trainingJob, nil
case DOC_TYPE_CLASSIFY_JOB:
classifyJob := NewClassifyJob(config)
err = classifyJob.Find(doc.Id)
if err != nil {
errMsg := fmt.Errorf("Didn't retrieve: %v - %v", doc.Id, err)
logg.LogError(errMsg)
return nil, errMsg
}
return classifyJob, nil
}
return nil, fmt.Errorf("Unable to create job for: %+v", jobDescriptor)
}