Skip to content

Commit

Permalink
poc: allow stopping services that are starting and restart services t…
Browse files Browse the repository at this point in the history
…hat quickly exist within the 1s okay delay
  • Loading branch information
IronCore864 committed Sep 24, 2024
1 parent 0ca17af commit 2c66795
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
16 changes: 16 additions & 0 deletions internals/overlord/servstate/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,12 @@ func (s *serviceData) exited(exitCode int) error {
case stateStarting:
s.started <- fmt.Errorf("exited quickly with code %d", exitCode)
s.transition(stateExited) // not strictly necessary as doStart will return, but doesn't hurt
action, onType := getAction(s.config, exitCode == 0)
switch action {
case plan.ActionRestart:
s.doBackoff(action, onType)
default:
}

case stateRunning:
logger.Noticef("Service %q stopped unexpectedly with code %d", s.config.Name, exitCode)
Expand Down Expand Up @@ -682,6 +688,16 @@ func (s *serviceData) stop() error {
defer s.manager.servicesLock.Unlock()

switch s.state {
case stateStarting:
s.started <- fmt.Errorf("stopped before the 1 second okay delay")
logger.Debugf("Attempting to stop service %q by sending SIGTERM", s.config.Name)
err := syscall.Kill(-s.cmd.Process.Pid, syscall.SIGTERM)
if err != nil {
logger.Noticef("Cannot send SIGTERM to process: %v", err)
}
s.transition(stateTerminating)
time.AfterFunc(s.killDelay(), func() { logError(s.terminateTimeElapsed()) })

case stateRunning:
logger.Debugf("Attempting to stop service %q by sending SIGTERM", s.config.Name)
// First send SIGTERM to try to terminate it gracefully.
Expand Down
2 changes: 1 addition & 1 deletion internals/overlord/servstate/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func servicesToStop(m *ServiceManager) ([][]string, error) {
var notStopped []string
for _, name := range services {
s := m.services[name]
if s != nil && (s.state == stateRunning || s.state == stateBackoff) {
if s != nil && (s.state == stateStarting || s.state == stateRunning || s.state == stateBackoff) {
notStopped = append(notStopped, name)
}
}
Expand Down
6 changes: 1 addition & 5 deletions tests/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ services:

createLayer(t, pebbleDir, "001-simple-layer.yaml", layerYAML)

_, stderrCh := pebbleRun(t, pebbleDir)
waitForLog(t, stderrCh, "pebble", "Started default services", 3*time.Second)

_, _ = pebbleRun(t, pebbleDir)
waitForFile(t, filepath.Join(pebbleDir, "svc1"), 3*time.Second)
waitForFile(t, filepath.Join(pebbleDir, "svc2"), 3*time.Second)
}
Expand Down Expand Up @@ -142,7 +140,6 @@ services:
stdoutCh, stderrCh := pebbleRun(t, pebbleDir, "--verbose")
waitForLog(t, stderrCh, "pebble", "Started daemon", 3*time.Second)
waitForLog(t, stdoutCh, "svc1", "hello world", 3*time.Second)
waitForLog(t, stderrCh, "pebble", "Started default services", 3*time.Second)
}

// TestArgs tests that Pebble provides additional arguments to a service
Expand All @@ -168,7 +165,6 @@ services:
)
waitForLog(t, stderrCh, "pebble", "Started daemon", 3*time.Second)
waitForLog(t, stdoutCh, "svc1", "hello world", 3*time.Second)
waitForLog(t, stderrCh, "pebble", "Started default services", 3*time.Second)
}

// TestIdentities tests that Pebble seeds identities from a file
Expand Down

0 comments on commit 2c66795

Please sign in to comment.