better detect container will restart

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-11-08 12:54:26 +01:00
parent e6599c7213
commit ed2395819d
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
1 changed files with 12 additions and 1 deletions

View File

@ -115,7 +115,7 @@ func (s *composeService) watchContainers(ctx context.Context, projectName string
restarted := watched[container.ID]
watched[container.ID] = restarted + 1
// Container terminated.
willRestart := inspected.HostConfig.RestartPolicy.MaximumRetryCount > restarted
willRestart := willContainerRestart(inspected, restarted)
listener(api.ContainerEvent{
Type: api.ContainerEventExit,
@ -162,3 +162,14 @@ func (s *composeService) watchContainers(ctx context.Context, projectName string
}
return err
}
func willContainerRestart(container moby.ContainerJSON, restarted int) bool {
policy := container.HostConfig.RestartPolicy
if policy.IsAlways() || policy.IsUnlessStopped() {
return true
}
if policy.IsOnFailure() {
return container.State.ExitCode != 0 && policy.MaximumRetryCount > restarted
}
return false
}