server/admin/sync/full.go

96 lines
2.7 KiB
Go
Raw Normal View History

2017-12-09 09:54:16 +00:00
package sync
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"sync"
2017-12-09 09:54:16 +00:00
"srs.epita.fr/fic-server/libfic"
"srs.epita.fr/fic-server/settings"
2017-12-09 09:54:16 +00:00
)
// DeepReportPath stores the path to the report generated during full recursive import.
var DeepReportPath = "full_import_report.json"
2018-05-11 23:08:37 +00:00
// 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
2018-05-11 23:08:37 +00:00
// SyncDeep performs a recursive synchronisation: from themes to challenge items.
2018-01-08 00:34:22 +00:00
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
2018-01-08 00:34:22 +00:00
errs = map[string][]string{}
2017-12-09 09:54:16 +00:00
startTime := time.Now()
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
2017-12-09 09:54:16 +00:00
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
2017-12-09 09:54:16 +00:00
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
2017-12-09 09:54:16 +00:00
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...)
2017-12-09 09:54:16 +00:00
}
}
}
}
DeepSyncProgress = 254
errs["_date"] = append(errs["_date"], fmt.Sprintf("%v", time.Now()))
2018-01-08 00:34:22 +00:00
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 {
2018-01-08 00:34:22 +00:00
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
log.Println(err)
}
} else {
2018-01-08 00:34:22 +00:00
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))
2018-01-08 00:34:22 +00:00
return
2017-12-09 09:54:16 +00:00
}