Skip to content

Commit

Permalink
t3c remove stale git lock file (#7346)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jpappa200 committed Feb 10, 2023
1 parent f18c5ed commit dad5772
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions cache-config/t3c-apply/t3c-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions cache-config/t3c-apply/util/gitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
Expand Down Expand Up @@ -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
}

0 comments on commit dad5772

Please sign in to comment.