Skip to content

Commit

Permalink
Only warn when failing to clean up the cache dir
Browse files Browse the repository at this point in the history
  • Loading branch information
espadolini committed Mar 8, 2022
1 parent 489d921 commit 4243228
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,12 +1100,9 @@ func initExternalLog(ctx context.Context, auditConfig types.ClusterAuditConfig,
}

func (process *TeleportProcess) getCacheDir() (string, error) {
process.Lock()
if cacheDir := process.cacheDir; cacheDir != "" {
process.Unlock()
if cacheDir := process.currentCacheDir(); cacheDir != "" {
return cacheDir, nil
}
process.Unlock()

newCacheDir, err := os.MkdirTemp(process.Config.DataDir, "cache-")
if err != nil {
Expand All @@ -1119,7 +1116,7 @@ func (process *TeleportProcess) getCacheDir() (string, error) {
if cacheDir := process.cacheDir; cacheDir != "" {
process.Unlock()
if err := os.Remove(newCacheDir); err != nil {
process.log.WithError(err).Warnf("failed to clean up cache directory %v", newCacheDir)
process.log.WithError(err).Warnf("Failed to clean up cache directory %q.", newCacheDir)
}
return cacheDir, nil
}
Expand All @@ -1129,6 +1126,12 @@ func (process *TeleportProcess) getCacheDir() (string, error) {
return newCacheDir, nil
}

func (process *TeleportProcess) currentCacheDir() string {
process.Lock()
defer process.Unlock()
return process.cacheDir
}

// initAuthService can be called to initialize auth server service
func (process *TeleportProcess) initAuthService() error {
var err error
Expand Down Expand Up @@ -3847,9 +3850,9 @@ func (process *TeleportProcess) StartShutdown(ctx context.Context) context.Conte
}
}

if process.cacheDir != "" {
if cacheDir := process.currentCacheDir(); cacheDir != "" {
if err := os.RemoveAll(process.cacheDir); err != nil {
process.log.Warningf("Failed deleting cache directory: %v.", err)
process.log.WithError(err).Warnf("Failed to clean up cache directory %q.", cacheDir)
}
}
}()
Expand All @@ -3875,16 +3878,19 @@ func (process *TeleportProcess) Close() error {
var errors []error
localAuth := process.getLocalAuth()
if localAuth != nil {
errors = append(errors, process.localAuth.Close())
errors = append(errors, localAuth.Close())
}

if process.storage != nil {
errors = append(errors, process.storage.Close())
}

if process.cacheDir != "" {
if err := os.RemoveAll(process.cacheDir); err != nil {
errors = append(errors, trace.ConvertSystemError(err))
if cacheDir := process.currentCacheDir(); cacheDir != "" {
// services are still running but we've been asked to close as quickly
// as possible, so we'll only give this one attempt and not exit with an
// error in case something created something in the cacheDir
if err := os.RemoveAll(cacheDir); err != nil {
process.log.WithError(err).Warnf("Failed to clean up cache directory %q.", cacheDir)
}
}

Expand Down

0 comments on commit 4243228

Please sign in to comment.