evdist: Retactor + include dashboard lookup
This commit is contained in:
parent
d44fc4f715
commit
5592fabefa
6 changed files with 336 additions and 178 deletions
129
evdist/main.go
129
evdist/main.go
|
|
@ -2,13 +2,10 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -21,7 +18,7 @@ var SettingsDistDir = "./SETTINGSDIST/"
|
|||
var TmpSettingsDirectory string
|
||||
var TmpSettingsDistDirectory string
|
||||
|
||||
func watchsubdir(l *distList, watcher *fsnotify.Watcher, pathname string) error {
|
||||
func watchsubdir(l *distList, watcher *fsnotify.Watcher, pathname string, treat func(l *distList, pathname string)) error {
|
||||
log.Println("Watch new directory:", pathname)
|
||||
if err := watcher.Add(pathname); err != nil {
|
||||
return err
|
||||
|
|
@ -33,7 +30,7 @@ func watchsubdir(l *distList, watcher *fsnotify.Watcher, pathname string) error
|
|||
for _, d := range ds {
|
||||
p := path.Join(pathname, d.Name())
|
||||
if d.Mode().IsRegular() && d.Name() != ".tmp" {
|
||||
l.treat(p)
|
||||
treat(l, p)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
@ -41,6 +38,7 @@ func watchsubdir(l *distList, watcher *fsnotify.Watcher, pathname string) error
|
|||
}
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&DashboardDir, "dashboard", DashboardDir, "Base directory where read dashboard files")
|
||||
flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where read settings")
|
||||
flag.StringVar(&SettingsDistDir, "settingsDist", SettingsDistDir, "Directory where place settings to distribute")
|
||||
var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
|
||||
|
|
@ -48,6 +46,7 @@ func main() {
|
|||
|
||||
log.SetPrefix("[evdist] ")
|
||||
|
||||
DashboardDir = path.Clean(DashboardDir)
|
||||
settings.SettingsDir = path.Clean(settings.SettingsDir)
|
||||
|
||||
log.Println("Creating settingsDist directory...")
|
||||
|
|
@ -72,58 +71,73 @@ func main() {
|
|||
}
|
||||
defer watcher.Close()
|
||||
|
||||
l := &distList{}
|
||||
l.Timer = time.NewTimer(time.Minute)
|
||||
lDashboard := &distList{}
|
||||
lDashboard.Timer = time.NewTimer(time.Minute)
|
||||
|
||||
if err := watchsubdir(l, watcher, settings.SettingsDir); err != nil {
|
||||
if err := watchsubdir(lDashboard, watcher, DashboardDir, newDashboardFile); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
lSettings := &distList{}
|
||||
lSettings.Timer = time.NewTimer(time.Minute)
|
||||
|
||||
if err := watchsubdir(lSettings, watcher, settings.SettingsDir, newSettingsFile); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
watchedNotify := fsnotify.Create
|
||||
|
||||
treatNewFile := func(name string) {
|
||||
if strings.HasPrefix(name, DashboardDir) {
|
||||
newDashboardFile(lDashboard, name)
|
||||
} else {
|
||||
newSettingsFile(lSettings, name)
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-l.Timer.C:
|
||||
if v := l.Pop(); v != nil {
|
||||
log.Printf("TREATING DIFF: %v", v)
|
||||
|
||||
v, err = settings.ReadNextSettingsFile(path.Join(settings.SettingsDir, fmt.Sprintf("%d.json", v.Id)), v.Id)
|
||||
if err != nil {
|
||||
log.Printf("Unable to read json: %s", err.Error())
|
||||
} else if cur_settings, err := settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile)); err != nil {
|
||||
log.Printf("Unable to read settings.json: %s", err.Error())
|
||||
} else {
|
||||
cur_settings = settings.MergeSettings(*cur_settings, v.Values)
|
||||
|
||||
if err = settings.SaveSettings(path.Join(TmpSettingsDirectory, "settings.json"), cur_settings); err != nil {
|
||||
log.Printf("Unable to save settings.json to tmp dir: %s", err.Error())
|
||||
} else if err = os.Rename(path.Join(TmpSettingsDirectory, "settings.json"), path.Join(settings.SettingsDir, "settings.json")); err != nil {
|
||||
log.Printf("Unable to move settings.json to dest dir: %s", err.Error())
|
||||
} else if err = os.Remove(path.Join(settings.SettingsDir, fmt.Sprintf("%d.json", v.Id))); err != nil {
|
||||
log.Printf("Unable to remove initial diff file (%d.json): %s", v.Id, err.Error())
|
||||
}
|
||||
case <-lSettings.Timer.C:
|
||||
if v := lSettings.Pop(); v != nil {
|
||||
log.Printf("TREATING SETTINGS DIFF: %v", v)
|
||||
treatSettingsFile(v)
|
||||
}
|
||||
lSettings.ResetTimer()
|
||||
case <-lDashboard.Timer.C:
|
||||
if v := lDashboard.Pop(); v != nil {
|
||||
log.Printf("TREATING DASHBOARD DIFF: %v", v)
|
||||
if ndf, ok := v.(*NextDashboardFile); ok {
|
||||
treatDashboardFile(ndf)
|
||||
}
|
||||
}
|
||||
l.ResetTimer()
|
||||
lDashboard.ResetTimer()
|
||||
case ev := <-watcher.Events:
|
||||
if d, err := os.Lstat(ev.Name); err == nil && ev.Op&watchedNotify == watchedNotify && d.Name() != ".tmp" && d.Mode().IsRegular() {
|
||||
if *debugINotify {
|
||||
log.Println("Treating event:", ev, "for", ev.Name)
|
||||
}
|
||||
l.treat(ev.Name)
|
||||
} else if err == nil && ev.Op&watchedNotify == fsnotify.Remove && d.Mode().IsRegular() {
|
||||
treatNewFile(ev.Name)
|
||||
} else if ev.Op&fsnotify.Remove == fsnotify.Remove {
|
||||
if *debugINotify {
|
||||
log.Println("Treating deletion event:", ev, "for", ev.Name)
|
||||
}
|
||||
if ts, err := strconv.ParseInt(strings.TrimSuffix(path.Base(ev.Name), ".json"), 10, 64); err == nil {
|
||||
log.Println("Unable to parseint", ev.Name, err.Error())
|
||||
if strings.HasPrefix(ev.Name, DashboardDir) {
|
||||
if ts, err := parseDashboardFilename(ev.Name); err == nil {
|
||||
log.Println("Unable to parseint", ev.Name, err.Error())
|
||||
} else {
|
||||
lDashboard.DelEvent(ts)
|
||||
}
|
||||
} else {
|
||||
l.DelEvent(ts)
|
||||
if ts, err := parseSettingsFilename(ev.Name); err == nil {
|
||||
log.Println("Unable to parseint", ev.Name, err.Error())
|
||||
} else {
|
||||
lSettings.DelEvent(ts)
|
||||
}
|
||||
}
|
||||
} else if err == nil && ev.Op&fsnotify.Write == fsnotify.Write {
|
||||
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
|
||||
watchedNotify = fsnotify.Write
|
||||
l.treat(ev.Name)
|
||||
treatNewFile(ev.Name)
|
||||
} else if *debugINotify {
|
||||
log.Println("Skipped event:", ev, "for", ev.Name)
|
||||
}
|
||||
|
|
@ -132,50 +146,3 @@ func main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *distList) treat(raw_path string) {
|
||||
bpath := path.Base(raw_path)
|
||||
|
||||
if bpath == "challenge.json" || bpath == "settings.json" {
|
||||
log.Printf("Copying %s to SETTINGDIST...", bpath)
|
||||
// Copy content through tmp file
|
||||
fd, err := os.Open(raw_path)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Unable to open %s: %s", raw_path, err.Error())
|
||||
return
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
tmpfile, err := ioutil.TempFile(TmpSettingsDistDirectory, "")
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Unable to create temporary file for %s: %s", bpath, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(tmpfile, fd)
|
||||
tmpfile.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Unable to copy content to temporary file (%s): %s", bpath, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
os.Chmod(tmpfile.Name(), 0644)
|
||||
|
||||
if err = os.Rename(tmpfile.Name(), path.Join(SettingsDistDir, bpath)); err != nil {
|
||||
log.Println("ERROR: Unable to move file:", err)
|
||||
return
|
||||
}
|
||||
} else if ts, err := strconv.ParseInt(strings.TrimSuffix(bpath, ".json"), 10, 64); err == nil {
|
||||
activateTime := time.Unix(ts, 0)
|
||||
|
||||
log.Printf("Preparing %s: activation time at %s", bpath, activateTime)
|
||||
|
||||
l.AddEvent(&settings.NextSettingsFile{
|
||||
Id: ts,
|
||||
Date: activateTime,
|
||||
})
|
||||
} else {
|
||||
log.Println("WARNING: Unknown file to treat: not a valid timestamp:", err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue