Commit 96f44d6c
96f44d6cacdaMerged
Svyatoslav <[email protected]> ยท 5 years ago ยท 1 file ยท ~1 modified
Description
Refactor a bit repo update
Details
- Commit
- 96f44d6cacda
- Parent
- d9e6d048bee7
pkg/repo/helm.gomodifiedView file
@@ -13,10 +13,16 @@
1313
"time"1414
)1515
16+
const (17+
//flockTimeout is timeout for repo flock18+
flockTimeout = 30 * time.Second19+
)20+
1621
func (rep *Config) Install(settings *helm.EnvSettings) error {1722
return Write(settings.RepositoryConfig, &rep.Entry, settings)1823
}1924
25+
// Write updates given repository name if it exists in helm repository2026
// TODO it better later2127
func Write(repofile string, o *repo.Entry, helm *helm.EnvSettings) error {2228
//Ensure the file directory exists as it is required for file locking@@ -25,21 +31,28 @@
2531
return err2632
}2733
34+
flockPath := strings.Replace(repofile, filepath.Ext(repofile), ".lock", 1)2835
// Acquire a file lock for process synchronization29-
fileLock := flock.New(strings.Replace(repofile, filepath.Ext(repofile), ".lock", 1))30-
lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)36+
fileLock := flock.New(flockPath)37+
lockCtx, cancel := context.WithTimeout(context.Background(), flockTimeout)3138
defer cancel()3239
3340
locked, err := fileLock.TryLockContext(lockCtx, time.Second)34-
if err == nil && locked {35-
defer func(fileLock *flock.Flock) {36-
err := fileLock.Unlock()37-
if err != nil {38-
log.Errorf("Failed to release flock: %v", err.Error())39-
}40-
}(fileLock)41+
if err != nil {42+
log.Errorf("Failed to get flock: %v", err.Error())43+
return err44+
}45+
if !locked {46+
log.Errorf("Flock is not locked")4147
}4248
49+
defer func(fileLock *flock.Flock) {50+
err := fileLock.Unlock()51+
if err != nil {52+
log.Errorf("Failed to release flock: %v", err.Error())53+
}54+
}(fileLock)55+
4356
f, err := repo.LoadFile(repofile)4457
if err != nil {4558
return err