增加 net 通知器
This commit is contained in:
parent
84aad1bfb8
commit
87cde6a03a
@ -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)
|
||||
|
@ -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
67
notifer.go
Normal 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
12
raw.go
@ -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 ?
|
||||
|
@ -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++ {
|
||||
|
Loading…
Reference in New Issue
Block a user