diff --git a/caddy/caddy.go b/caddy/caddy.go index 4c99986771..77079ac520 100644 --- a/caddy/caddy.go +++ b/caddy/caddy.go @@ -40,22 +40,8 @@ func init() { httpcaddyfile.RegisterDirectiveOrder("php_server", "before", "file_server") } -type mainPHPinterpreterKeyType int - -var mainPHPInterpreterKey mainPHPinterpreterKeyType - -var phpInterpreter = caddy.NewUsagePool() - var metrics = frankenphp.NewPrometheusMetrics(prometheus.DefaultRegisterer) -type phpInterpreterDestructor struct{} - -func (phpInterpreterDestructor) Destruct() error { - frankenphp.Shutdown() - - return nil -} - type workerConfig struct { // FileName sets the path to the worker script. FileName string `json:"file_name,omitempty"` @@ -91,29 +77,24 @@ func (f *FrankenPHPApp) Start() error { opts = append(opts, frankenphp.WithWorkers(repl.ReplaceKnown(w.FileName, ""), w.Num, w.Env, w.Watch)) } - _, loaded, err := phpInterpreter.LoadOrNew(mainPHPInterpreterKey, func() (caddy.Destructor, error) { - if err := frankenphp.Init(opts...); err != nil { - return nil, err - } - - return phpInterpreterDestructor{}, nil - }) - if err != nil { + frankenphp.Shutdown() + if err := frankenphp.Init(opts...); err != nil { return err } - if loaded { - frankenphp.Shutdown() - if err := frankenphp.Init(opts...); err != nil { - return err - } - } - return nil } func (f *FrankenPHPApp) Stop() error { caddy.Log().Info("FrankenPHP stopped 🐘") + + // attempt a graceful shutdown if caddy is exiting + // note: Exiting() is currently marked as 'experimental' + // https://github.com/caddyserver/caddy/blob/e76405d55058b0a3e5ba222b44b5ef00516116aa/caddy.go#L810 + if caddy.Exiting() { + frankenphp.Shutdown() + } + // reset configuration so it doesn't bleed into later tests f.Workers = nil f.NumThreads = 0 diff --git a/frankenphp.go b/frankenphp.go index 809e4af7d4..8196513d1b 100644 --- a/frankenphp.go +++ b/frankenphp.go @@ -64,6 +64,7 @@ var ( ScriptExecutionError = errors.New("error during PHP script execution") requestChan chan *http.Request + isRunning bool loggerMu sync.RWMutex logger *zap.Logger @@ -275,9 +276,10 @@ func calculateMaxThreads(opt *opt) (int, int, error) { // Init starts the PHP runtime and the configured workers. func Init(options ...Option) error { - if requestChan != nil { + if isRunning { return AlreadyStartedError } + isRunning = true // Ignore all SIGPIPE signals to prevent weird issues with systemd: https://github.com/dunglas/frankenphp/issues/1020 // Docker/Moby has a similar hack: https://github.com/moby/moby/blob/d828b032a87606ae34267e349bf7f7ccb1f6495a/cmd/dockerd/docker.go#L87-L90 @@ -357,6 +359,10 @@ func Init(options ...Option) error { // Shutdown stops the workers and the PHP runtime. func Shutdown() { + if !isRunning { + return + } + drainWorkers() drainPHPThreads() metrics.Shutdown() @@ -368,6 +374,7 @@ func Shutdown() { } logger.Debug("FrankenPHP shut down") + isRunning = false } func getLogger() *zap.Logger {