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 88433a9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
44 changes: 34 additions & 10 deletions internal/job/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,33 @@ func (e *Executor) CheckoutPhase(ctx context.Context) error {
roko.WithMaxAttempts(3),
roko.WithStrategy(roko.Constant(2*time.Second)),
).DoWithContext(ctx, func(r *roko.Retrier) error {
err := e.defaultCheckoutPhase(ctx)
if err == nil {

var errLockTimeout ErrTimedOutAcquiringLock

switch err := e.defaultCheckoutPhase(ctx); {
case err == nil:
return nil
}

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

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()
return err

case errors.Is(ctx.Err(), context.Canceled):
e.shell.Warningf("Checkout was cancelled due to context cancellation")
r.Break()
return err

default:
e.shell.Warningf("Checkout failed! %s (%s)", err, r)
Expand Down Expand Up @@ -206,12 +216,9 @@ func (e *Executor) CheckoutPhase(ctx context.Context) error {
// Now make sure the build directory exists again before we try
// to checkout again, or proceed and run hooks which presume the
// checkout dir exists
if err := e.createCheckoutDir(); err != nil {
return err
}
return e.createCheckoutDir()
}

return err
}); err != nil {
return err
}
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 88433a9

Please sign in to comment.