remote-challenge-sync-airbus: WIP

This commit is contained in:
nemunaire 2022-06-07 16:06:36 +02:00
commit 367e686e8a
5 changed files with 270 additions and 51 deletions

View file

@ -4,12 +4,10 @@ import (
"flag"
"log"
"os"
"os/signal"
"path"
"path/filepath"
"strconv"
"syscall"
"gopkg.in/fsnotify.v1"
"time"
)
var (
@ -19,8 +17,12 @@ var (
func main() {
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
//var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
flag.BoolVar(&skipInitialSync, "skipinitialsync", skipInitialSync, "Skip the initial synchronization")
//watcher := flag.Bool("watch", false, "Enable daemon mode by watching the directory")
tspath := flag.String("timestamp-file", "./REMOTE/timestamp", "Path to the file storing the last timestamp")
exercicespath := flag.String("exercices-file", "./REMOTE/exercices-bindings.json", "Path to the file containing the ID bindings")
coeff := flag.Float64("global-coeff", 10.0, "Coefficient to use to multiply all scores before passing them to the other platform")
flag.Parse()
api := AirbusAPI{
@ -35,7 +37,7 @@ func main() {
}
if v, exists := os.LookupEnv("AIRBUS_SESSIONID"); exists {
var err error
api.SessionID, err = strconv.ParseUint(v, 10, 64)
api.SessionID, err = strconv.ParseInt(v, 10, 64)
if err != nil {
log.Fatal("AIRBUS_SESSIONID is invalid: ", err.Error())
}
@ -45,54 +47,43 @@ func main() {
TeamsDir = path.Clean(TeamsDir)
log.Println("Registering directory events...")
watcher, err := fsnotify.NewWatcher()
// Load the timestamp
ts, err := loadTS(*tspath)
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
if err := watcher.Add(TeamsDir); err != nil {
log.Fatal(err)
log.Fatal("Unable to open timestamp file: ", err.Error())
}
if !skipInitialSync {
if _, err := os.Stat(path.Join(TeamsDir, "teams.json")); err == nil {
treatAll(path.Join(TeamsDir, "teams.json"))
}
// Load exercices bindings
exbindings, err := ReadExercicesBindings(*exercicespath)
if err != nil {
log.Fatal("Unable to open exercices bindings file: ", err.Error())
}
// Register SIGUSR1, SIGUSR2
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGHUP)
// Load teams.json
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
if err != nil {
log.Fatal("Unable to open teams bindings file: ", err.Error())
}
watchedNotify := fsnotify.Create
w := Walker{
LastSync: *ts,
Exercices: exbindings,
Teams: teamsbindings,
API: api,
Coeff: *coeff,
}
for {
select {
case <-interrupt:
log.Println("SIGHUP received, resyncing all teams' score...")
treatAll(path.Join(TeamsDir, "teams.json"))
log.Println("SIGHUP treated.")
case ev := <-watcher.Events:
if path.Base(ev.Name) == "teams.json" {
if ev.Op&watchedNotify == watchedNotify {
if *debugINotify {
log.Println("Treating event:", ev, "for", ev.Name)
}
go treatDiff(ev.Name)
} else if 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
go treatDiff(ev.Name)
} else if *debugINotify {
log.Println("Skipped teams.json event:", ev)
}
} else if *debugINotify {
log.Println("Skipped NON teams.json event:", ev, "for", ev.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
// Iterate over teams scores
err = filepath.WalkDir(TeamsDir, w.WalkScore)
if err != nil {
log.Printf("Something goes wrong during walking")
}
// Update timestamp for the next time
w.LastSync = time.Now()
err = saveTS(*tspath, &w.LastSync)
if err != nil {
log.Fatal("Unable to save timestamp file: ", err.Error())
}
}