-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_job_stream.go
77 lines (64 loc) · 1.88 KB
/
migration_job_stream.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
package anser
import (
"context"
"github.com/cdr/amboy"
"github.com/cdr/amboy/job"
"github.com/cdr/amboy/registry"
"github.com/deciduosity/anser/model"
"github.com/cdr/grip"
"github.com/cdr/grip/message"
"github.com/pkg/errors"
)
func init() {
registry.AddJobType("stream-migration", func() amboy.Job { return makeStreamProducer() })
}
func NewStreamMigration(e Environment, m model.Stream) Migration {
j := makeStreamProducer()
j.Definition = m
j.MigrationHelper = NewMigrationHelper(e)
return j
}
func makeStreamProducer() *streamMigrationJob {
return &streamMigrationJob{
MigrationHelper: &migrationBase{},
Base: job.Base{
JobType: amboy.JobType{
Name: "stream-migration",
Version: 0,
},
},
}
}
type streamMigrationJob struct {
Definition model.Stream `bson:"migration" json:"migration" yaml:"migration"`
job.Base `bson:"job_base" json:"job_base" yaml:"job_base"`
MigrationHelper `bson:"-" json:"-" yaml:"-"`
}
func (j *streamMigrationJob) Run(ctx context.Context) {
grip.Info(message.Fields{
"message": "starting migration",
"migration": j.Definition.Migration,
"operation": "stream",
"id": j.ID(),
"ns": j.Definition.Namespace,
"name": j.Definition.ProcessorName,
})
defer j.FinishMigration(ctx, j.Definition.Migration, &j.Base)
env := j.Env()
if producer, ok := env.GetDocumentProcessor(j.Definition.ProcessorName); ok {
client, err := env.GetClient()
if err != nil {
j.AddError(errors.Wrap(err, "problem getting database client"))
return
}
iter := producer.Load(client, j.Definition.Namespace, j.Definition.Query)
if iter == nil {
j.AddError(errors.Errorf("document processor for %s could not return iterator",
j.Definition.Migration))
return
}
j.AddError(producer.Migrate(iter))
} else {
j.AddError(errors.Errorf("producer named '%s' is not defined", j.Definition.ProcessorName))
}
}