dunglas / frankenphp

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

Trying to get TYPO3 to work in worker-mode #477

Open ochorocho opened 8 months ago

ochorocho commented 8 months ago

I tried to get TYPO3 working in worker mode. I used the Custom Apps as a base.

This is what i have so far:

<?php

ignore_user_abort(true);

$classLoader = require dirname(__DIR__).'/vendor/autoload.php';

\TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::run(0, \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::REQUESTTYPE_FE);
/** @var \TYPO3\CMS\Frontend\Http\Application $app */
$app = \TYPO3\CMS\Core\Core\Bootstrap::init($classLoader)->get(\TYPO3\CMS\Frontend\Http\Application::class);

// Handler outside the loop for better performance (doing less work)
$handler = static function () use ($app) {
    // Called when a request is received,
    // superglobals, php://input and the like are reset
    $response = $app->handle(\TYPO3\CMS\Core\Http\ServerRequestFactory::fromGlobals());
    $app->sendResponse($response);
};
for ($nbRequests = 0, $running = true; isset($_SERVER['MAX_REQUESTS']) && ($nbRequests < ((int)$_SERVER['MAX_REQUESTS'])) && $running; ++$nbRequests) {
    $running = \frankenphp_handle_request($handler);

    // What to do with the $app here?
    // $app->terminate();
    gc_collect_cycles();
}

// What to do with the $app here?
// $app->shutdown();

Here my questions:

Output of frankenphp run -c Caddyfile --envfile .env:

