feat(fsrepo) add eventlog config to repo/config struct
Brian Tiger Chow committed
Feb 2, 2015 at 17:42 UTC
454cd454ea3631e8040db907c9511e25f240a9b9
8 files changed
+31
-14
repo/config/config.go
+1
@@ -24,6 +24,7 @@ type Config struct {
24
Bootstrap []string // local nodes's bootstrap peer addresses
25
Tour Tour // local node's tour position
26
Gateway Gateway // local node's gateway server options
27
+ Log Log
28
}
29
30
const (
repo/config/log.go
new
+8
@@ -0,0 +1,8 @@
1
+package config
2
+
3
+
4
+type Log struct {
5
+ MaxSizeMB uint64
6
+ MaxBackups uint64
7
+ MaxAgeDays uint64
8
+}
repo/fsrepo/component/component.go
+1
-1
@@ -7,7 +7,7 @@ import (
7
)
8
9
type Component interface {
10
- Open() error
10
+ Open(*config.Config) error
11
io.Closer
12
SetPath(string)
13
}
repo/fsrepo/component/config.go
+4
-2
@@ -37,8 +37,10 @@ func InitConfigComponent(path string, conf *config.Config) error {
37
return nil
38
}
39
40
-// Open returns an error if the config file is not present.
41
-func (c *ConfigComponent) Open() error {
40
+// Open returns an error if the config file is not present. This component is
41
+// always called with a nil config parameter. Other components rely on the
42
+// config, to keep the interface uniform, it is special-cased.
43
+func (c *ConfigComponent) Open(_ *config.Config) error {
44
configFilename, err := config.Filename(c.path)
45
if err != nil {
46
return err
repo/fsrepo/component/datastore.go
+1
-1
@@ -66,7 +66,7 @@ func (dsc *DatastoreComponent) SetPath(p string) {
66
func (dsc *DatastoreComponent) Datastore() datastore.ThreadSafeDatastore { return dsc.ds }
67
68
// Open returns an error if the config file is not present.
69
-func (dsc *DatastoreComponent) Open() error {
69
+func (dsc *DatastoreComponent) Open(*config.Config) error {
70
71
dsLock.Lock()
72
defer dsLock.Unlock()
repo/fsrepo/component/datastore_test.go
+2
-2
@@ -22,8 +22,8 @@ func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
22
path := testRepoPath(t)
23
dsc1 := DatastoreComponent{path: path}
24
dsc2 := DatastoreComponent{path: path}
25
- assert.Nil(dsc1.Open(), t, "first repo should open successfully")
26
- assert.Nil(dsc2.Open(), t, "second repo should open successfully")
25
+ assert.Nil(dsc1.Open(nil), t, "first repo should open successfully")
26
+ assert.Nil(dsc2.Open(nil), t, "second repo should open successfully")
27
28
assert.Nil(dsc1.Close(), t)
29
assert.Nil(dsc2.Close(), t)
repo/fsrepo/component/eventlog.go
+9
-4
@@ -35,17 +35,22 @@ func (c *EventlogComponent) Close() error {
35
return nil
36
}
37
38
-func (c *EventlogComponent) Open() error {
38
+func (c *EventlogComponent) Open(config *config.Config) error {
39
// log.Debugf("writing eventlogs to ...", c.path)
40
- return configureEventLoggerAtRepoPath(c.path)
40
+ return configureEventLoggerAtRepoPath(config, c.path)
41
}
42
43
-func configureEventLoggerAtRepoPath(repoPath string) error {
43
+func configureEventLoggerAtRepoPath(c *config.Config, repoPath string) error {
44
eventlog.Configure(eventlog.LevelInfo)
45
eventlog.Configure(eventlog.LdJSONFormatter)
46
rotateConf := eventlog.LogRotatorConfig{
47
- Filename: path.Join(repoPath, "logs", "events.log"),
47
+ Filename: path.Join(repoPath, "logs", "events.log"),
48
+ MaxSizeMB: c.Log.MaxSizeMB,
49
+ MaxBackups: c.Log.MaxBackups,
50
+ MaxAgeDays: c.Log.MaxAgeDays,
51
}
52
eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf))
53
return nil
54
}
55
+
56
+var _ Component = &EventlogComponent{}
repo/fsrepo/fsrepo.go
+5
-4
@@ -316,14 +316,15 @@ func (r *FSRepo) components() []component.Component {
316
func componentBuilders() []componentBuilder {
317
return []componentBuilder{
318
319
- // ConfigComponent
319
+ // ConfigComponent must be initialized first because other components
320
+ // depend on it.
321
componentBuilder{
322
Init: component.InitConfigComponent,
323
IsInitialized: component.ConfigComponentIsInitialized,
324
OpenHandler: func(r *FSRepo) error {
325
c := component.ConfigComponent{}
326
c.SetPath(r.path)
326
- if err := c.Open(); err != nil {
327
+ if err := c.Open(nil); err != nil {
328
return err
329
}
330
r.configComponent = c
@@ -338,7 +339,7 @@ func componentBuilders() []componentBuilder {
339
OpenHandler: func(r *FSRepo) error {
340
c := component.DatastoreComponent{}
341
c.SetPath(r.path)
341
- if err := c.Open(); err != nil {
342
+ if err := c.Open(r.configComponent.Config()); err != nil {
343
return err
344
}
345
r.datastoreComponent = c
@@ -353,7 +354,7 @@ func componentBuilders() []componentBuilder {
354
OpenHandler: func(r *FSRepo) error {
355
c := component.EventlogComponent{}
356
c.SetPath(r.path)
356
- if err := c.Open(); err != nil {
357
+ if err := c.Open(r.configComponent.Config()); err != nil {
358
return err
359
}
360
r.eventlogComponent = c