MCPcopy Index your code
hub / github.com/php/frankenphp / tearDownWorkerScript

Function tearDownWorkerScript

threadworker.go:135–203  ·  view source on GitHub ↗
(handler *workerThread, exitStatus int)

Source from the content-addressed store, hash-verified

133}
134
135func tearDownWorkerScript(handler *workerThread, exitStatus int) {
136 worker := handler.worker
137 handler.dummyFrankenPHPContext = nil
138 handler.dummyContext = nil
139
140 // if the worker request is not nil, the script might have crashed
141 // make sure to close the worker request context
142 if handler.workerFrankenPHPContext != nil {
143 handler.workerFrankenPHPContext.closeContext()
144 handler.thread.contextMu.Lock()
145 handler.workerFrankenPHPContext = nil
146 handler.workerContext = nil
147 handler.thread.contextMu.Unlock()
148 }
149
150 // on exit status 0 we just run the worker script again
151 if exitStatus == 0 && !handler.isBootingScript {
152 metrics.StopWorker(worker.name, StopReasonRestart)
153
154 if globalLogger.Enabled(globalCtx, slog.LevelDebug) {
155 globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "restarting", slog.String("worker", worker.name), slog.Int("thread", handler.thread.threadIndex), slog.Int("exit_status", exitStatus))
156 }
157
158 return
159 }
160
161 // worker has thrown a fatal error or has not reached frankenphp_handle_request
162 if handler.isBootingScript {
163 metrics.StopWorker(worker.name, StopReasonBootFailure)
164 } else {
165 metrics.StopWorker(worker.name, StopReasonCrash)
166 }
167
168 if !handler.isBootingScript {
169 // fatal error (could be due to exit(1), timeouts, etc.)
170 if globalLogger.Enabled(globalCtx, slog.LevelDebug) {
171 globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "restarting", slog.String("worker", worker.name), slog.Int("thread", handler.thread.threadIndex), slog.Int("exit_status", exitStatus))
172 }
173
174 return
175 }
176
177 if worker.maxConsecutiveFailures >= 0 && startupFailChan != nil && !watcherIsEnabled && handler.failureCount >= worker.maxConsecutiveFailures {
178 startupFailChan <- fmt.Errorf("too many consecutive failures: worker %s has not reached frankenphp_handle_request()", worker.fileName)
179 handler.thread.state.Set(state.ShuttingDown)
180 return
181 }
182
183 if watcherIsEnabled {
184 // worker script has probably failed due to script changes while watcher is enabled
185 if globalLogger.Enabled(globalCtx, slog.LevelError) {
186 globalLogger.LogAttrs(globalCtx, slog.LevelWarn, "(watcher enabled) worker script has not reached frankenphp_handle_request()", slog.String("worker", worker.name), slog.Int("thread", handler.thread.threadIndex))
187 }
188 } else {
189 // rare case where worker script has failed on a restart during normal operation
190 // this can happen if startup success depends on external resources
191 if globalLogger.Enabled(globalCtx, slog.LevelWarn) {
192 globalLogger.LogAttrs(globalCtx, slog.LevelWarn, "worker script has failed on restart", slog.String("worker", worker.name), slog.Int("thread", handler.thread.threadIndex), slog.Int("failures", handler.failureCount))

Callers 1

afterScriptExecutionMethod · 0.85

Calls 4

closeContextMethod · 0.80
SetMethod · 0.80
StopWorkerMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected