From 5fb85c22dc3a58af07847cb7866ce9a32c85fbde Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sat, 6 May 2023 02:27:46 +0200 Subject: [PATCH] sync: Don't pull repo when doing synchronization. Do it only on auto-sync --- admin/api/sync.go | 8 ++++++++ admin/sync/full.go | 16 ---------------- admin/sync/importer_git.go | 3 +++ admin/sync/importer_git_common.go | 3 +++ admin/sync/importer_gitbin.go | 3 +++ 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/admin/api/sync.go b/admin/api/sync.go index 3f08f716..6483a249 100644 --- a/admin/api/sync.go +++ b/admin/api/sync.go @@ -205,6 +205,14 @@ func declareSyncExercicesRoutes(router *gin.RouterGroup) { func autoSync(c *gin.Context) { p := strings.Split(strings.TrimPrefix(c.Params.ByName("p"), "/"), "/") + if !IsProductionEnv { + if err := sync.GlobalImporter.Sync(); err != nil { + log.Println("Unable to sync.GI.Sync:", err.Error()) + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to perform the pull."}) + return + } + } + themes, err := fic.GetThemes() if err != nil { log.Println("Unable to GetThemes:", err.Error()) diff --git a/admin/sync/full.go b/admin/sync/full.go index 96d42160..e67be453 100644 --- a/admin/sync/full.go +++ b/admin/sync/full.go @@ -21,17 +21,11 @@ var oneThemeDeepSync sync.Mutex // DeepSyncProgress expose the progression of the depp synchronization (0 = 0%, 255 = 100%). var DeepSyncProgress uint8 -// avoidImporterSync checks if Sync should be called or not. -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 []string `json:"_regeneration"` - SyncError error `json:"_sync,omitempty"` SyncId string `json:"_id,omitempty"` ThemesSync []string `json:"_themes,omitempty"` Themes map[string][]string `json:"themes"` @@ -116,10 +110,6 @@ func SyncDeep(i Importer) (errs SyncReport) { startTime := time.Now() - if err := i.Sync(); err != nil { - errs.SyncError = err - } - errs.DateStart = startTime exceptions, sterrs := SyncThemes(i) for _, sterr := range sterrs { @@ -206,12 +196,6 @@ func SyncThemeDeep(i Importer, theme *fic.Theme, tid int, themeStep uint8, excep oneThemeDeepSync.Lock() defer oneThemeDeepSync.Unlock() - if !avoidImporterSync() { - if err := i.Sync(); err != nil { - errs = append(errs, err) - } - } - DeepSyncProgress = 3 + uint8(tid)*themeStep errs = SyncExercices(i, theme, exceptions) diff --git a/admin/sync/importer_git.go b/admin/sync/importer_git.go index b936c690..0b4f60c0 100644 --- a/admin/sync/importer_git.go +++ b/admin/sync/importer_git.go @@ -107,6 +107,9 @@ func (i GitImporter) Init() error { } func (i GitImporter) Sync() error { + oneGitPull.Lock() + defer oneGitPull.Unlock() + r, err := git.PlainOpen(i.li.Base) if err != nil { return err diff --git a/admin/sync/importer_git_common.go b/admin/sync/importer_git_common.go index 6782c075..87751f78 100644 --- a/admin/sync/importer_git_common.go +++ b/admin/sync/importer_git_common.go @@ -5,10 +5,13 @@ import ( "net/url" "os" "regexp" + "sync" ) var gitRemoteRe = regexp.MustCompile(`^(?:(?:git@|https://)([\w.@]+)(?:/|:))((?:[\w-_]+)/(?:[\w-_/]+))(?:.git){0,1}(?:(?:/){0,1})$`) +var oneGitPull sync.Mutex + func countFileInDir(dirname string) (int, error) { files, err := os.ReadDir(dirname) if err != nil { diff --git a/admin/sync/importer_gitbin.go b/admin/sync/importer_gitbin.go index cde353f3..c8d75a70 100644 --- a/admin/sync/importer_gitbin.go +++ b/admin/sync/importer_gitbin.go @@ -96,6 +96,9 @@ func (i GitImporter) Init() error { } func (i GitImporter) Sync() error { + oneGitPull.Lock() + defer oneGitPull.Unlock() + log.Println("Synchronizing local git repository...") cmdfetch := exec.Command("git", "-C", i.li.Base, "fetch", "origin") stdout, err := cmdfetch.CombinedOutput()