Commit b6290361 authored by Christian Jürges's avatar Christian Jürges
Browse files

added inotify

parent 67d1a628
Pipeline #432 passed with stages
in 1 minute and 41 seconds
......@@ -6,3 +6,12 @@ db=1
[apache]
apachectl="/usr/sbin/apache2ctl"
sudo="/usr/bin/sudo"
[inotify]
paths=[
"/home/xcloud/apacheconf/sites-available",
"/home/xcloud/apacheconf/sites-enabled",
"/home/xcloud/apacheconf/static-sites-available",
"/home/xcloud/apacheconf/static-sites-enabled",
]
......@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/coreos/go-systemd/daemon"
"github.com/fsnotify/fsnotify"
"github.com/go-redis/redis"
"github.com/op/go-logging"
log2 "log"
......@@ -40,9 +41,14 @@ type apacheStruct struct {
Sudo string
}
type inotifyStruct struct {
Paths []string
}
type tomlConfig struct {
Redis redisServerStruct
Apache apacheStruct
Redis redisServerStruct
Apache apacheStruct
Inotify inotifyStruct
}
var tc tomlConfig
......@@ -51,7 +57,7 @@ func main() {
mode := flag.String("mode", "", "mode send or receive")
msg := flag.String("msg", "", "message to send")
logLevelConfig := flag.String("loglevel", "info", "loglevel to use")
flag.StringVar(&configPath, "config", "./etc/apacheocreloader.toml", "path of the config file")
flag.StringVar(&configPath, "config", "etc/apacheocreloader.toml", "path of the config file")
flag.Parse()
......@@ -96,6 +102,67 @@ func main() {
daemon.SdNotify(false, "STOPPING=1")
}
func inotifyWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Errorf("%v", err)
return
}
defer watcher.Close()
done := make(chan bool)
go func() {
callSend := false
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Info("modified file", event.Name)
callSend = true
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
log.Info("removed file", event.Name)
callSend = true
}
if event.Op&fsnotify.Create == fsnotify.Create {
log.Info("create file", event.Name)
callSend = true
}
log.Debug(event)
if callSend {
send("graceful")
callSend = false
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Error(err)
}
}
}()
for _, path := range tc.Inotify.Paths {
log.Info("adding inotify watcher on path", path)
err = watcher.Add(path)
}
<-done
}
func send(msg string) {
client := redis.NewClient(&redis.Options{
Addr: redisServer,
......@@ -128,6 +195,8 @@ func receiver() {
// notify systemd that we are more or less ready
daemon.SdNotify(false, "READY=1")
go inotifyWatcher()
// endless receiver loop
for {
......@@ -189,6 +258,7 @@ func receiver() {
}
}
// doing apache configtest and graceful if no error
func testAndDoGraceful() {
log.Info("doing apachectl")
_, err := apacheCtl(configtest)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment