Skip to content

Commit 6817ea3

Browse files
committed
调整通知格式,优先显示进程名. mod构建不再需要vendor,
1 parent edb3276 commit 6817ea3

35 files changed

+86
-5978
lines changed

.vscode/launch.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${fileDirname}",
13+
"env": {},
14+
"args": []
15+
}
16+
]
17+
}

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
LDFLAGS:=-w
22
BUILD_DIR:=./build/
33
PROJECT_NAME:=supervisor-event-listener
4-
VERSION:=1.1.0
4+
VERSION:=1.1.1
55

66
install:
77
cp ./supervisor-event-listener /usr/local/bin/

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ webhook_url = http://my.webhook.com
5050
## 通知内容
5151
邮件、Slack
5252
```shell
53-
Host: ip(hostname)
5453
Process: process-name
55-
PID: 6152
56-
EXITED FROM state: RUNNING
54+
Host: ip(hostname)
55+
EXITED FROM state: RUNNING
56+
PID: 6152
5757
```
5858
WebHook, Post请求, 字段含义查看Supervisor文档
5959
```json

event/event.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ func NewMessage(h *Header, p *Payload) Message {
2828
}
2929

3030
func (msg *Message) String() string {
31-
tmpl := `Host: %s
32-
Process: %s
33-
PID: %d
34-
EXITED FROM state: %s
35-
Date: %s`
31+
tmpl := `Process: %s
32+
Host: %s
33+
State: %s
34+
PID: %d
35+
Date: %s`
3636
return fmt.Sprintf(tmpl,
37-
msg.Payload.Ip,
3837
msg.Payload.ProcessName,
39-
msg.Payload.Pid,
38+
msg.Payload.IP,
4039
msg.Payload.FromState,
40+
msg.Payload.PID,
4141
msg.TS.Format(time.RFC3339),
4242
)
4343
}
@@ -78,20 +78,20 @@ type Header struct {
7878

7979
// Payload
8080
type Payload struct {
81-
Ip string
81+
IP string
8282
ProcessName string // 进程名称
8383
GroupName string // 进程组名称
8484
FromState string
8585
Expected int
86-
Pid int
86+
PID int
8787
}
8888

8989
// Fields
9090
type Fields map[string]string
9191

9292
var (
93-
ErrParseHeader = errors.New("解析Header失败")
94-
ErrParsePayload = errors.New("解析Payload失败")
93+
ErrParseHeader = errors.New("parse header failed")
94+
ErrParsePayload = errors.New("parse payload failed")
9595
)
9696

9797
func ParseHeader(header string) (*Header, error) {
@@ -119,12 +119,12 @@ func ParsePayload(payload string) (*Payload, error) {
119119
return p, ErrParsePayload
120120
}
121121
hostname, _ := os.Hostname()
122-
p.Ip = fmt.Sprintf("%s(%s)", utils.GetLocalIp(), hostname)
122+
p.IP = fmt.Sprintf("%s(%s)", utils.GetLocalIp(), hostname)
123123
p.ProcessName = fields["processname"]
124124
p.GroupName = fields["groupname"]
125125
p.FromState = fields["from_state"]
126126
p.Expected, _ = strconv.Atoi(fields["expected"])
127-
p.Pid, _ = strconv.Atoi(fields["pid"])
127+
p.PID, _ = strconv.Atoi(fields["pid"])
128128

129129
return p, nil
130130
}

listener/notify/init.go

+52-23
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11
package notify
22

33
import (
4+
"syscall"
5+
46
"github.com/ouqiang/supervisor-event-listener/config"
57
"github.com/ouqiang/supervisor-event-listener/event"
68
"github.com/ouqiang/supervisor-event-listener/utils/tmpfslog"
79

810
"fmt"
911
"os"
12+
"os/signal"
1013
"time"
1114
)
1215

1316
var (
14-
Conf *config.Config
15-
queue chan event.Message
17+
confFilePath string
18+
Conf *config.Config
19+
chanMsg chan event.Message
20+
chanSig chan os.Signal = make(chan os.Signal, 100)
1621
)
1722

1823
func Init(fpath string) error {
24+
tmpfslog.Info("loading config: %s", fpath)
25+
if Conf != nil {
26+
return fmt.Errorf("init twice!!!")
27+
}
28+
Conf = config.ParseConfig(fpath)
29+
chanMsg = make(chan event.Message, 10)
30+
confFilePath = fpath
31+
signal.Notify(chanSig, syscall.SIGHUP)
32+
return nil
33+
}
34+
35+
func Reload() error {
36+
fpath := confFilePath
1937
tmpfslog.Info("loading config: %s", fpath)
2038
Conf = config.ParseConfig(fpath)
21-
queue = make(chan event.Message, 10)
2239
return nil
2340
}
2441

@@ -27,35 +44,47 @@ type Notifiable interface {
2744
}
2845

2946
func Push(header *event.Header, payload *event.Payload) {
30-
queue <- event.NewMessage(header, payload)
47+
chanMsg <- event.NewMessage(header, payload)
3148
}
3249

3350
func Start() {
3451
go start()
3552
}
3653

3754
func start() {
38-
var message event.Message
55+
select {
56+
case msg := <-chanMsg:
57+
handleMessage(msg)
58+
case sig := <-chanSig:
59+
handleSignal(sig)
60+
}
61+
}
62+
63+
func handleSignal(sig os.Signal) error {
64+
if sig != syscall.SIGHUP {
65+
return fmt.Errorf("invalid signal %v", sig)
66+
}
67+
return Reload()
68+
}
69+
70+
func handleMessage(msg event.Message) error {
71+
tmpfslog.Debug("message: %+v\n", msg)
3972
var notifyHandler Notifiable
40-
for {
41-
message = <-queue
42-
tmpfslog.Debug("message: %+v\n", message)
43-
switch Conf.NotifyType {
44-
case "mail":
45-
notifyHandler = &Mail{}
46-
case "slack":
47-
notifyHandler = &Slack{}
48-
case "webhook":
49-
notifyHandler = &WebHook{}
50-
case "bearychat":
51-
notifyHandler = &BearyChat{}
52-
}
53-
if notifyHandler == nil {
54-
continue
55-
}
56-
go send(notifyHandler, message)
57-
time.Sleep(1 * time.Second)
73+
switch Conf.NotifyType {
74+
case "mail":
75+
notifyHandler = &Mail{}
76+
case "slack":
77+
notifyHandler = &Slack{}
78+
case "webhook":
79+
notifyHandler = &WebHook{}
80+
case "bearychat":
81+
notifyHandler = &BearyChat{}
5882
}
83+
if notifyHandler == nil {
84+
return fmt.Errorf("invalid notify type %s", Conf.NotifyType)
85+
}
86+
go send(notifyHandler, msg)
87+
return nil
5988
}
6089

6190
func send(notifyHandler Notifiable, message event.Message) {

vendor/github.com/go-gomail/gomail/CHANGELOG.md

-20
This file was deleted.

vendor/github.com/go-gomail/gomail/CONTRIBUTING.md

-20
This file was deleted.

vendor/github.com/go-gomail/gomail/LICENSE

-20
This file was deleted.

vendor/github.com/go-gomail/gomail/README.md

-92
This file was deleted.

0 commit comments

Comments
 (0)