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:
parent
6c059494af
commit
91da7026f8
18
supervise.go
18
supervise.go
@ -205,6 +205,10 @@ func redirectToStatus(w http.ResponseWriter, r *http.Request, path string) {
|
|||||||
http.Redirect(w, r, u.String(), http.StatusSeeOther)
|
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) {
|
func superviseServices(services []*service) {
|
||||||
for _, s := range services {
|
for _, s := range services {
|
||||||
go supervise(s)
|
go supervise(s)
|
||||||
@ -219,6 +223,20 @@ func superviseServices(services []*service) {
|
|||||||
return nil
|
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) {
|
http.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "expected a POST request", http.StatusBadRequest)
|
http.Error(w, "expected a POST request", http.StatusBadRequest)
|
||||||
|
@ -166,14 +166,16 @@ func initUpdate() error {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement a shutdown sequence, i.e. kill + timeout + term; unmount, then force-unmount?
|
go func() {
|
||||||
|
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 {
|
if err := syscall.Unmount("/perm", unix.MNT_FORCE); err != nil {
|
||||||
log.Printf("unmounting /perm failed: %v", err)
|
log.Printf("unmounting /perm failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
|
||||||
time.Sleep(1 * time.Second) // give the HTTP response some time to be sent
|
|
||||||
if err := unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART); err != nil {
|
if err := unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user