server/admin/sync/full.go

96 lines
2.7 KiB
Go

package sync
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"sync"
"srs.epita.fr/fic-server/libfic"
"srs.epita.fr/fic-server/settings"
)
// DeepReportPath stores the path to the report generated during full recursive import.
var DeepReportPath = "full_import_report.json"
// oneDeepSync ensure there is no more than one running deep sync.
var oneDeepSync sync.Mutex
// DeepSyncProgress expose the progression of the depp synchronization (0 = 0%, 255 = 100%).
var DeepSyncProgress uint8
// SyncDeep performs a recursive synchronisation: from themes to challenge items.
func SyncDeep(i Importer) (errs map[string][]string) {
oneDeepSync.Lock()
defer func(){
oneDeepSync.Unlock()
if DeepSyncProgress != 255 {
log.Printf("Full synchronization terminated at step %d/255", DeepSyncProgress)
}
}()
DeepSyncProgress = 1
errs = map[string][]string{}
startTime := time.Now()
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
errs["_themes"] = SyncThemes(i)
if themes, err := fic.GetThemes(); err == nil {
DeepSyncProgress = 2
var themeStep uint8 = uint8(250) / uint8(len(themes))
for tid, theme := range themes {
DeepSyncProgress = 3 + uint8(tid) * themeStep
errs[theme.Name] = SyncExercices(i, theme)
if exercices, err := theme.GetExercices(); err == nil {
var exerciceStep uint8 = themeStep / uint8(len(exercices))
for eid, exercice := range exercices {
log.Printf("Full synchronization in progress: %d/255 - doing Theme %q, Exercice %q: %q\n", DeepSyncProgress, theme.Name, exercice.Title, exercice.Path)
DeepSyncProgress = 3 + uint8(tid) * themeStep + uint8(eid) * exerciceStep
errs[theme.Name] = append(errs[theme.Name], SyncExerciceFiles(i, exercice)...)
DeepSyncProgress += exerciceStep / 3
flagsBindings, ferrs := SyncExerciceFlags(i, exercice)
errs[theme.Name] = append(errs[theme.Name], ferrs...)
DeepSyncProgress += exerciceStep / 3
_, herrs := SyncExerciceHints(i, exercice, flagsBindings)
errs[theme.Name] = append(errs[theme.Name], herrs...)
}
}
}
}
DeepSyncProgress = 254
errs["_date"] = append(errs["_date"], fmt.Sprintf("%v", time.Now()))
errs["_regeneration"] = []string{}
if fdto, err := os.Create(DeepReportPath); err == nil {
defer fdto.Close()
if out, err := json.Marshal(errs); err == nil {
fdto.Write(out)
} else {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
log.Println(err)
}
} else {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
log.Println(err)
}
if err := settings.ForceRegeneration(); err != nil {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
}
DeepSyncProgress = 255
log.Println("Full synchronization done in", time.Since(startTime))
return
}