Skip to content

Commit

Permalink
Exit 94 if a mirror lock times out
Browse files Browse the repository at this point in the history
  • Loading branch information
DrJosh9000 committed Oct 3, 2024
1 parent 79ed39a commit ef8d246
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
28 changes: 26 additions & 2 deletions internal/job/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,18 @@ func (e *Executor) CheckoutPhase(ctx context.Context) error {
return nil
}

var errLockTimeout ErrTimedOutAcquiringLock
switch {
case shell.IsExitError(err) && shell.GetExitCode(err) == -1:
e.shell.Warningf("Checkout was interrupted by a signal")
r.Break()

case errors.As(err, &errLockTimeout):
e.shell.Warningf("Checkout could not acquire the %s lock before timing out", errLockTimeout.Name)
r.Break()
// 94 chosen by fair die roll
return &shell.ExitError{Code: 94, Err: err}

case errors.Is(err, context.Canceled):
e.shell.Warningf("Checkout was cancelled")
r.Break()
Expand Down Expand Up @@ -304,7 +311,10 @@ func (e *Executor) updateGitMirror(ctx context.Context, repository string) (stri
defer canc()
mirrorCloneLock, err := e.shell.LockFile(cloneCtx, mirrorDir+".clonelock")
if err != nil {
return "", err
if errors.Is(err, context.DeadlineExceeded) {
return "", ErrTimedOutAcquiringLock{Name: "clone", Err: err}
}
return "", fmt.Errorf("unable to acquire clone lock: %w", err)
}
defer mirrorCloneLock.Unlock()

Expand Down Expand Up @@ -343,7 +353,10 @@ func (e *Executor) updateGitMirror(ctx context.Context, repository string) (stri
defer canc()
mirrorUpdateLock, err := e.shell.LockFile(updateCtx, mirrorDir+".updatelock")
if err != nil {
return "", err
if errors.Is(err, context.DeadlineExceeded) {
return "", ErrTimedOutAcquiringLock{Name: "update", Err: err}
}
return "", fmt.Errorf("unable to acquire update lock: %w", err)
}
defer mirrorUpdateLock.Unlock()

Expand Down Expand Up @@ -407,6 +420,17 @@ func (e *Executor) updateGitMirror(ctx context.Context, repository string) (stri
return mirrorDir, nil
}

type ErrTimedOutAcquiringLock struct {
Name string
Err error
}

func (e ErrTimedOutAcquiringLock) Error() string {
return fmt.Sprintf("timed out acquiring %s lock: %v", e.Name, e.Err)
}

func (e ErrTimedOutAcquiringLock) Unwrap() error { return e.Err }

// updateRemoteURL updates the URL for 'origin'. If gitDir == "", it assumes the
// local repo is in the current directory, otherwise it includes --git-dir.
// If the remote has changed, it logs some extra information. updateRemoteURL
Expand Down
4 changes: 2 additions & 2 deletions internal/job/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ func (e *Executor) runWrappedShellScriptHook(ctx context.Context, hookName strin
// so it may inform the Buildkite API
if shell.IsExitError(err) {
return &shell.ExitError{
Code: exitCode,
Message: fmt.Sprintf("The %s hook exited with status %d", hookName, exitCode),
Code: exitCode,
Err: fmt.Errorf("The %s hook exited with status %d", hookName, exitCode),
}
}

Expand Down
10 changes: 5 additions & 5 deletions internal/job/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,11 @@ func IsExitError(err error) bool {

// ExitError is an error that carries a shell exit code
type ExitError struct {
Code int
Message string
Code int
Err error
}

// Error returns the string message and fulfils the error interface
func (ee *ExitError) Error() string {
return ee.Message
}
func (ee *ExitError) Error() string { return ee.Err.Error() }

func (ee *ExitError) Unwrap() error { return ee.Err }

0 comments on commit ef8d246

Please sign in to comment.