Skip to content

Commit

Permalink
Add retry on locks
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabianoshz committed Oct 11, 2024
1 parent 346a4d8 commit c76ac47
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions server/events/working_dir_locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"strings"
"sync"
"time"
)

//go:generate pegomock generate --package mocks -o mocks/mock_working_dir_locker.go WorkingDirLocker
Expand Down Expand Up @@ -78,19 +79,33 @@ func (d *DefaultWorkingDirLocker) TryLock(repoFullName string, pullNum int, work
d.mutex.Lock()
defer d.mutex.Unlock()

pullKey := d.pullKey(repoFullName, pullNum)
workspaceKey := d.workspaceKey(repoFullName, pullNum, workspace, path)
for _, l := range d.locks {
if l == pullKey || l == workspaceKey {
return func() {}, fmt.Errorf("the %s workspace at path %s is currently locked by another"+
" command that is running for this pull request.\n"+
"Wait until the previous command is complete and try again", workspace, path)
ticker := time.NewTicker(time.Second)
timeout := time.NewTimer(600 * time.Second)
addLock := len(d.locks) == 0

for {
if addLock {
return func() {
d.unlock(repoFullName, pullNum, workspace, path)
}, nil
}

select {
case <-timeout.C:
return func() {}, fmt.Errorf("the Atlantis working dir is currently locked by another" +
" command that is running for this pull request.\n" +
"Wait until the previous command is complete and try again")
case <-ticker.C:
pullKey := d.pullKey(repoFullName, pullNum)
workspaceKey := d.workspaceKey(repoFullName, pullNum, workspace, path)
for _, l := range d.locks {
if l != pullKey && l != workspaceKey {
addLock = true
}
}
}

}
d.locks = append(d.locks, workspaceKey)
return func() {
d.unlock(repoFullName, pullNum, workspace, path)
}, nil
}

// Unlock unlocks the workspace for this pull.
Expand Down

0 comments on commit c76ac47

Please sign in to comment.