增加 net 通知器

master
dengsgo 2019-01-02 19:07:52 +08:00
parent 84aad1bfb8
commit 87cde6a03a
5 changed files with 89 additions and 4 deletions

View File

@ -129,7 +129,7 @@ func initWatcher() {
defer watcher.Close()
done := make(chan bool)
taskMan = newTaskMan(cfg.Command.DelayMillSecond)
taskMan = newTaskMan(cfg.Command.DelayMillSecond, cfg.Notifier.CallUrl)
go func() {
for {
select {
@ -176,7 +176,7 @@ func parseArgs() {
return
case "exec":
parseConfig()
newTaskMan(0).run(new(changeFile))
newTaskMan(0, cfg.Notifier.CallUrl).run(new(changeFile))
return
case "version", "v", "-v", "--version":
fmt.Println(versionDesc)

View File

@ -13,4 +13,7 @@ type FileGirl struct {
Exec []string `yaml:"exec"`
DelayMillSecond int `yaml:"delayMillSecond"`
}
Notifier struct {
CallUrl string `yaml:"callUrl"`
}
}

67
notifer.go Normal file
View File

@ -0,0 +1,67 @@
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"strings"
)
type postParams struct {
ProjectFolder string `json:"project_folder"`
File string `json:"file"`
Changed int64 `json:"changed"`
Ext string `json:"ext"`
}
type NetNotifier struct {
CallUrl string
CanPost bool
}
func newNetNotifier(callUrl string) *NetNotifier {
callPost := true
if strings.TrimSpace(callUrl) == "" {
callPost = false
}
return &NetNotifier{
CallUrl: callUrl,
CanPost: callPost,
}
}
func (n *NetNotifier) Put(cf *changeFile) {
if !n.CanPost {
log.Println("notifier call url ignore. ", n.CallUrl)
return
}
n.dispatch(&postParams{
ProjectFolder: projectFolder,
File: cf.Name,
Changed: cf.Changed,
Ext: cf.Ext,
})
}
func (n *NetNotifier) dispatch(params *postParams) {
b, err := json.Marshal(params)
if err != nil {
log.Println("error: json.Marshal n.params. ", err)
return
}
client := &http.Client{}
req, err := http.NewRequest("POST", n.CallUrl, bytes.NewBuffer(b))
if err != nil {
log.Println("error: http.NewRequest. ", err)
return
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.2")
resp, err := client.Do(req)
defer resp.Body.Close()
if resp.StatusCode >= 300 {
// todo retry???
}
log.Println("notifier done .")
}

12
raw.go
View File

@ -53,6 +53,18 @@ command:
#
# 0
delayMillSecond: 1000
#
notifier:
# url POST json
# : http://example.com/notifier/fileboy-listener
# command
# POST :
# Content-Type: application/json;charset=UTF-8
# User-Agent: FileBoy Net Notifier v1.2
# Body: {"project_folder":"/watcher-dirs","file":"test.go","changed":1546421173070433800,"ext":".go"}
#
callUrl: ""
`
var firstRunHelp = ` fileboy ?

View File

@ -14,13 +14,15 @@ type TaskMan struct {
lastTaskId int64
delay int
cmd *exec.Cmd
notifier *NetNotifier
putLock sync.Mutex
runLock sync.Mutex
}
func newTaskMan(delay int) *TaskMan {
func newTaskMan(delay int, callUrl string) *TaskMan {
return &TaskMan{
delay: delay,
delay: delay,
notifier: newNetNotifier(callUrl),
}
}
@ -53,6 +55,7 @@ func (t *TaskMan) preRun(cf *changeFile) {
}
func (t *TaskMan) run(cf *changeFile) {
go t.notifier.Put(cf)
t.runLock.Lock()
defer t.runLock.Unlock()
for i := 0; i < len(cfg.Command.Exec); i++ {