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 oneDeepSync.Unlock() DeepSyncProgress = 1 errs = map[string][]string{} errs["_date"] = []string{fmt.Sprintf("%v", time.Now())} 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 { DeepSyncProgress = 3 + uint8(tid) * themeStep + uint8(eid) * exerciceStep errs[theme.Name] = append(errs[theme.Name], SyncExerciceFiles(i, exercice)...) DeepSyncProgress += exerciceStep / 3 errs[theme.Name] = append(errs[theme.Name], SyncExerciceFlags(i, exercice)...) DeepSyncProgress += exerciceStep / 3 errs[theme.Name] = append(errs[theme.Name], SyncExerciceHints(i, exercice)...) } } } } 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() fdto.Write([]byte("disp(")) if out, err := json.Marshal(errs); err == nil { fdto.Write(out) } else { errs["_regeneration"] = append(errs["_regeneration"], err.Error()) log.Println(err) } fdto.Write([]byte(");")) } 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 return }