@@ -286,9 +286,18 @@ func containerEvents(containers Containers, eventFunc func(string) progress.Even
286
286
return events
287
287
}
288
288
289
+ func containerSkippedEvents (containers Containers , eventFunc func (string , string ) progress.Event , reason string ) []progress.Event {
290
+ events := []progress.Event {}
291
+ for _ , container := range containers {
292
+ events = append (events , eventFunc (getContainerProgressName (container ), reason ))
293
+ }
294
+ return events
295
+ }
296
+
289
297
// ServiceConditionRunningOrHealthy is a service condition on status running or healthy
290
298
const ServiceConditionRunningOrHealthy = "running_or_healthy"
291
299
300
+ //nolint:gocyclo
292
301
func (s * composeService ) waitDependencies (ctx context.Context , project * types.Project , dependencies types.DependsOnConfig , containers Containers ) error {
293
302
eg , _ := errgroup .WithContext (ctx )
294
303
w := progress .ContextWriter (ctx )
@@ -312,6 +321,11 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
312
321
case ServiceConditionRunningOrHealthy :
313
322
healthy , err := s .isServiceHealthy (ctx , waitingFor , true )
314
323
if err != nil {
324
+ if ! config .Required {
325
+ w .Events (containerSkippedEvents (waitingFor , progress .SkippedEvent , fmt .Sprintf ("optional dependency %q is not running or is unhealthy" , dep )))
326
+ logrus .Warnf ("optional dependency %q is not running or is unhealthy: %s" , dep , err .Error ())
327
+ return nil
328
+ }
315
329
return err
316
330
}
317
331
if healthy {
@@ -321,6 +335,11 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
321
335
case types .ServiceConditionHealthy :
322
336
healthy , err := s .isServiceHealthy (ctx , waitingFor , false )
323
337
if err != nil {
338
+ if ! config .Required {
339
+ w .Events (containerSkippedEvents (waitingFor , progress .SkippedEvent , fmt .Sprintf ("optional dependency %q failed to start" , dep )))
340
+ logrus .Warnf ("optional dependency %q failed to start: %s" , dep , err .Error ())
341
+ return nil
342
+ }
324
343
w .Events (containerEvents (waitingFor , progress .ErrorEvent ))
325
344
return errors .Wrap (err , "dependency failed to start" )
326
345
}
@@ -334,6 +353,12 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
334
353
return err
335
354
}
336
355
if exited {
356
+ logMessageSuffix := fmt .Sprintf ("%q didn't complete successfully: exit %d" , dep , code )
357
+ if ! config .Required {
358
+ w .Events (containerSkippedEvents (waitingFor , progress .SkippedEvent , fmt .Sprintf ("optional dependency %s" , logMessageSuffix )))
359
+ logrus .Warnf ("optional dependency %s" , logMessageSuffix )
360
+ return nil
361
+ }
337
362
w .Events (containerEvents (waitingFor , progress .Exited ))
338
363
if code != 0 {
339
364
return fmt .Errorf ("service %q didn't complete successfully: exit %d" , dep , code )
@@ -362,9 +387,6 @@ func shouldWaitForDependency(serviceName string, dependencyConfig types.ServiceD
362
387
return false , nil
363
388
}
364
389
}
365
- if ! dependencyConfig .Required {
366
- return false , nil
367
- }
368
390
return false , err
369
391
} else if service .Scale == 0 {
370
392
// don't wait for the dependency which configured to have 0 containers running
0 commit comments