Skip to content

Commit

Permalink
fix: graceful shutdown (#1242)
Browse files Browse the repository at this point in the history
* Shuts caddy down gracefully.

* Moves isRunning to the very end.

* Changes check to Exiting().
  • Loading branch information
Alliballibaba2 authored Dec 17, 2024
1 parent f592e0f commit fbbc129
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
39 changes: 10 additions & 29 deletions caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion frankenphp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -368,6 +374,7 @@ func Shutdown() {
}

logger.Debug("FrankenPHP shut down")
isRunning = false
}

func getLogger() *zap.Logger {
Expand Down

0 comments on commit fbbc129

Please sign in to comment.