fixup! refactor(container): restart handling
Some checks are pending
ci-build / lint (push) Waiting to run
ci-build / build (push) Blocked by required conditions
ci-build / release (push) Blocked by required conditions

This commit is contained in:
Rob Watson 2025-04-13 10:04:53 +02:00
parent f776a2fe16
commit e39ac23f69
2 changed files with 21 additions and 17 deletions

View File

@ -377,23 +377,30 @@ func (a *Client) runContainerLoop(
respC, errC := a.apiClient.ContainerWait(ctx, containerID, container.WaitConditionNextExit) respC, errC := a.apiClient.ContainerWait(ctx, containerID, container.WaitConditionNextExit)
select { select {
case resp := <-respC: case resp := <-respC:
if shouldRestartFunc != nil { exit := func(err error) {
shouldRestart, err := shouldRestartFunc(resp.StatusCode, restartCount, time.Since(startedWaitingAt)) a.logger.Info("Container exited", "id", shortID(containerID), "should_restart", "false", "exit_code", resp.StatusCode, "restart_count", restartCount)
if shouldRestart && err != nil { containerRespC <- containerWaitResponse{
panic(fmt.Errorf("shouldRestart must return nil error if restarting, but returned: %w", err)) WaitResponse: resp,
} restarting: false,
if !shouldRestart { restartCount: restartCount,
a.logger.Info("Container exited", "id", shortID(containerID), "should_restart", "false", "exit_code", resp.StatusCode, "restart_count", restartCount) err: err,
containerRespC <- containerWaitResponse{
WaitResponse: resp,
restarting: false,
restartCount: restartCount,
err: err,
}
return
} }
} }
if shouldRestartFunc == nil {
exit(nil)
return
}
shouldRestart, err := shouldRestartFunc(resp.StatusCode, restartCount, time.Since(startedWaitingAt))
if shouldRestart && err != nil {
panic(fmt.Errorf("shouldRestart must return nil error if restarting, but returned: %w", err))
}
if !shouldRestart {
exit(err)
return
}
a.logger.Info("Container exited", "id", shortID(containerID), "should_restart", "true", "exit_code", resp.StatusCode, "restart_count", restartCount) a.logger.Info("Container exited", "id", shortID(containerID), "should_restart", "true", "exit_code", resp.StatusCode, "restart_count", restartCount)
timer.Reset(restartInterval) timer.Reset(restartInterval)

View File

@ -94,9 +94,6 @@ func TestClientRunContainer(t *testing.T) {
Mode: 0755, Mode: 0755,
}, },
}, },
ShouldRestart: func(int64, int, time.Duration) (bool, error) {
return false, nil
},
}) })
done := make(chan struct{}) done := make(chan struct{})