mirror of https://github.com/portainer/portainer
feat(stacks): simplify WaitForStatus() BE-11505 (#241)
parent
35dcb5ca46
commit
13317ec43c
|
@ -59,7 +59,7 @@ services:
|
|||
|
||||
require.True(t, containerExists(composeContainerName))
|
||||
|
||||
waitResult := <-w.WaitForStatus(ctx, projectName, libstack.StatusCompleted)
|
||||
waitResult := w.WaitForStatus(ctx, projectName, libstack.StatusCompleted)
|
||||
|
||||
require.Empty(t, waitResult.ErrorMsg)
|
||||
require.Equal(t, libstack.StatusCompleted, waitResult.Status)
|
||||
|
|
|
@ -111,17 +111,14 @@ func aggregateStatuses(services []service) (libstack.Status, string) {
|
|||
|
||||
}
|
||||
|
||||
func (c *ComposeDeployer) WaitForStatus(ctx context.Context, name string, status libstack.Status) <-chan libstack.WaitResult {
|
||||
waitResultCh := make(chan libstack.WaitResult)
|
||||
func (c *ComposeDeployer) WaitForStatus(ctx context.Context, name string, status libstack.Status) libstack.WaitResult {
|
||||
waitResult := libstack.WaitResult{Status: status}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if ctx.Err() != nil {
|
||||
waitResult.ErrorMsg = "failed to wait for status: " + ctx.Err().Error()
|
||||
waitResultCh <- waitResult
|
||||
default:
|
||||
|
||||
return waitResult
|
||||
}
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
@ -148,26 +145,24 @@ func (c *ComposeDeployer) WaitForStatus(ctx context.Context, name string, status
|
|||
services := serviceListFromContainerSummary(containerSummaries)
|
||||
|
||||
if len(services) == 0 && status == libstack.StatusRemoved {
|
||||
waitResultCh <- waitResult
|
||||
return
|
||||
return waitResult
|
||||
}
|
||||
|
||||
aggregateStatus, errorMessage := aggregateStatuses(services)
|
||||
if aggregateStatus == status {
|
||||
waitResultCh <- waitResult
|
||||
return
|
||||
return waitResult
|
||||
}
|
||||
|
||||
if status == libstack.StatusRunning && aggregateStatus == libstack.StatusCompleted {
|
||||
waitResult.Status = libstack.StatusCompleted
|
||||
waitResultCh <- waitResult
|
||||
return
|
||||
|
||||
return waitResult
|
||||
}
|
||||
|
||||
if errorMessage != "" {
|
||||
waitResult.ErrorMsg = errorMessage
|
||||
waitResultCh <- waitResult
|
||||
return
|
||||
|
||||
return waitResult
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
|
@ -176,9 +171,6 @@ func (c *ComposeDeployer) WaitForStatus(ctx context.Context, name string, status
|
|||
Str("status", string(aggregateStatus)).
|
||||
Msg("waiting for status")
|
||||
}
|
||||
}()
|
||||
|
||||
return waitResultCh
|
||||
}
|
||||
|
||||
func serviceListFromContainerSummary(containerSummaries []api.ContainerSummary) []service {
|
||||
|
|
|
@ -106,8 +106,7 @@ func waitForStatus(deployer libstack.Deployer, ctx context.Context, stackName st
|
|||
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
statusCh := deployer.WaitForStatus(ctx, stackName, requiredStatus)
|
||||
result := <-statusCh
|
||||
result := deployer.WaitForStatus(ctx, stackName, requiredStatus)
|
||||
if result.ErrorMsg == "" {
|
||||
return result.Status, "", nil
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ type Deployer interface {
|
|||
Pull(ctx context.Context, filePaths []string, options Options) error
|
||||
Run(ctx context.Context, filePaths []string, serviceName string, options RunOptions) error
|
||||
Validate(ctx context.Context, filePaths []string, options Options) error
|
||||
WaitForStatus(ctx context.Context, name string, status Status) <-chan WaitResult
|
||||
WaitForStatus(ctx context.Context, name string, status Status) WaitResult
|
||||
Config(ctx context.Context, filePaths []string, options Options) ([]byte, error)
|
||||
GetExistingEdgeStacks(ctx context.Context) ([]EdgeStack, error)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue