reboot: send SIGTERM to processes, wait for 1s

This way, programs can flush state to permanent storage if they need to.
This commit is contained in:
Michael Stapelberg 2018-06-17 08:53:19 +02:00
parent 6c059494af
commit 91da7026f8
2 changed files with 27 additions and 7 deletions

View File

@ -205,6 +205,10 @@ func redirectToStatus(w http.ResponseWriter, r *http.Request, path string) {
http.Redirect(w, r, u.String(), http.StatusSeeOther)
}
// killSupervisedServices is called before rebooting when upgrading, allowing
// processes to terminate in an orderly fashion.
var killSupervisedServices = func() {}
func superviseServices(services []*service) {
for _, s := range services {
go supervise(s)
@ -219,6 +223,20 @@ func superviseServices(services []*service) {
return nil
}
killSupervisedServices = func() {
for _, s := range services {
if s.Stopped() {
continue
}
s.setStopped(true)
if p := s.Process(); p != nil {
p.Signal(syscall.SIGTERM)
}
}
}
http.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "expected a POST request", http.StatusBadRequest)

View File

@ -166,14 +166,16 @@ func initUpdate() error {
return
}
// TODO: implement a shutdown sequence, i.e. kill + timeout + term; unmount, then force-unmount?
if err := syscall.Unmount("/perm", unix.MNT_FORCE); err != nil {
log.Printf("unmounting /perm failed: %v", err)
}
go func() {
time.Sleep(1 * time.Second) // give the HTTP response some time to be sent
killSupervisedServices()
// give the HTTP response some time to be sent; allow processes some time to terminate
time.Sleep(1 * time.Second)
if err := syscall.Unmount("/perm", unix.MNT_FORCE); err != nil {
log.Printf("unmounting /perm failed: %v", err)
}
if err := unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}