dunglas / frankenphp

🧟 The modern PHP app server
https://frankenphp.dev
MIT License
6.64k stars 218 forks source link

Unexpected Termination (255) #774

Open Diddyy opened 4 months ago

Diddyy commented 4 months ago

What happened?

Hey All!

Im trying to see if its possible to get Xenforo working with FrankenPHP.

This is my Caddyfile

{
    {$CADDY_GLOBAL_OPTIONS}

    frankenphp {
        worker ./worker.php
    }

    order mercure after encode
    order vulcain after reverse_proxy
    order php_server before file_server
    order php before file_server
}

forum.mydomain.net {
    # Set the root to the /xenforo directory
    root * /app

    # Enable compression encoders
    encode zstd br gzip

    # Include additional server directives (if any)
    {$CADDY_SERVER_EXTRA_DIRECTIVES}

    php_server {
        index index.php
    }

    # Additional configurations for Xenforo can be placed here
    tls {
        dns cloudflare {env.CF_API_TOKEN}
    }
}

This is my worker.php that I have tried to make to work well with Xenforo

<?php
// /app/worker.php

// Prevent worker script termination when a client connection is interrupted
ignore_user_abort(true);

// Boot Xenforo
// Adjust the path to reflect the location of Xenforo's initialization file
require __DIR__ . '/src/XF.php';

// Start the application
\XF::start();

// Handler outside the loop for better performance (doing less work)
$handler = static function () {
    // Called when a request is received
    $app = \XF::setupApp('XF\Pub\App');

    // Process the request and return the response
    $response = $app->run();
    $response->send();
};

// Handle requests with a limit on the number of requests or other termination conditions
for ($nbRequests = 0, $running = true; isset($_SERVER['MAX_REQUESTS']) && ($nbRequests < (int)$_SERVER['MAX_REQUESTS']) && $running; ++$nbRequests) {
    $running = \frankenphp_handle_request($handler);

    // Do something after sending the HTTP response
    $app->terminate();

    // Call the garbage collector
    gc_collect_cycles();
}

// Cleanup (if applicable)
\XF::shutdown();

However when I do docker compose up you can see from the logs what it spams in my console. I gather its an issue to do with the worker.php but I have no clue where to start to figure out what the issue may be?

Any guidance would be greatly appreciated! :)

docker-composer.yml if its of any use

services:
  frankenphp:
    image: xenforo-forum
    restart: always
    ports:
      - "8080:80"        # HTTP
      - "9445:443"      # HTTPS
      - "9445:443/udp"  # HTTP/3
    volumes:
      - /home/josh/xenforo:/app
      - /home/josh/xenforo-environment/Caddyfile:/caddy-config/Caddyfile
      - caddy_data:/data
      - caddy_config:/config
    environment:
      - MAX_REQUESTS=1000
    command: ["--config", "/caddy-config/Caddyfile"]

volumes:
  caddy_data:
  caddy_config:

Build Type

Docker (Debian Bookworm)

Worker Mode

Yes

Operating System

GNU/Linux

CPU Architecture

x86_64

PHP configuration

N/A

Relevant log output

frankenphp-1  | {"level":"error","ts":1715298046.7656398,"msg":"unexpected termination, restarting","worker":"/app/worker.php","exit_status":255}
frankenphp-1  | {"level":"error","ts":1715298046.7660913,"msg":"unexpected termination, restarting","worker":"/app/worker.php","exit_status":255}
withinboredom commented 4 months ago

I'm pretty sure it was fixed, but I suspect the issue is that the MAX_REQUESTS env variable might not be a part of the $_SERVER variable. Maybe try:

$maxRequests = $_SERVER['MAX_REQUESTS'] ?? (getenv('MAX_REQUESTS') ?: 100);

Looking at a worker.php file I have it does:

for ($nbRequests = 0, $running = true; $nbRequests < ($_SERVER['MAX_REQUESTS'] ?? 100) && $running; ++$nbRequests) {
  // ...
}
jbcr commented 3 months ago

I have the same result with:

Mode Worker : Yes

ENV:

APP_RUNTIME=Runtime\\FrankenPhpSymfony\\Runtime
FRANKENPHP_CONFIG=worker ./public/index.php
APP_ENV=dev

Application version:

ibexa/oss: 4.6.3
symfony: 5.4.39

Dockerfile

Work without worker mode

FROM dunglas/frankenphp:latest

RUN apt update && apt install -y graphicsmagick-imagemagick-compat
RUN install-php-extensions pdo_mysql gd imagick redis exif pcntl intl xsl zip opcache
withinboredom commented 3 months ago

Could this be related to #796 and the variable is going away after the first request? @dunglas

jbcr commented 3 months ago

I have tried with the frankenphp-dev Docker image with same result.