diff --git a/containerm/mgr/mgr.go b/containerm/mgr/mgr.go index a7cafa0..61191e1 100644 --- a/containerm/mgr/mgr.go +++ b/containerm/mgr/mgr.go @@ -269,7 +269,7 @@ func (mgr *containerMgr) Stop(ctx context.Context, id string, stopOpts *types.St } container.Lock() defer container.Unlock() - go mgr.applyRestartPolicy(context.Background(), container) + mgr.applyRestartPolicy(context.Background(), container) return nil } @@ -312,10 +312,11 @@ func (mgr *containerMgr) Update(ctx context.Context, id string, updateOpts *type changesMade = true } + var rpChanged bool if updateOpts.RestartPolicy != nil && !reflect.DeepEqual(updateOpts.RestartPolicy, container.HostConfig.RestartPolicy) { mgr.resetContainerRestartManager(container, false) container.HostConfig.RestartPolicy = updateOpts.RestartPolicy - changesMade = true + changesMade, rpChanged = true, true } if changesMade { @@ -327,6 +328,10 @@ func (mgr *containerMgr) Update(ctx context.Context, id string, updateOpts *type log.ErrorErr(errMeta, failedConfigStoringErrorMsg) } } + + if rpChanged && (container.State.Exited || container.State.Status == types.Stopped) { + mgr.applyRestartPolicy(context.Background(), container) + } return nil } diff --git a/containerm/mgr/mgr_internal.go b/containerm/mgr/mgr_internal.go index 74437b5..382f525 100644 --- a/containerm/mgr/mgr_internal.go +++ b/containerm/mgr/mgr_internal.go @@ -96,7 +96,7 @@ func (mgr *containerMgr) applyRestartPolicy(ctx context.Context, container *type } } if err != nil { - mgr.updateConfigToStopped(ctx, container, -1, err) + mgr.updateConfigToStopped(ctx, container, -1, err, false) } }() } @@ -132,7 +132,7 @@ func (mgr *containerMgr) containersToArray() []*types.Container { } return ctrs } -func (mgr *containerMgr) updateConfigToStopped(ctx context.Context, c *types.Container, exitCode int64, err error) error { +func (mgr *containerMgr) updateConfigToStopped(ctx context.Context, c *types.Container, exitCode int64, err error, releaseContainerResources bool) error { var ( code int64 errMsg string @@ -155,7 +155,10 @@ func (mgr *containerMgr) updateConfigToStopped(ctx context.Context, c *types.Con } }() - return mgr.releaseContainerResources(c) + if releaseContainerResources { + return mgr.releaseContainerResources(c) + } + return nil } func (mgr *containerMgr) updateConfigToExited(ctx context.Context, c *types.Container, exitCode int64, err error, oomKilled bool) error { @@ -298,7 +301,7 @@ func (mgr *containerMgr) processStartContainer(ctx context.Context, id string, r pid, err = mgr.ctrClient.StartContainer(ctx, container, "") if err != nil { - _ = mgr.updateConfigToStopped(ctx, container, -1, err) + _ = mgr.updateConfigToStopped(ctx, container, -1, err, true) return err } @@ -361,7 +364,7 @@ func (mgr *containerMgr) stopContainer(ctx context.Context, container *types.Con container.ManuallyStopped = false return exitErr } - return mgr.updateConfigToStopped(ctx, container, exitCode, exitErr) + return mgr.updateConfigToStopped(ctx, container, exitCode, exitErr, true) } func (mgr *containerMgr) stopManagerService(ctx context.Context) error {