This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new dad57721e2 t3c remove stale git lock file (#7346)
dad57721e2 is described below

commit dad57721e2a82ad0fb5eb1d30ba2ca33542d68ec
Author: Joe Pappano <[email protected]>
AuthorDate: Fri Feb 10 13:34:09 2023 -0500

    t3c remove stale git lock file (#7346)
    
    * added routines to find and remove old git lock files
    
    * updated to find and remove a git lock file older than 5 minutes.
    
    * added CHANGELOG entry
    
    * using filepath instead of string concatenation
    
    * added comment and time units to variable name
    
    * go routine gets file path instead of whole config
    
    * pass file path not config
    
    * fixed formatting issue
---
 CHANGELOG.md                           |  1 +
 cache-config/t3c-apply/t3c-apply.go    | 16 ++++++++++++++++
 cache-config/t3c-apply/util/gitutil.go | 20 ++++++++++++++++++++
 3 files changed, 37 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4205c4f8b1..c12dcd8740 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -80,6 +80,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#7204](https://github.com/apache/trafficcontrol/pull/7204) *Traffic Control 
Cache Config (t3c)* strategies.yaml hash_key only for consistent_hash
 - [#7277](https://github.com/apache/trafficcontrol/pull/7277) *Traffic Control 
Cache Config (t3c)* remapdotconfig: remove skip check at mids for nocache/live
 - [#7282](https://github.com/apache/trafficcontrol/pull/7282) *Traffic Ops* 
Fixed issue with user getting correctly logged when using an access or bearer 
token authentication.
+- [#7346](https://github.com/apache/trafficcontrol/pull/7346) *Traffic Control 
Cache Config (t3c)* Fixed issue with stale lock file when using git to track 
changes.
 
 ## [7.0.0] - 2022-07-19
 ### Added
diff --git a/cache-config/t3c-apply/t3c-apply.go 
b/cache-config/t3c-apply/t3c-apply.go
index 353e24a864..959d9f4a58 100644
--- a/cache-config/t3c-apply/t3c-apply.go
+++ b/cache-config/t3c-apply/t3c-apply.go
@@ -146,6 +146,22 @@ func Main() int {
        }
 
        if cfg.UseGit == config.UseGitYes || cfg.UseGit == config.UseGitAuto {
+               //need to see if there is an old lock file laying around.
+               //older than 5 minutes
+               const gitMaxLockAgeMinutes = 5
+               const gitLock = ".git/index.lock"
+               gitLockFile := filepath.Join(cfg.TsConfigDir, gitLock)
+               oldLock, err := util.IsGitLockFileOld(gitLockFile, time.Now(), 
gitMaxLockAgeMinutes*time.Minute)
+               if err != nil {
+                       log.Errorln("checking for git lock file: " + 
err.Error())
+               }
+               if oldLock {
+                       log.Errorf("removing git lock file older than %dm", 
gitMaxLockAgeMinutes)
+                       err := util.RemoveGitLock(gitLockFile)
+                       if err != nil {
+                               log.Errorf("couldn't remove git lock file: %v", 
err.Error())
+                       }
+               }
                // commit anything someone else changed when we weren't looking,
                // with a keyword indicating it wasn't our change
                if err := util.MakeGitCommitAll(cfg, util.GitChangeNotSelf, 
true); err != nil {
diff --git a/cache-config/t3c-apply/util/gitutil.go 
b/cache-config/t3c-apply/util/gitutil.go
index 3706c3be2a..33d05b0529 100644
--- a/cache-config/t3c-apply/util/gitutil.go
+++ b/cache-config/t3c-apply/util/gitutil.go
@@ -24,6 +24,7 @@ import (
        "errors"
        "fmt"
        "io/ioutil"
+       "os"
        "os/exec"
        "strconv"
        "strings"
@@ -201,3 +202,22 @@ func makeGitCommitMsg(cfg config.Cfg, now time.Time, self 
bool, success bool) st
        const sep = " "
        return strings.Join([]string{appStr, selfStr, modeStr, successStr, 
timeStr}, sep)
 }
+
+func IsGitLockFileOld(lockFile string, now time.Time, maxAge time.Duration) 
(bool, error) {
+       lockFileInfo, err := os.Stat(lockFile)
+       if err != nil {
+               return false, fmt.Errorf("stat returned error: %v on file %v", 
err, lockFile)
+       }
+       if diff := now.Sub(lockFileInfo.ModTime()); diff > maxAge {
+               return true, nil
+       }
+       return false, nil
+}
+
+func RemoveGitLock(lockFile string) error {
+       err := os.Remove(lockFile)
+       if err != nil {
+               return fmt.Errorf("error removing file: %v, %v", lockFile, 
err.Error())
+       }
+       return nil
+}

Reply via email to