@@ -17,6 +17,7 @@ import (
1717var (
1818 logAddr string
1919 logPort int
20+ drainURI string
2021 enablePublish bool
2122 publishHost string
2223 publishPath string
2829func init () {
2930 flag .StringVar (& logAddr , "log-addr" , "0.0.0.0" , "bind address for the logger" )
3031 flag .IntVar (& logPort , "log-port" , 514 , "bind port for the logger" )
32+ flag .StringVar (& drainURI , "drain-uri" , "" , "default drainURI, once set in etcd, this has no effect." )
3133 flag .StringVar (& syslogd .LogRoot , "log-root" , "/data/logs" , "log path to store logs" )
3234 flag .BoolVar (& enablePublish , "enable-publish" , false , "enable publishing to service discovery" )
3335 flag .StringVar (& publishHost , "publish-host" , getopt ("HOST" , "127.0.0.1" ), "service discovery hostname" )
@@ -42,26 +44,46 @@ func main() {
4244
4345 client := etcd .NewClient ([]string {"http://" + publishHost + ":" + publishPort })
4446
45- // Wait for terminating signal
46- exitChan := make (chan os.Signal , 2 )
47+ signalChan := make (chan os.Signal , 1 )
48+ drainChan := make (chan * etcd.Response )
49+ stopChan := make (chan bool )
50+ exitChan := make (chan bool )
4751 cleanupChan := make (chan bool )
48- signal .Notify (exitChan , syscall .SIGTERM , syscall .SIGINT )
52+ signal .Notify (signalChan , syscall .SIGTERM , syscall .SIGINT )
4953
50- go syslogd .Listen (exitChan , cleanupChan , fmt .Sprintf ("%s:%d" , logAddr , logPort ))
54+ // ensure the drain key exists in etcd.
55+ if _ , err := client .Get (publishPath + "/drain" , false , false ); err != nil {
56+ setEtcd (client , publishPath + "/drain" , drainURI , 0 )
57+ }
5158
59+ go client .Watch (publishPath + "/drain" , 0 , false , drainChan , stopChan )
60+ go syslogd .Listen (exitChan , cleanupChan , drainChan , fmt .Sprintf ("%s:%d" , logAddr , logPort ))
5261 if enablePublish {
53- go publishService (client , publishHost , publishPath , strconv .Itoa (logPort ), uint64 (time .Duration (publishTTL ).Seconds ()))
62+ go publishService (exitChan , client , publishHost , publishPath , strconv .Itoa (logPort ), uint64 (time .Duration (publishTTL ).Seconds ()))
5463 }
5564
56- // Wait for the proper shutdown of the syslog server before exit
57- <- cleanupChan
65+ for {
66+ select {
67+ case <- signalChan :
68+ close (exitChan )
69+ stopChan <- true
70+ case <- cleanupChan :
71+ return
72+ }
73+ }
5874}
5975
60- func publishService (client * etcd.Client , host string , etcdPath string , port string , ttl uint64 ) {
76+ func publishService (exitChan chan bool , client * etcd.Client , host string , etcdPath string , port string , ttl uint64 ) {
77+ t := time .NewTicker (time .Duration (publishInterval ))
78+
6179 for {
62- setEtcd (client , etcdPath + "/host" , host , ttl )
63- setEtcd (client , etcdPath + "/port" , port , ttl )
64- time .Sleep (time .Duration (publishInterval ))
80+ select {
81+ case <- t .C :
82+ setEtcd (client , etcdPath + "/host" , host , ttl )
83+ setEtcd (client , etcdPath + "/port" , port , ttl )
84+ case <- exitChan :
85+ return
86+ }
6587 }
6688}
6789
0 commit comments