Skip to content

Commit

Permalink
调整通知格式,优先显示进程名. mod构建不再需要vendor,
Browse files Browse the repository at this point in the history
  • Loading branch information
cupen committed May 22, 2020
1 parent edb3276 commit 6817ea3
Show file tree
Hide file tree
Showing 35 changed files with 86 additions and 5,978 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
LDFLAGS:=-w
BUILD_DIR:=./build/
PROJECT_NAME:=supervisor-event-listener
VERSION:=1.1.0
VERSION:=1.1.1

install:
cp ./supervisor-event-listener /usr/local/bin/
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ webhook_url = http://my.webhook.com
## 通知内容
邮件、Slack
```shell
Host: ip(hostname)
Process: process-name
PID: 6152
EXITED FROM state: RUNNING
Host: ip(hostname)
EXITED FROM state: RUNNING
PID: 6152
```
WebHook, Post请求, 字段含义查看Supervisor文档
```json
Expand Down
26 changes: 13 additions & 13 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ func NewMessage(h *Header, p *Payload) Message {
}

func (msg *Message) String() string {
tmpl := `Host: %s
Process: %s
PID: %d
EXITED FROM state: %s
Date: %s`
tmpl := `Process: %s
Host: %s
State: %s
PID: %d
Date: %s`
return fmt.Sprintf(tmpl,
msg.Payload.Ip,
msg.Payload.ProcessName,
msg.Payload.Pid,
msg.Payload.IP,
msg.Payload.FromState,
msg.Payload.PID,
msg.TS.Format(time.RFC3339),
)
}
Expand Down Expand Up @@ -78,20 +78,20 @@ type Header struct {

// Payload
type Payload struct {
Ip string
IP string
ProcessName string // 进程名称
GroupName string // 进程组名称
FromState string
Expected int
Pid int
PID int
}

// Fields
type Fields map[string]string

var (
ErrParseHeader = errors.New("解析Header失败")
ErrParsePayload = errors.New("解析Payload失败")
ErrParseHeader = errors.New("parse header failed")
ErrParsePayload = errors.New("parse payload failed")
)

func ParseHeader(header string) (*Header, error) {
Expand Down Expand Up @@ -119,12 +119,12 @@ func ParsePayload(payload string) (*Payload, error) {
return p, ErrParsePayload
}
hostname, _ := os.Hostname()
p.Ip = fmt.Sprintf("%s(%s)", utils.GetLocalIp(), hostname)
p.IP = fmt.Sprintf("%s(%s)", utils.GetLocalIp(), hostname)
p.ProcessName = fields["processname"]
p.GroupName = fields["groupname"]
p.FromState = fields["from_state"]
p.Expected, _ = strconv.Atoi(fields["expected"])
p.Pid, _ = strconv.Atoi(fields["pid"])
p.PID, _ = strconv.Atoi(fields["pid"])

return p, nil
}
Expand Down
75 changes: 52 additions & 23 deletions listener/notify/init.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
package notify

import (
"syscall"

"github.com/ouqiang/supervisor-event-listener/config"
"github.com/ouqiang/supervisor-event-listener/event"
"github.com/ouqiang/supervisor-event-listener/utils/tmpfslog"

"fmt"
"os"
"os/signal"
"time"
)

var (
Conf *config.Config
queue chan event.Message
confFilePath string
Conf *config.Config
chanMsg chan event.Message
chanSig chan os.Signal = make(chan os.Signal, 100)
)

func Init(fpath string) error {
tmpfslog.Info("loading config: %s", fpath)
if Conf != nil {
return fmt.Errorf("init twice!!!")
}
Conf = config.ParseConfig(fpath)
chanMsg = make(chan event.Message, 10)
confFilePath = fpath
signal.Notify(chanSig, syscall.SIGHUP)
return nil
}

func Reload() error {
fpath := confFilePath
tmpfslog.Info("loading config: %s", fpath)
Conf = config.ParseConfig(fpath)
queue = make(chan event.Message, 10)
return nil
}

Expand All @@ -27,35 +44,47 @@ type Notifiable interface {
}

func Push(header *event.Header, payload *event.Payload) {
queue <- event.NewMessage(header, payload)
chanMsg <- event.NewMessage(header, payload)
}

func Start() {
go start()
}

func start() {
var message event.Message
select {
case msg := <-chanMsg:
handleMessage(msg)
case sig := <-chanSig:
handleSignal(sig)
}
}

func handleSignal(sig os.Signal) error {
if sig != syscall.SIGHUP {
return fmt.Errorf("invalid signal %v", sig)
}
return Reload()
}

func handleMessage(msg event.Message) error {
tmpfslog.Debug("message: %+v\n", msg)
var notifyHandler Notifiable
for {
message = <-queue
tmpfslog.Debug("message: %+v\n", message)
switch Conf.NotifyType {
case "mail":
notifyHandler = &Mail{}
case "slack":
notifyHandler = &Slack{}
case "webhook":
notifyHandler = &WebHook{}
case "bearychat":
notifyHandler = &BearyChat{}
}
if notifyHandler == nil {
continue
}
go send(notifyHandler, message)
time.Sleep(1 * time.Second)
switch Conf.NotifyType {
case "mail":
notifyHandler = &Mail{}
case "slack":
notifyHandler = &Slack{}
case "webhook":
notifyHandler = &WebHook{}
case "bearychat":
notifyHandler = &BearyChat{}
}
if notifyHandler == nil {
return fmt.Errorf("invalid notify type %s", Conf.NotifyType)
}
go send(notifyHandler, msg)
return nil
}

func send(notifyHandler Notifiable, message event.Message) {
Expand Down
20 changes: 0 additions & 20 deletions vendor/github.com/go-gomail/gomail/CHANGELOG.md

This file was deleted.

20 changes: 0 additions & 20 deletions vendor/github.com/go-gomail/gomail/CONTRIBUTING.md

This file was deleted.

20 changes: 0 additions & 20 deletions vendor/github.com/go-gomail/gomail/LICENSE

This file was deleted.

92 changes: 0 additions & 92 deletions vendor/github.com/go-gomail/gomail/README.md

This file was deleted.

Loading

0 comments on commit 6817ea3

Please sign in to comment.