Init starts the PHP runtime and the configured workers.
(options ...Option)
| 238 | |
| 239 | // Init starts the PHP runtime and the configured workers. |
| 240 | func Init(options ...Option) error { |
| 241 | if isRunning { |
| 242 | return ErrAlreadyStarted |
| 243 | } |
| 244 | isRunning = true |
| 245 | |
| 246 | // Ignore all SIGPIPE signals to prevent weird issues with systemd: https://github.com/php/frankenphp/issues/1020 |
| 247 | // Docker/Moby has a similar hack: https://github.com/moby/moby/blob/d828b032a87606ae34267e349bf7f7ccb1f6495a/cmd/dockerd/docker.go#L87-L90 |
| 248 | signal.Ignore(syscall.SIGPIPE) |
| 249 | |
| 250 | registerExtensions() |
| 251 | |
| 252 | opt := &opt{} |
| 253 | for _, o := range options { |
| 254 | if err := o(opt); err != nil { |
| 255 | Shutdown() |
| 256 | return err |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | globalMu.Lock() |
| 261 | |
| 262 | if opt.ctx != nil { |
| 263 | globalCtx = opt.ctx |
| 264 | opt.ctx = nil |
| 265 | } |
| 266 | |
| 267 | if opt.logger != nil { |
| 268 | globalLogger = opt.logger |
| 269 | opt.logger = nil |
| 270 | } |
| 271 | |
| 272 | globalMu.Unlock() |
| 273 | |
| 274 | if opt.metrics != nil { |
| 275 | metrics = opt.metrics |
| 276 | } |
| 277 | |
| 278 | maxWaitTime = opt.maxWaitTime |
| 279 | maxRequestsPerThread = opt.maxRequests |
| 280 | |
| 281 | if opt.maxIdleTime > 0 { |
| 282 | maxIdleTime = opt.maxIdleTime |
| 283 | } |
| 284 | |
| 285 | workerThreadCount, err := calculateMaxThreads(opt) |
| 286 | if err != nil { |
| 287 | Shutdown() |
| 288 | return err |
| 289 | } |
| 290 | |
| 291 | metrics.TotalThreads(opt.numThreads) |
| 292 | |
| 293 | config := Config() |
| 294 | |
| 295 | if config.Version.MajorVersion < 8 || (config.Version.MajorVersion == 8 && config.Version.MinorVersion < 2) { |
| 296 | Shutdown() |
| 297 | return ErrInvalidPHPVersion |