Skip to content

Commit

Permalink
Merge branch 'main' into feat/export-command
Browse files Browse the repository at this point in the history
  • Loading branch information
jarqvi authored Sep 24, 2024
2 parents 4776a0b + 8b9fe89 commit 4569bac
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
17 changes: 13 additions & 4 deletions pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,12 @@ func containerReasonEvents(containers Containers, eventFunc func(string, string)
const ServiceConditionRunningOrHealthy = "running_or_healthy"

//nolint:gocyclo
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, dependant string, dependencies types.DependsOnConfig, containers Containers) error {
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, dependant string, dependencies types.DependsOnConfig, containers Containers, timeout time.Duration) error {
if timeout > 0 {
withTimeout, cancelFunc := context.WithTimeout(ctx, timeout)
defer cancelFunc()
ctx = withTimeout
}
eg, _ := errgroup.WithContext(ctx)
w := progress.ContextWriter(ctx)
for dep, config := range dependencies {
Expand Down Expand Up @@ -454,7 +459,11 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
}
})
}
return eg.Wait()
err := eg.Wait()
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("timeout waiting for dependencies")
}
return err
}

func shouldWaitForDependency(serviceName string, dependencyConfig types.ServiceDependency, project *types.Project) (bool, error) {
Expand Down Expand Up @@ -760,12 +769,12 @@ func (s *composeService) isServiceCompleted(ctx context.Context, containers Cont
return false, 0, nil
}

func (s *composeService) startService(ctx context.Context, project *types.Project, service types.ServiceConfig, containers Containers) error {
func (s *composeService) startService(ctx context.Context, project *types.Project, service types.ServiceConfig, containers Containers, timeout time.Duration) error {
if service.Deploy != nil && service.Deploy.Replicas != nil && *service.Deploy.Replicas == 0 {
return nil
}

err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, containers)
err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, containers, timeout)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/compose/convergence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func TestWaitDependencies(t *testing.T) {
"db": {Condition: ServiceConditionRunningOrHealthy},
"redis": {Condition: ServiceConditionRunningOrHealthy},
}
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil, 0))
})
t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
dbService := types.ServiceConfig{Name: "db", Scale: intPtr(1)}
Expand All @@ -252,7 +252,7 @@ func TestWaitDependencies(t *testing.T) {
"db": {Condition: types.ServiceConditionStarted, Required: true},
"redis": {Condition: types.ServiceConditionStarted, Required: true},
}
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil, 0))
})
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,11 +1003,18 @@ func buildContainerSecretMounts(p types.Project, s types.ServiceConfig) ([]mount
continue
}

if _, err := os.Stat(definedSecret.File); os.IsNotExist(err) {
logrus.Warnf("secret file %s does not exist", definedSecret.Name)
}

mnt, err := buildMount(p, types.ServiceVolumeConfig{
Type: types.VolumeTypeBind,
Source: definedSecret.File,
Target: target,
ReadOnly: true,
Bind: &types.ServiceVolumeBind{
CreateHostPath: false,
},
})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (p *printer) Run(cascade api.Cascade, exitCodeFrom string, stopFn func() er
case api.UserCancel:
aborting = true
case api.ContainerEventAttach:
if _, ok := containers[id]; ok {
if attached, ok := containers[id]; ok && attached {
continue
}
containers[id] = true
Expand Down
2 changes: 1 addition & 1 deletion pkg/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project,
}

if !opts.NoDeps {
if err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, observedState); err != nil {
if err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, observedState, 0); err != nil {
return "", err
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/compose/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
return err
}

return s.startService(ctx, project, service, containers)
return s.startService(ctx, project, service, containers, options.WaitTimeout)
})
if err != nil {
return err
Expand All @@ -149,7 +149,7 @@ func (s *composeService) start(ctx context.Context, projectName string, options
defer cancel()
}

err = s.waitDependencies(ctx, project, project.Name, depends, containers)
err = s.waitDependencies(ctx, project, project.Name, depends, containers, 0)
if err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return fmt.Errorf("application not healthy after %s", options.WaitTimeout)
Expand Down

0 comments on commit 4569bac

Please sign in to comment.