mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-14 01:24:54 +00:00
Merge pull request #24 from adnanh/development
added hook reload on USR1 signal
This commit is contained in:
commit
aeacb6dac7
1 changed files with 49 additions and 19 deletions
46
webhook.go
46
webhook.go
|
@ -10,7 +10,9 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/adnanh/webhook/hook"
|
||||
|
||||
|
@ -21,7 +23,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
version = "2.3.1"
|
||||
version = "2.3.2"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -36,6 +38,7 @@ var (
|
|||
key = flag.String("key", "key.pem", "path to the HTTPS certificate private key pem file")
|
||||
|
||||
watcher *fsnotify.Watcher
|
||||
signals chan os.Signal
|
||||
|
||||
hooks hook.Hooks
|
||||
)
|
||||
|
@ -54,6 +57,14 @@ func init() {
|
|||
|
||||
log.Println("version " + version + " starting")
|
||||
|
||||
// set os signal watcher
|
||||
log.Printf("setting up os signal watcher\n")
|
||||
|
||||
signals = make(chan os.Signal, 1)
|
||||
signal.Notify(signals, syscall.Signal(0xa))
|
||||
|
||||
go watchForSignals()
|
||||
|
||||
// load and parse hooks
|
||||
log.Printf("attempting to load hooks from %s\n", *hooksFilePath)
|
||||
|
||||
|
@ -206,13 +217,7 @@ func handleHook(hook *hook.Hook, headers, query, payload *map[string]interface{}
|
|||
}
|
||||
}
|
||||
|
||||
func watchForFileChange() {
|
||||
for {
|
||||
select {
|
||||
case event := <-(*watcher).Events:
|
||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||
log.Println("hooks file modified")
|
||||
|
||||
func reloadHooks() {
|
||||
newHooks := hook.Hooks{}
|
||||
|
||||
// parse and swap
|
||||
|
@ -231,6 +236,16 @@ func watchForFileChange() {
|
|||
|
||||
hooks = newHooks
|
||||
}
|
||||
}
|
||||
|
||||
func watchForFileChange() {
|
||||
for {
|
||||
select {
|
||||
case event := <-(*watcher).Events:
|
||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||
log.Println("hooks file modified")
|
||||
|
||||
reloadHooks()
|
||||
}
|
||||
case err := <-(*watcher).Errors:
|
||||
log.Println("watcher error:", err)
|
||||
|
@ -238,6 +253,21 @@ func watchForFileChange() {
|
|||
}
|
||||
}
|
||||
|
||||
func watchForSignals() {
|
||||
log.Println("os signal watcher ready")
|
||||
|
||||
for {
|
||||
sig := <-signals
|
||||
if sig == syscall.Signal(0xa) {
|
||||
log.Println("caught USR1 signal")
|
||||
|
||||
reloadHooks()
|
||||
} else {
|
||||
log.Printf("caught unhandled signal %+v\n", sig)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// valuesToMap converts map[string][]string to a map[string]string object
|
||||
func valuesToMap(values map[string][]string) map[string]interface{} {
|
||||
ret := make(map[string]interface{})
|
||||
|
|
Loading…
Add table
Reference in a new issue