-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.go
230 lines (177 loc) · 5.63 KB
/
environment.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
/*
Migration Execution Environment
Anser provides the Environment interface, with a global instance
accessible via the exported GetEnvironment() function to provide
access to runtime configuration state: database connections;
amboy.Queue objects, and registries for task implementations.
The Environment is an interface: you can build a mock, or use one
provided for testing purposes by anser (coming soon).
*/
package anser
import (
"sync"
"github.com/cdr/amboy"
"github.com/cdr/amboy/dependency"
"github.com/deciduosity/anser/client"
"github.com/deciduosity/anser/model"
"github.com/cdr/grip"
"github.com/pkg/errors"
)
const (
defaultMetadataCollection = "migrations.metadata"
defaultAnserDB = "anser"
)
var globalEnv *envState
func init() { ResetEnvironment() }
// Environment exposes the execution environment for the migration
// utility, and is the method by which, potentially serialized job
// definitions are able to gain access to the database and through
// which generator jobs are able to gain access to the queue.
//
// Implementations should be thread-safe, and are not required to be
// reconfigurable after their initial configuration.
type Environment interface {
Setup(amboy.Queue, client.Client, string) error
GetClient() (client.Client, error)
GetQueue() (amboy.Queue, error)
GetDependencyNetwork() (model.DependencyNetworker, error)
MetadataNamespace() model.Namespace
RegisterManualMigrationOperation(string, client.MigrationOperation) error
GetManualMigrationOperation(string) (client.MigrationOperation, bool)
RegisterDocumentProcessor(string, client.Processor) error
GetDocumentProcessor(string) (client.Processor, bool)
NewDependencyManager(string) dependency.Manager
RegisterCloser(func() error)
Close() error
}
// GetEnvironment returns the global environment object. Because this
// produces a pointer to the global object, make sure that you have a
// way to replace it with a mock as needed for testing.
func GetEnvironment() Environment { return globalEnv }
// ResetEnvironment resets the global environment object. Use this
// only in testing (and only when you must.) It is not safe for
// concurrent use.
func ResetEnvironment() {
globalEnv = &envState{
migrations: make(map[string]client.MigrationOperation),
processor: make(map[string]client.Processor),
}
}
type envState struct {
queue amboy.Queue
metadataNS model.Namespace
client client.Client
deps model.DependencyNetworker
migrations map[string]client.MigrationOperation
processor map[string]client.Processor
closers []func() error
isSetup bool
preferClient bool
mu sync.RWMutex
}
func (e *envState) Setup(q amboy.Queue, cl client.Client, name string) error {
catcher := grip.NewBasicCatcher()
catcher.NewWhen(cl == nil, "cannot use a nil client")
e.mu.Lock()
defer e.mu.Unlock()
catcher.NewWhen(e.isSetup, "reconfiguring a queue is not supported")
catcher.NewWhen(!q.Info().Started, "configuring anser environment with a non-running queue")
if catcher.HasErrors() {
return catcher.Resolve()
}
e.queue = q
e.client = cl
e.metadataNS.Collection = defaultMetadataCollection
e.metadataNS.DB = name
e.isSetup = true
e.deps = newDependencyNetwork()
return nil
}
func (e *envState) GetClient() (client.Client, error) {
e.mu.RLock()
defer e.mu.RUnlock()
if e.client == nil {
return nil, errors.New("no session defined")
}
return e.client, nil
}
func (e *envState) GetQueue() (amboy.Queue, error) {
e.mu.RLock()
defer e.mu.RUnlock()
if e.queue == nil {
return nil, errors.New("no queue defined")
}
return e.queue, nil
}
func (e *envState) GetDependencyNetwork() (model.DependencyNetworker, error) {
e.mu.RLock()
defer e.mu.RUnlock()
if e.deps == nil {
return nil, errors.New("no dependency networker specified")
}
return e.deps, nil
}
func (e *envState) RegisterManualMigrationOperation(name string, op client.MigrationOperation) error {
e.mu.Lock()
defer e.mu.Unlock()
if _, ok := e.migrations[name]; ok {
return errors.Errorf("migration operation %s already exists", name)
}
e.migrations[name] = op
return nil
}
func (e *envState) GetManualMigrationOperation(name string) (client.MigrationOperation, bool) {
e.mu.RLock()
defer e.mu.RUnlock()
op, ok := e.migrations[name]
return op, ok
}
func (e *envState) RegisterDocumentProcessor(name string, docp client.Processor) error {
e.mu.Lock()
defer e.mu.Unlock()
if _, ok := e.processor[name]; ok {
return errors.Errorf("document processor named %s already registered", name)
}
e.processor[name] = docp
return nil
}
func (e *envState) GetDocumentProcessor(name string) (client.Processor, bool) {
e.mu.RLock()
defer e.mu.RUnlock()
docp, ok := e.processor[name]
return docp, ok
}
func (e *envState) MetadataNamespace() model.Namespace {
e.mu.RLock()
defer e.mu.RUnlock()
return e.metadataNS
}
func (e *envState) NewDependencyManager(migrationID string) dependency.Manager {
d := makeMigrationDependencyManager()
e.mu.RLock()
defer e.mu.RUnlock()
d.MigrationID = migrationID
d.MigrationHelper = NewMigrationHelper(e)
return d
}
func (e *envState) RegisterCloser(closer func() error) {
if closer == nil {
return
}
e.mu.Lock()
defer e.mu.Unlock()
e.closers = append(e.closers, closer)
}
func (e *envState) Close() error {
e.mu.Lock()
defer e.mu.Unlock()
grip.Noticef("closing %d resources registered in the anser environment", len(e.closers))
catcher := grip.NewSimpleCatcher()
for _, closer := range e.closers {
catcher.Add(closer())
}
if catcher.HasErrors() {
grip.Warningf("encountered %d errors closing anser resources, out of %d", catcher.Len(), len(e.closers))
}
return catcher.Resolve()
}