Skip to content

Commit 916983c

Browse files
committed
支持热加载配置(HUP信号)
1 parent 1144a9f commit 916983c

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
project_name:=supervisor-event-listener
2-
project_version:=1.2.0
2+
project_version:=1.2.1
33
root_dir := $(abspath $(CURDIR))
44
build_dir := $(root_dir)/build
55
GOPATH := ${HOME}/go
66

77

88
.PHONY: clean
99
clean:
10-
rm -fr $(buld_dir)
10+
rm -fr $(build_dir)
1111

1212
.PHONY: build-bydocker
1313
build-bydocker:

listener/listener.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ import (
1313
)
1414

1515
var (
16+
// ErrPayloadLength ...
1617
ErrPayloadLength = errors.New("Header中len长度与实际读取长度不一致")
1718
)
1819

20+
// Start ...
1921
func Start() {
22+
go run()
23+
}
24+
25+
func run() {
2026
for {
2127
defer func() {
2228
if err := recover(); err != nil {

notify/init.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package notify
22

33
import (
4+
"fmt"
5+
"os"
6+
"strings"
47
"sync/atomic"
5-
"syscall"
8+
"time"
69

710
"github.com/ouqiang/supervisor-event-listener/conf"
811
"github.com/ouqiang/supervisor-event-listener/event"
912
"github.com/ouqiang/supervisor-event-listener/utils/errlog"
10-
11-
"fmt"
12-
"os"
13-
"os/signal"
14-
"time"
1513
)
1614

1715
var (
18-
confFilePath string
19-
chanMsg = make(chan *event.Message, 2048)
20-
chanSig = make(chan os.Signal, 128)
16+
chanMsg = make(chan *event.Message, 2048)
17+
chanReload = make(chan *conf.Config, 16)
2118
// notifiables map[string]Notifiable
2219
notifiables atomic.Value
2320
)
@@ -26,7 +23,6 @@ func Init(conf *conf.Config) error {
2623
if conf == nil {
2724
return fmt.Errorf("nil config")
2825
}
29-
signal.Notify(chanSig, syscall.SIGHUP)
3026

3127
m := map[string]Notifiable{}
3228
if conf.WebHook != nil {
@@ -48,11 +44,23 @@ func Init(conf *conf.Config) error {
4844
return nil
4945
}
5046

51-
func Reload() error {
52-
fpath := confFilePath
53-
errlog.Info("loading config: %s", fpath)
47+
func Reload(conf *conf.Config) {
48+
chanReload <- conf
49+
}
50+
51+
func reload(conf *conf.Config) error {
52+
if err := Init(conf); err != nil {
53+
errlog.Info("loading config failed. %v", err)
54+
return err
55+
}
56+
names := []string{}
57+
for name := range Get() {
58+
names = append(names, name)
59+
}
60+
errlog.Info("reloaded config: %s", strings.Join(names, " "))
5461
return nil
5562
}
63+
5664
func Push(msg *event.Message) {
5765
chanMsg <- msg
5866
}
@@ -68,21 +76,14 @@ func run() {
6876
errlog.Info("msg=%s", msg.ToJson(2))
6977
handleMessage(msg)
7078
time.Sleep(50 * time.Millisecond)
71-
case sig := <-chanSig:
72-
handleSignal(sig)
79+
case conf := <-chanReload:
80+
_ = reload(conf)
7381
default:
7482
time.Sleep(50 * time.Millisecond)
7583
}
7684
}
7785
}
7886

79-
func handleSignal(sig os.Signal) error {
80-
if sig != syscall.SIGHUP {
81-
return fmt.Errorf("invalid signal %v", sig)
82-
}
83-
return Reload()
84-
}
85-
8687
func handleMessage(msg *event.Message) error {
8788
errlog.Debug("message: %+v\n", msg)
8889
m := Get()

supervisor-event-listener.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ import (
44
"flag"
55
"log"
66
"os"
7+
"os/signal"
8+
"syscall"
79

810
"github.com/ouqiang/supervisor-event-listener/conf"
911
"github.com/ouqiang/supervisor-event-listener/listener"
1012
"github.com/ouqiang/supervisor-event-listener/notify"
1113
"github.com/ouqiang/supervisor-event-listener/utils/errlog"
1214
)
1315

16+
var (
17+
chanSig = make(chan os.Signal, 128)
18+
)
19+
1420
func main() {
1521
defer func() {
1622
if err := recover(); err != nil {
@@ -38,4 +44,18 @@ func main() {
3844
}
3945
notify.Start()
4046
listener.Start()
47+
48+
signal.Notify(chanSig, syscall.SIGHUP)
49+
for sig := range chanSig {
50+
switch sig {
51+
case syscall.SIGHUP:
52+
if err := conf.Reload(*configFile); err != nil {
53+
errlog.Error("config init failed. err: %v", err)
54+
continue
55+
}
56+
notify.Reload(conf.Get())
57+
default:
58+
errlog.Info("unexpected signal %v", sig)
59+
}
60+
}
4161
}

0 commit comments

Comments
 (0)