()
| 495 | } |
| 496 | |
| 497 | func (w *Watcher) stopByMethod() error { |
| 498 | // no need singleflight here because it will only be called once every tick. |
| 499 | |
| 500 | // if the container is not running, skip and stop dependencies. |
| 501 | if !w.running() { |
| 502 | if err := w.stopDependencies(); err != nil { |
| 503 | return w.newWatcherError(err) |
| 504 | } |
| 505 | return nil |
| 506 | } |
| 507 | |
| 508 | cfg := w.cfg |
| 509 | ctx, cancel := context.WithTimeout(context.Background(), cfg.StopTimeout) |
| 510 | defer cancel() |
| 511 | |
| 512 | // stop itself first. |
| 513 | var err error |
| 514 | p := w.provider.Load() |
| 515 | if p == nil { |
| 516 | return errors.New("provider not set") |
| 517 | } |
| 518 | switch cfg.StopMethod { |
| 519 | case types.ContainerStopMethodPause: |
| 520 | err = p.ContainerPause(ctx) |
| 521 | case types.ContainerStopMethodStop: |
| 522 | err = p.ContainerStop(ctx, cfg.StopSignal, int(math.Ceil(cfg.StopTimeout.Seconds()))) |
| 523 | case types.ContainerStopMethodKill: |
| 524 | err = p.ContainerKill(ctx, cfg.StopSignal) |
| 525 | default: |
| 526 | err = w.newWatcherError(fmt.Errorf("unexpected stop method: %q", cfg.StopMethod)) |
| 527 | } |
| 528 | |
| 529 | if err != nil { |
| 530 | return w.newWatcherError(err) |
| 531 | } |
| 532 | |
| 533 | w.l.Info().Msg("container stopped") |
| 534 | |
| 535 | // then stop dependencies. |
| 536 | if err := w.stopDependencies(); err != nil { |
| 537 | return w.newWatcherError(err) |
| 538 | } |
| 539 | return nil |
| 540 | } |
| 541 | |
| 542 | func (w *Watcher) resetIdleTimer() { |
| 543 | w.idleTicker.Reset(w.cfg.IdleTimeout) |
no test coverage detected