sync: Use errors instead of string to report

This commit is contained in:
nemunaire 2022-07-02 00:01:05 +02:00
parent d8943ba1f3
commit b0129e5239
9 changed files with 186 additions and 178 deletions

View file

@ -2,7 +2,6 @@ package sync
import (
"encoding/json"
"fmt"
"log"
"os"
"sync"
@ -27,8 +26,19 @@ func avoidImporterSync() bool {
return DeepSyncProgress > 1 && DeepSyncProgress < 255
}
type SyncReport struct {
DateStart time.Time `json:"_started"`
DateEnd time.Time `json:"_ended"`
DateUpdated []time.Time `json:"_updated"`
Regeneration []error `json:"_regeneration"`
SyncError error `json:"_sync,omitempty"`
SyncId string `json:"_id,omitempty"`
ThemesSync []error `json:"_themes,omitempty"`
Themes map[string][]error `json:"themes"`
}
// SpeedySyncDeep performs a recursive synchronisation without importing files.
func SpeedySyncDeep(i Importer) (errs map[string][]string) {
func SpeedySyncDeep(i Importer) (errs SyncReport) {
oneDeepSync.Lock()
defer func() {
oneDeepSync.Unlock()
@ -38,19 +48,19 @@ func SpeedySyncDeep(i Importer) (errs map[string][]string) {
}()
DeepSyncProgress = 1
errs = map[string][]string{}
errs.Themes = map[string][]error{}
startTime := time.Now()
if err := i.Sync(); err != nil {
errs["_sync"] = []string{err.Error()}
errs.SyncError = err
if _id := i.Id(); _id != nil {
errs["_sync"] = append(errs["_sync"], *_id)
errs.SyncId = *_id
}
}
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
errs["_themes"] = SyncThemes(i)
errs.DateStart = startTime
errs.ThemesSync = SyncThemes(i)
if themes, err := fic.GetThemes(); err == nil {
DeepSyncProgress = 2
@ -58,7 +68,7 @@ func SpeedySyncDeep(i Importer) (errs map[string][]string) {
for tid, theme := range themes {
DeepSyncProgress = 3 + uint8(tid)*themeStep
errs[theme.Name] = SyncExercices(i, theme)
errs.Themes[theme.Name] = SyncExercices(i, theme)
if exercices, err := theme.GetExercices(); err == nil {
if len(exercices) == 0 {
@ -70,18 +80,18 @@ func SpeedySyncDeep(i Importer) (errs map[string][]string) {
DeepSyncProgress = 3 + uint8(tid)*themeStep + uint8(eid)*exerciceStep
flagsBindings, ferrs := SyncExerciceFlags(i, exercice)
errs[theme.Name] = append(errs[theme.Name], ferrs...)
errs.Themes[theme.Name] = append(errs.Themes[theme.Name], ferrs...)
DeepSyncProgress += exerciceStep / 2
_, herrs := SyncExerciceHints(i, exercice, flagsBindings)
errs[theme.Name] = append(errs[theme.Name], herrs...)
errs.Themes[theme.Name] = append(errs.Themes[theme.Name], herrs...)
}
}
}
}
DeepSyncProgress = 254
errs["_date"] = append(errs["_date"], fmt.Sprintf("%v", time.Now()))
errs.DateEnd = time.Now()
DeepSyncProgress = 255
log.Println("Speedy synchronization done in", time.Since(startTime))
@ -89,7 +99,7 @@ func SpeedySyncDeep(i Importer) (errs map[string][]string) {
}
// SyncDeep performs a recursive synchronisation: from themes to challenge items.
func SyncDeep(i Importer) (errs map[string][]string) {
func SyncDeep(i Importer) (errs SyncReport) {
oneDeepSync.Lock()
defer func() {
oneDeepSync.Unlock()
@ -99,32 +109,32 @@ func SyncDeep(i Importer) (errs map[string][]string) {
}()
DeepSyncProgress = 1
errs = map[string][]string{}
errs.Themes = map[string][]error{}
startTime := time.Now()
if err := i.Sync(); err != nil {
errs["_sync"] = []string{err.Error()}
errs.SyncError = err
}
errs["_date"] = []string{fmt.Sprintf("%v", startTime)}
errs["_themes"] = SyncThemes(i)
errs.DateStart = startTime
errs.ThemesSync = SyncThemes(i)
if themes, err := fic.GetThemes(); err == nil && len(themes) > 0 {
DeepSyncProgress = 2
var themeStep uint8 = uint8(250) / uint8(len(themes))
for tid, theme := range themes {
errs[theme.Name] = SyncThemeDeep(i, theme, tid, themeStep)
errs.Themes[theme.Name] = SyncThemeDeep(i, theme, tid, themeStep)
}
}
DeepSyncProgress = 254
EditDeepReport(errs, true)
EditDeepReport(&errs, true)
if err := settings.ForceRegeneration(); err != nil {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
errs.Regeneration = append(errs.Regeneration, err)
}
DeepSyncProgress = 255
@ -132,7 +142,7 @@ func SyncDeep(i Importer) (errs map[string][]string) {
return
}
func readDeepReport() (ret map[string][]string, err error) {
func readDeepReport() (ret *SyncReport, err error) {
if fdfrom, err := os.Open(DeepReportPath); err == nil {
defer fdfrom.Close()
@ -148,25 +158,23 @@ func readDeepReport() (ret map[string][]string, err error) {
return
}
func EditDeepReport(errs map[string][]string, erase bool) {
errs["_regeneration"] = []string{}
func EditDeepReport(errs *SyncReport, erase bool) {
errs.Regeneration = []error{}
if !erase {
if in, err := readDeepReport(); err != nil {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
errs.Regeneration = append(errs.Regeneration, err)
log.Println(err)
} else {
for k, v := range errs {
in[k] = v
for k, v := range errs.Themes {
in.Themes[k] = v
}
errs = in
}
}
if _, ok := errs["_date"]; ok {
errs["_date"] = append(errs["_date"], fmt.Sprintf("%v", time.Now()))
}
errs.DateUpdated = append(errs.DateUpdated, time.Now())
if fdto, err := os.Create(DeepReportPath); err == nil {
defer fdto.Close()
@ -174,24 +182,24 @@ func EditDeepReport(errs map[string][]string, erase bool) {
if out, err := json.Marshal(errs); err == nil {
fdto.Write(out)
} else {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
errs.Regeneration = append(errs.Regeneration, err)
log.Println(err)
}
} else {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
errs.Regeneration = append(errs.Regeneration, err)
log.Println(err)
}
}
// SyncThemeDeep performs a recursive synchronisation: from challenges to challenge items.
func SyncThemeDeep(i Importer, theme *fic.Theme, tid int, themeStep uint8) (errs []string) {
func SyncThemeDeep(i Importer, theme *fic.Theme, tid int, themeStep uint8) (errs []error) {
oneThemeDeepSync.Lock()
defer oneThemeDeepSync.Unlock()
if !avoidImporterSync() {
if err := i.Sync(); err != nil {
errs = append(errs, err.Error())
errs = append(errs, err)
}
}