@@ -18,6 +18,7 @@ import (
1818var (
1919 logAddr string
2020 logPort int
21+ drainURI string
2122 enablePublish bool
2223 publishHost string
2324 publishPath string
2930func init () {
3031 flag .StringVar (& logAddr , "log-addr" , "0.0.0.0" , "bind address for the logger" )
3132 flag .IntVar (& logPort , "log-port" , 514 , "bind port for the logger" )
33+ flag .StringVar (& drainURI , "drain-uri" , "" , "default drainURI, once set in etcd, this has no effect." )
3234 flag .StringVar (& syslogd .LogRoot , "log-root" , "/data/logs" , "log path to store logs" )
3335 flag .BoolVar (& enablePublish , "enable-publish" , false , "enable publishing to service discovery" )
3436 flag .StringVar (& publishHost , "publish-host" , getopt ("HOST" , "127.0.0.1" ), "service discovery hostname" )
@@ -43,26 +45,49 @@ func main() {
4345
4446 client := etcd .NewClient ([]string {"http://" + publishHost + ":" + publishPort })
4547
46- // Wait for terminating signal
47- exitChan := make (chan os.Signal , 2 )
48+ signalChan := make (chan os.Signal , 1 )
49+ drainRespChan := make (chan * etcd.Response )
50+ drainChan := make (chan string )
51+ stopChan := make (chan bool )
52+ exitChan := make (chan bool )
4853 cleanupChan := make (chan bool )
49- signal .Notify (exitChan , syscall .SIGTERM , syscall .SIGINT )
54+ signal .Notify (signalChan , syscall .SIGTERM , syscall .SIGINT )
5055
51- go syslogd .Listen (exitChan , cleanupChan , fmt .Sprintf ("%s:%d" , logAddr , logPort ))
56+ // ensure the drain key exists in etcd.
57+ if _ , err := client .Get (publishPath + "/drain" , false , false ); err != nil {
58+ setEtcd (client , publishPath + "/drain" , drainURI , 0 )
59+ }
5260
61+ go client .Watch (publishPath + "/drain" , 0 , false , drainRespChan , stopChan )
62+ go syslogd .Listen (exitChan , cleanupChan , drainChan , fmt .Sprintf ("%s:%d" , logAddr , logPort ))
5363 if enablePublish {
54- go publishService (client , publishHost , publishPath , strconv .Itoa (logPort ), uint64 (time .Duration (publishTTL ).Seconds ()))
64+ go publishService (exitChan , client , publishHost , publishPath , strconv .Itoa (logPort ), uint64 (time .Duration (publishTTL ).Seconds ()))
5565 }
5666
57- // Wait for the proper shutdown of the syslog server before exit
58- <- cleanupChan
67+ for {
68+ select {
69+ case er := <- drainRespChan :
70+ drainChan <- er .Node .Value
71+ case <- signalChan :
72+ close (exitChan )
73+ stopChan <- true
74+ case <- cleanupChan :
75+ return
76+ }
77+ }
5978}
6079
61- func publishService (client * etcd.Client , host string , etcdPath string , port string , ttl uint64 ) {
80+ func publishService (exitChan chan bool , client * etcd.Client , host string , etcdPath string , port string , ttl uint64 ) {
81+ t := time .NewTicker (time .Duration (publishInterval ))
82+
6283 for {
63- setEtcd (client , etcdPath + "/host" , host , ttl )
64- setEtcd (client , etcdPath + "/port" , port , ttl )
65- time .Sleep (time .Duration (publishInterval ))
84+ select {
85+ case <- t .C :
86+ setEtcd (client , etcdPath + "/host" , host , ttl )
87+ setEtcd (client , etcdPath + "/port" , port , ttl )
88+ case <- exitChan :
89+ return
90+ }
6691 }
6792}
6893
0 commit comments