2024/01/16 00:52:11.166 INFO    using provided configuration    {"config_file": "Caddyfile", "config_adapter": ""}
2024/01/16 00:52:11.169 WARN    Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies    {"adapter": "caddyfile", "file": "Caddyfile", "line": 19}
2024/01/16 00:52:11.171 INFO    admin   admin endpoint started  {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2024/01/16 00:52:11.171 WARN    http.auto_https server is listening only on the HTTP port, so no automatic HTTPS will be applied to this server {"server_name": "srv0", "http_port": 8787}
2024/01/16 00:52:11.171 INFO    http.auto_https server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv1", "https_port": 8788}
2024/01/16 00:52:11.171 INFO    http.auto_https enabling automatic HTTP->HTTPS redirects        {"server_name": "srv1"}
2024/01/16 00:52:11.171 INFO    tls.cache.maintenance   started background certificate maintenance      {"cache": "0x140002e9100"}
2024/01/16 00:52:11.172 DEBUG   http.auto_https adjusted config {"tls": {"automation":{"policies":[{"subjects":["localhost"]},{}]}}, "http": {"http_port":8787,"https_port":8788,"servers":{"srv0":{"listen":[":8787"],"routes":[{"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"vars","root":"public/"}]},{"handle":[{"handler":"static_response","headers":{"Location":["{http.request.uri.path}/"]},"status_code":308}],"match":[{"file":{"try_files":["{http.request.uri.path}/index.php"]},"not":[{"path":["*/"]}]}]},{"group":"group0","handle":[{"handler":"rewrite","uri":"{http.matchers.file.relative}"}],"match":[{"file":{"split_path":[".php"],"try_files":["{http.request.uri.path}","{http.request.uri.path}/index.php","/index.php"]},"not":[{"path":["/typo3/*","/.well-known/*","/_assets/*","/favicon.ico"]}]}]},{"group":"group0","handle":[{"handler":"rewrite","uri":"{http.matchers.file.relative}"}],"match":[{"file":{"split_path":[".php"],"try_files":["{http.request.uri.path}","{http.request.uri.path}/index.php","/typo3/index.php"]},"not":[{"path":["/typo3/install.php","/.well-known/*","/_assets/*","/favicon.ico"]}],"path":["/typo3/*"]}]},{"handle":[{"encodings":{"gzip":{},"zstd":{}},"handler":"encode","prefer":["zstd","gzip"]},{"anonymous":true,"demo":true,"handler":"mercure","publisher_jwt":{"alg":"{env.MERCURE_PUBLISHER_JWT_ALG}","key":"{env.MERCURE_PUBLISHER_JWT_KEY}"},"subscriber_jwt":{"alg":"{env.MERCURE_SUBSCRIBER_JWT_ALG}","key":"{env.MERCURE_SUBSCRIBER_JWT_KEY}"},"subscriptions":true,"transport_url":"bolt://mercure-database.db"}]},{"handle":[{"handler":"static_response","headers":{"Location":["{http.request.orig_uri.path}/"]},"status_code":308}],"match":[{"file":{"try_files":["{http.request.uri.path}/index.php"]},"not":[{"path":["*/"]}]}]},{"handle":[{"handler":"rewrite","uri":"{http.matchers.file.relative}"}],"match":[{"file":{"split_path":[".php"],"try_files":["{http.request.uri.path}","{http.request.uri.path}/index.php","index.php"]}}]},{"handle":[{"handler":"php","split_path":[".php"]}],"match":[{"path":["*.php"]}]},{"handle":[{"handler":"file_server"}]}]}],"terminal":true},{},{}],"automatic_https":{"disable":true},"logs":{"logger_names":{"localhost":"log0"}},"metrics":{}},"srv1":{"listen":[":8788"],"routes":[{"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"vars","root":"public/"}]},{"handle":[{"handler":"static_response","headers":{"Location":["{http.request.uri.path}/"]},"status_code":308}],"match":[{"file":{"try_files":["{http.request.uri.path}/index.php"]},"not":[{"path":["*/"]}]}]},{"group":"group1","handle":[{"handler":"rewrite","uri":"{http.matchers.file.relative}"}],"match":[{"file":{"split_path":[".php"],"try_files":["{http.request.uri.path}","{http.request.uri.path}/index.php","/index.php"]},"not":[{"path":["/typo3/*","/.well-known/*","/_assets/*","/favicon.ico"]}]}]},{"group":"group1","handle":[{"handler":"rewrite","uri":"{http.matchers.file.relative}"}],"match":[{"file":{"split_path":[".php"],"try_files":["{http.request.uri.path}","{http.request.uri.path}/index.php","/typo3/index.php"]},"not":[{"path":["/typo3/install.php","/.well-known/*","/_assets/*","/favicon.ico"]}],"path":["/typo3/*"]}]},{"handle":[{"encodings":{"gzip":{},"zstd":{}},"handler":"encode","prefer":["zstd","gzip"]},{"anonymous":true,"demo":true,"handler":"mercure","publisher_jwt":{"alg":"{env.MERCURE_PUBLISHER_JWT_ALG}","key":"{env.MERCURE_PUBLISHER_JWT_KEY}"},"subscriber_jwt":{"alg":"{env.MERCURE_SUBSCRIBER_JWT_ALG}","key":"{env.MERCURE_SUBSCRIBER_JWT_KEY}"},"subscriptions":true,"transport_url":"bolt://mercure-database.db"}]},{"handle":[{"handler":"static_response","headers":{"Location":["{http.request.orig_uri.path}/"]},"status_code":308}],"match":[{"file":{"try_files":["{http.request.uri.path}/index.php"]},"not":[{"path":["*/"]}]}]},{"handle":[{"handler":"rewrite","uri":"{http.matchers.file.relative}"}],"match":[{"file":{"split_path":[".php"],"try_files":["{http.request.uri.path}","{http.request.uri.path}/index.php","index.php"]}}]},{"handle":[{"handler":"php","split_path":[".php"]}],"match":[{"path":["*.php"]}]},{"handle":[{"handler":"file_server"}]}]}],"terminal":true}],"tls_connection_policies":[{}],"automatic_https":{},"logs":{"logger_names":{"localhost":"log0"}},"metrics":{}}}}}
2024/01/16 00:52:11.179 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:11.179 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:11.179 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:11.179 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:11.179 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
.....
2024/01/16 00:52:13.455 INFO    restarting      {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:13.455 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:13.474 INFO    restarting      {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:13.474 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:13.479 INFO    restarting      {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:13.479 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:13.479 INFO    restarting      {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:13.479 DEBUG   starting        {"worker": "/Users/jochen/Development/franky/public/worker.php"}
2024/01/16 00:52:13.558 INFO    restarting      {"worker": "/Users/jochen/Development/franky/public/worker.php"}
-----> KEEPS RESTARTING ALL DAY

Here is my playground project: https://gitlab.knallimall.org/ochorocho/franky

Thank you. :-)

withinboredom commented 8 months ago

Is $_SERVER['MAX_REQUESTS'] actually set to something? I usually just hardcode a number as a default in case it isn't set, but in your code that is missing. So, it is probably being cast to a 0 since that is the default when casting null.

ochorocho commented 8 months ago

@withinboredom thanks, now it works.

Worker script now is set to FRANKENPHP_CONFIG="worker ./public/index.php"

index.php minimal example:

use Symfony\Component\HttpFoundation\Response;

ignore_user_abort(true);

$classLoader = require dirname(__DIR__).'/vendor/autoload.php';

// Handler outside the loop for better performance (doing less work)
$handler = static function () {
    echo new Response("Hello World!", 200);
};
for ($nbRequests = 0, $running = true; ($nbRequests < (10)) && $running; ++$nbRequests) {
    $running = \frankenphp_handle_request($handler);

    // What to do with the $app here?
    // $app->terminate();
    gc_collect_cycles();
}

When the worker script was set to public/worker.php for some reason it still called the public/index.php. No idea if this is intended. Was just wondering it this is expected behaviour.

withinboredom commented 8 months ago

Was just wondering it this is expected behaviour.

Heh, smells exactly like #100. I don't know if it is intended, but its been that way forever. I think its just that the (default) script and (worker) script have to be the same file.