root: handle SIGHUP and SIGUSR2
This is the first step to handle configuration reloading. With those
changes, it is already possible to do so, by sending a SIGUSR2 signal to
the Go server process. The next step would be to watch for changes to
configuration files and call the Restart function of the GoUnicorn
instance.
SIGHUP is catched by the go server and forwarded as-is to gunicorn,
which causes it to restart its workers. However, that does not trigger
a reload of the Django settings, probably because they are already
loaded in the master, before creating any of the worker instances.
SIGUSR2, however, can be used to spawn a new gunicorn master process,
but handling it is a bit trickier. Please refer to Gunicorn's
documentation[0] for details, especially the "Upgrading to a new binary
on the fly" section.
As we are now effectively killing the gunicorn processed launched by the
server, we need to handle some sort of check to make sure it is still
running. That's done by using the already existing healthchecks, making
them useful not only for the application start, but also for its
lifetime. If a check is failed too many times in a given time period,
the gunicorn processed is killed (if necessary) and then restarted.
[0] https://docs.gunicorn.org/en/20.1.0/signals.html
Other relevant links and documentation:
Python library handling the processing swaping upon a SIGUSR2:
https://github.com/flupke/rainbow-saddle/
Golang cannot easily check if a process exists on Unix systems:
https://github.com/golang/go/issues/34396
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
2023-04-28 21:52:02 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2023-06-23 16:44:25 +00:00
|
|
|
"errors"
|
root: handle SIGHUP and SIGUSR2
This is the first step to handle configuration reloading. With those
changes, it is already possible to do so, by sending a SIGUSR2 signal to
the Go server process. The next step would be to watch for changes to
configuration files and call the Restart function of the GoUnicorn
instance.
SIGHUP is catched by the go server and forwarded as-is to gunicorn,
which causes it to restart its workers. However, that does not trigger
a reload of the Django settings, probably because they are already
loaded in the master, before creating any of the worker instances.
SIGUSR2, however, can be used to spawn a new gunicorn master process,
but handling it is a bit trickier. Please refer to Gunicorn's
documentation[0] for details, especially the "Upgrading to a new binary
on the fly" section.
As we are now effectively killing the gunicorn processed launched by the
server, we need to handle some sort of check to make sure it is still
running. That's done by using the already existing healthchecks, making
them useful not only for the application start, but also for its
lifetime. If a check is failed too many times in a given time period,
the gunicorn processed is killed (if necessary) and then restarted.
[0] https://docs.gunicorn.org/en/20.1.0/signals.html
Other relevant links and documentation:
Python library handling the processing swaping upon a SIGUSR2:
https://github.com/flupke/rainbow-saddle/
Golang cannot easily check if a process exists on Unix systems:
https://github.com/golang/go/issues/34396
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
2023-04-28 21:52:02 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FindProcess(pid int) (*os.Process, error) {
|
|
|
|
if pid <= 0 {
|
|
|
|
return nil, fmt.Errorf("invalid pid %v", pid)
|
|
|
|
}
|
|
|
|
// The error doesn't mean anything on Unix systems, let's just check manually
|
|
|
|
// that the new gunicorn master has properly started
|
|
|
|
// https://github.com/golang/go/issues/34396
|
|
|
|
proc, err := os.FindProcess(int(pid))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = proc.Signal(syscall.Signal(0))
|
|
|
|
if err == nil {
|
|
|
|
return proc, nil
|
|
|
|
}
|
2023-06-23 16:44:25 +00:00
|
|
|
if errors.Is(err, os.ErrProcessDone) {
|
root: handle SIGHUP and SIGUSR2
This is the first step to handle configuration reloading. With those
changes, it is already possible to do so, by sending a SIGUSR2 signal to
the Go server process. The next step would be to watch for changes to
configuration files and call the Restart function of the GoUnicorn
instance.
SIGHUP is catched by the go server and forwarded as-is to gunicorn,
which causes it to restart its workers. However, that does not trigger
a reload of the Django settings, probably because they are already
loaded in the master, before creating any of the worker instances.
SIGUSR2, however, can be used to spawn a new gunicorn master process,
but handling it is a bit trickier. Please refer to Gunicorn's
documentation[0] for details, especially the "Upgrading to a new binary
on the fly" section.
As we are now effectively killing the gunicorn processed launched by the
server, we need to handle some sort of check to make sure it is still
running. That's done by using the already existing healthchecks, making
them useful not only for the application start, but also for its
lifetime. If a check is failed too many times in a given time period,
the gunicorn processed is killed (if necessary) and then restarted.
[0] https://docs.gunicorn.org/en/20.1.0/signals.html
Other relevant links and documentation:
Python library handling the processing swaping upon a SIGUSR2:
https://github.com/flupke/rainbow-saddle/
Golang cannot easily check if a process exists on Unix systems:
https://github.com/golang/go/issues/34396
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
2023-04-28 21:52:02 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
errno, ok := err.(syscall.Errno)
|
|
|
|
if !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch errno {
|
|
|
|
case syscall.ESRCH:
|
|
|
|
return nil, nil
|
|
|
|
case syscall.EPERM:
|
|
|
|
return proc, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|