Merge pull request #961 from hashicorp/executor-tests

Making the calls to exit idempotent
This commit is contained in:
Diptanu Choudhury 2016-03-22 12:27:37 -07:00
commit 2ab8ab7789
2 changed files with 25 additions and 5 deletions

View file

@ -10,6 +10,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
) )
@ -35,6 +36,9 @@ type FileRotator struct {
logger *log.Logger logger *log.Logger
purgeCh chan struct{} purgeCh chan struct{}
doneCh chan struct{} doneCh chan struct{}
closed bool
closedLock sync.Mutex
} }
// NewFileRotator returns a new file rotator // NewFileRotator returns a new file rotator
@ -194,6 +198,9 @@ func (f *FileRotator) flushPeriodically() {
} }
func (f *FileRotator) Close() { func (f *FileRotator) Close() {
f.closedLock.Lock()
defer f.closedLock.Unlock()
// Stop the ticker and flush for one last time // Stop the ticker and flush for one last time
f.flushTicker.Stop() f.flushTicker.Stop()
if f.bufw != nil { if f.bufw != nil {
@ -201,8 +208,11 @@ func (f *FileRotator) Close() {
} }
// Stop the purge go routine // Stop the purge go routine
f.doneCh <- struct{}{} if !f.closed {
close(f.purgeCh) f.doneCh <- struct{}{}
close(f.purgeCh)
f.closed = true
}
} }
// purgeOldFiles removes older files and keeps only the last N files rotated for // purgeOldFiles removes older files and keeps only the last N files rotated for

View file

@ -6,6 +6,7 @@ import (
"bufio" "bufio"
"log" "log"
"net" "net"
"sync"
) )
// SyslogServer is a server which listens to syslog messages and parses them // SyslogServer is a server which listens to syslog messages and parses them
@ -14,7 +15,10 @@ type SyslogServer struct {
messages chan *SyslogMessage messages chan *SyslogMessage
parser *DockerLogParser parser *DockerLogParser
doneCh chan interface{} doneCh chan interface{}
done bool
doneLock sync.Mutex
logger *log.Logger logger *log.Logger
} }
@ -71,6 +75,12 @@ func (s *SyslogServer) read(connection net.Conn) {
// Shutdown shutsdown the syslog server // Shutdown shutsdown the syslog server
func (s *SyslogServer) Shutdown() { func (s *SyslogServer) Shutdown() {
close(s.doneCh) s.doneLock.Lock()
close(s.messages) s.doneLock.Unlock()
if !s.done {
close(s.doneCh)
close(s.messages)
s.done = true
}
} }