remote-challenge-sync-airbus: Add inotify watcher

This commit is contained in:
nemunaire 2022-06-07 17:04:07 +02:00
commit cc1b212cca
3 changed files with 148 additions and 70 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"log"
"os"
"path/filepath"
"time"
@ -10,45 +11,52 @@ import (
)
type Walker struct {
LastSync time.Time
LastSync map[AirbusUserId]time.Time
Exercices AirbusExercicesBindings
Teams map[string]fic.ExportedTeam
API AirbusAPI
Coeff float64
}
func (w *Walker) WalkScore(path string, d os.DirEntry, err error) error {
if filepath.Base(path) == "scores.json" {
mypath := filepath.Join(filepath.Dir(path), "my.json")
if _, err := os.Stat(mypath); !os.IsNotExist(err) {
// Read team ID
fdmy, err := os.Open(mypath)
if err != nil {
return err
}
defer fdmy.Close()
func (w *Walker) treat(path string) {
mypath := filepath.Join(filepath.Dir(path), "my.json")
if _, err := os.Stat(mypath); !os.IsNotExist(err) {
// Read team ID
fdmy, err := os.Open(mypath)
if err != nil {
log.Println("Unable to open my.json:", err)
return
}
defer fdmy.Close()
teammy, err := fic.ReadMyJSON(fdmy)
if err != nil {
return err
}
teammy, err := fic.ReadMyJSON(fdmy)
if err != nil {
log.Println("Unable to parse my.json:", err)
return
}
airbusTeamId := NewAirbusUserId(w.Teams[fmt.Sprintf("%d", teammy.Id)].ExternalId)
airbusTeamId := NewAirbusUserId(w.Teams[fmt.Sprintf("%d", teammy.Id)].ExternalId)
// Treat score grid
err = w.TreatScoreGrid(path, airbusTeamId)
if err != nil {
return err
}
// Treat score grid
err = w.TreatScoreGrid(path, airbusTeamId)
if err != nil {
log.Println("Unable to treat score grid:", err)
return
}
// Balance scores
err = w.BalanceScore(int64(float64(teammy.Points)*w.Coeff), airbusTeamId)
if err != nil {
return err
}
// Balance scores
err = w.BalanceScore(int64(float64(teammy.Points)*w.Coeff), airbusTeamId)
if err != nil {
log.Println("Unable to balance score:", err)
return
}
}
}
func (w *Walker) WalkScore(path string, d os.DirEntry, err error) error {
if filepath.Base(path) == "scores.json" {
go w.treat(path)
}
return nil
}
@ -66,8 +74,12 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeamId AirbusUserId) error {
}
// Found all new entries
maxts := w.LastSync[airbusTeamId]
for _, row := range teamscores {
if row.Time.After(w.LastSync) {
if row.Time.After(maxts) {
maxts = row.Time
}
if row.Time.After(w.LastSync[airbusTeamId]) {
if row.Reason == "Validation" {
err = w.API.ValidateChallengeFromUser(airbusTeamId, w.Exercices[row.IdExercice])
} else {
@ -80,6 +92,8 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeamId AirbusUserId) error {
}
}
w.LastSync[airbusTeamId] = maxts
return nil
}