add waitgroup to journal
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
45c935403d
commit
b344f0a1c6
1 changed files with 5 additions and 1 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -25,6 +26,7 @@ func newJournal(path string) (*journal, error) {
|
||||||
enc: json.NewEncoder(f),
|
enc: json.NewEncoder(f),
|
||||||
wc: make(chan *Event, 2048),
|
wc: make(chan *Event, 2048),
|
||||||
}
|
}
|
||||||
|
j.wg.Add(1)
|
||||||
go j.start()
|
go j.start()
|
||||||
return j, nil
|
return j, nil
|
||||||
}
|
}
|
||||||
|
@ -33,9 +35,11 @@ type journal struct {
|
||||||
f *os.File
|
f *os.File
|
||||||
enc *json.Encoder
|
enc *json.Encoder
|
||||||
wc chan *Event
|
wc chan *Event
|
||||||
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) start() {
|
func (j *journal) start() {
|
||||||
|
defer j.wg.Done()
|
||||||
for e := range j.wc {
|
for e := range j.wc {
|
||||||
et := &entry{
|
et := &entry{
|
||||||
Event: e,
|
Event: e,
|
||||||
|
@ -51,7 +55,7 @@ func (j *journal) write(e *Event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *journal) Close() error {
|
func (j *journal) Close() error {
|
||||||
// TODO: add waitgroup to make sure journal is flushed
|
|
||||||
close(j.wc)
|
close(j.wc)
|
||||||
|
j.wg.Wait()
|
||||||
return j.f.Close()
|
return j.f.Close()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue