Introduce remote-challenge-sync-airbus
This commit is contained in:
parent
cf502bd9d5
commit
bfdb1c2bf7
6 changed files with 309 additions and 21 deletions
98
remote/challenge-sync-airbus/main.go
Normal file
98
remote/challenge-sync-airbus/main.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"gopkg.in/fsnotify.v1"
|
||||
)
|
||||
|
||||
var (
|
||||
TeamsDir string
|
||||
skipInitialSync bool
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
||||
var debugINotify = flag.Bool("debuginotify", false, "Show skipped inotofy events")
|
||||
flag.BoolVar(&skipInitialSync, "skipinitialsync", skipInitialSync, "Skip the initial synchronization")
|
||||
flag.Parse()
|
||||
|
||||
api := AirbusAPI{
|
||||
BaseURL: "https://portal.european-cybercup.lan/api/v1",
|
||||
}
|
||||
|
||||
if v, exists := os.LookupEnv("AIRBUS_BASEURL"); exists {
|
||||
api.BaseURL = v
|
||||
}
|
||||
if v, exists := os.LookupEnv("AIRBUS_TOKEN"); exists {
|
||||
api.Token = v
|
||||
}
|
||||
if v, exists := os.LookupEnv("AIRBUS_SESSIONID"); exists {
|
||||
var err error
|
||||
api.SessionID, err = strconv.ParseUint(v, 10, 64)
|
||||
if err != nil {
|
||||
log.Fatal("AIRBUS_SESSIONID is invalid: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
log.SetPrefix("[challenge-sync-airbus] ")
|
||||
|
||||
TeamsDir = path.Clean(TeamsDir)
|
||||
|
||||
log.Println("Registering directory events...")
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer watcher.Close()
|
||||
|
||||
if err := watcher.Add(TeamsDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if !skipInitialSync {
|
||||
if _, err := os.Stat(path.Join(TeamsDir, "teams.json")); err == nil {
|
||||
treatAll(path.Join(TeamsDir, "teams.json"))
|
||||
}
|
||||
}
|
||||
|
||||
// Register SIGUSR1, SIGUSR2
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt, syscall.SIGHUP)
|
||||
|
||||
watchedNotify := fsnotify.Create
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in a new issue