Skip to content

Commit c73b51b

Browse files
committed
Add retry on locks
1 parent 346a4d8 commit c73b51b

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

server/events/working_dir_locker.go

+28-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"strings"
1919
"sync"
20+
"time"
2021
)
2122

2223
//go:generate pegomock generate --package mocks -o mocks/mock_working_dir_locker.go WorkingDirLocker
@@ -78,19 +79,35 @@ func (d *DefaultWorkingDirLocker) TryLock(repoFullName string, pullNum int, work
7879
d.mutex.Lock()
7980
defer d.mutex.Unlock()
8081

81-
pullKey := d.pullKey(repoFullName, pullNum)
82-
workspaceKey := d.workspaceKey(repoFullName, pullNum, workspace, path)
83-
for _, l := range d.locks {
84-
if l == pullKey || l == workspaceKey {
85-
return func() {}, fmt.Errorf("the %s workspace at path %s is currently locked by another"+
86-
" command that is running for this pull request.\n"+
87-
"Wait until the previous command is complete and try again", workspace, path)
82+
ticker := time.NewTicker(time.Second)
83+
timeout := time.NewTimer(600 * time.Second)
84+
addLock := false
85+
86+
for {
87+
workspaceKey := d.workspaceKey(repoFullName, pullNum, workspace, path)
88+
89+
if addLock || len(d.locks) == 0 {
90+
d.locks = append(d.locks, workspaceKey)
91+
return func() {
92+
d.unlock(repoFullName, pullNum, workspace, path)
93+
}, nil
8894
}
95+
96+
select {
97+
case <-timeout.C:
98+
return func() {}, fmt.Errorf("the Atlantis working dir is currently locked by another" +
99+
" command that is running for this pull request.\n" +
100+
"Wait until the previous command is complete and try again")
101+
case <-ticker.C:
102+
for _, l := range d.locks {
103+
pullKey := d.pullKey(repoFullName, pullNum)
104+
if l != pullKey && l != workspaceKey {
105+
addLock = true
106+
}
107+
}
108+
}
109+
89110
}
90-
d.locks = append(d.locks, workspaceKey)
91-
return func() {
92-
d.unlock(repoFullName, pullNum, workspace, path)
93-
}, nil
94111
}
95112

96113
// Unlock unlocks the workspace for this pull.

0 commit comments

Comments
 (0)