wip: go embedded worker

Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
Marc 'risson' Schmitt
2025-06-06 16:16:26 +02:00
parent 40dbac7a65
commit 5acdd67cba
5 changed files with 68 additions and 129 deletions

View File

@ -6,15 +6,11 @@ import (
"os/exec"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"goauthentik.io/internal/config"
"goauthentik.io/internal/utils"
)
type Worker struct {
@ -26,30 +22,30 @@ type Worker struct {
pidFile string
started bool
killed bool
alive bool
}
func New(healthcheck func() bool) *Worker {
func New() *Worker {
logger := log.WithField("logger", "authentik.router.worker")
w := &Worker{
Healthcheck: healthcheck,
log: logger,
started: false,
killed: false,
alive: false,
HealthyCallback: func() {},
log: logger,
started: false,
killed: false,
}
w.initCmd()
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGUSR2)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
go func() {
for sig := range c {
if sig == syscall.SIGHUP {
w.log.Info("SIGHUP received, forwarding to gunicorn")
switch sig {
case syscall.SIGHUP:
w.log.Info("SIGHUP received, forwarding to dramatiq")
w.Reload()
} else if sig == syscall.SIGUSR2 {
w.log.Info("SIGUSR2 received, restarting gunicorn")
w.Restart()
case syscall.SIGINT:
w.log.Info("SIGINT received, stopping dramatiq")
w.Kill(syscall.SIGINT)
case syscall.SIGTERM:
w.log.Info("SIGTERM received, stopping dramatiq")
w.Kill(syscall.SIGTERM)
}
}
}()
@ -58,135 +54,56 @@ func New(healthcheck func() bool) *Worker {
func (w *Worker) initCmd() {
command := "./manage.py"
args := []string{"dev_server"}
if !config.Get().Debug {
pidFile, err := os.CreateTemp("", "authentik-gunicorn.*.pid")
if err != nil {
panic(fmt.Errorf("failed to create temporary pid file: %v", err))
}
w.pidFile = pidFile.Name()
command = "gunicorn"
args = []string{"-c", "./lifecycle/gunicorn.conf.py", "authentik.root.asgi:application"}
if w.pidFile != "" {
args = append(args, "--pid", w.pidFile)
}
args := []string{"worker"}
if config.Get().Debug {
args = append(args, "--reload")
}
w.log.WithField("args", args).WithField("cmd", command).Debug("Starting gunicorn")
pidFile, err := os.CreateTemp("", "authentik-dramatiq.pid")
if err != nil {
panic(fmt.Errorf("failed to create temporary pid file: %v", err))
}
w.pidFile = pidFile.Name()
args = append(args, "--pid-file", w.pidFile)
w.log.WithField("args", args).WithField("cmd", command).Debug("Starting dramatiq")
w.p = exec.Command(command, args...)
w.p.Env = os.Environ()
w.p.Stdout = os.Stdout
w.p.Stderr = os.Stderr
}
func (w *Worker) IsRunning() bool {
return w.alive
}
func (w *Worker) Start() error {
if w.started {
if !w.started {
w.initCmd()
}
w.killed = false
w.started = true
go w.healthcheck()
return w.p.Run()
}
func (w *Worker) healthcheck() {
w.log.Debug("starting healthcheck")
// Default healthcheck is every 1 second on startup
// once we've been healthy once, increase to 30 seconds
for range time.NewTicker(time.Second).C {
if w.Healthcheck() {
w.alive = true
w.log.Debug("backend is alive, backing off with healthchecks")
w.HealthyCallback()
break
}
w.log.Debug("backend not alive yet")
}
}
func (w *Worker) Reload() {
w.log.WithField("method", "reload").Info("reloading gunicorn")
w.log.WithField("method", "reload").Info("reloading dramatiq")
err := w.p.Process.Signal(syscall.SIGHUP)
if err != nil {
w.log.WithError(err).Warning("failed to reload gunicorn")
w.log.WithError(err).Warning("failed to reload dramatiq")
}
}
func (w *Worker) Restart() {
w.log.WithField("method", "restart").Info("restart gunicorn")
if w.pidFile == "" {
w.log.Warning("pidfile is non existent, cannot restart")
return
}
err := w.p.Process.Signal(syscall.SIGUSR2)
if err != nil {
w.log.WithError(err).Warning("failed to restart gunicorn")
return
}
newPidFile := fmt.Sprintf("%s.2", w.pidFile)
// Wait for the new PID file to be created
for range time.NewTicker(1 * time.Second).C {
_, err = os.Stat(newPidFile)
if err == nil || !os.IsNotExist(err) {
break
}
w.log.Debugf("waiting for new gunicorn pidfile to appear at %s", newPidFile)
}
if err != nil {
w.log.WithError(err).Warning("failed to find the new gunicorn process, aborting")
return
}
newPidB, err := os.ReadFile(newPidFile)
if err != nil {
w.log.WithError(err).Warning("failed to find the new gunicorn process, aborting")
return
}
newPidS := strings.TrimSpace(string(newPidB[:]))
newPid, err := strconv.Atoi(newPidS)
if err != nil {
w.log.WithError(err).Warning("failed to find the new gunicorn process, aborting")
return
}
w.log.Warningf("new gunicorn PID is %d", newPid)
newProcess, err := utils.FindProcess(newPid)
if newProcess == nil || err != nil {
w.log.WithError(err).Warning("failed to find the new gunicorn process, aborting")
return
}
// The new process has started, let's gracefully kill the old one
w.log.Warning("killing old gunicorn")
err = w.p.Process.Signal(syscall.SIGTERM)
if err != nil {
w.log.Warning("failed to kill old instance of gunicorn")
}
w.p.Process = newProcess
// No need to close any files and the .2 pid file is deleted by Gunicorn
}
func (w *Worker) Kill() {
func (w *Worker) Kill(sig syscall.Signal) {
if !w.started {
return
}
var err error
if runtime.GOOS == "darwin" {
w.log.WithField("method", "kill").Warning("stopping gunicorn")
w.log.WithField("method", "processKill").Warning("stopping dramatiq")
err = w.p.Process.Kill()
} else {
w.log.WithField("method", "sigterm").Warning("stopping gunicorn")
err = syscall.Kill(w.p.Process.Pid, syscall.SIGTERM)
w.log.WithField("method", "syscallKill").Warning("stopping dramatiq")
err = syscall.Kill(w.p.Process.Pid, sig)
}
if err != nil {
w.log.WithError(err).Warning("failed to stop gunicorn")
w.log.WithError(err).Warning("failed to stop dramatiq")
}
if w.pidFile != "" {
err := os.Remove(w.pidFile)