-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swoole HTTP server doesn't trigger ManagerStart event #5654
Comments
Please set the value of worker_num to be greater than 1. $server->set([
'worker_num' => 4
]); |
@NathanFreeman , hey, thank you! I tried your advice - but got even more confusing results. Here is my current test script: <?php
$server = new \Swoole\Http\Server('0.0.0.0', 9501);
$server->set([
'worker_num' => 4,
]);
$server->on('ManagerStart', function (\Swoole\Server $server) {
echo "ManagerStart\n";
\Swoole\Process::signal(SIGUSR1, function () use ($server) {
echo "SIGUSR1 callback\n";
$server->reload();
});
});
$server->on('Request', function ($request, $response) {
echo "Request\n";
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end('<h1>Rand. #' . rand(1000, 9999) . '</h1>');
});
$server->start(); When I launch it, I see this:
Then I try to change the text in response to any other value and send the signal: I get this messages:
The text from Upon reloading the page I see that the output in the browser don't change - it still shows "Rand. #something". Swoole triggers "ManagerStart", warns that the signal processor has been registered, says that the workers are reloaded - but doesn't actually reload them. |
You don't need to manually listen for the SIGUSR1 signal, as Swoole has already taken care of it for you. You just need to send the SIGUSR1 signal to the master process to restart the worker processes. The SIGUSR2 signal is used to restart both the worker and task processes. |
@NathanFreeman , yes, it seems Swoole tries to reload workers, but unsuccessfully. <?php
$server = new \Swoole\Http\Server('0.0.0.0', 9501);
$server->set(['worker_num' => 4]);
$server->on('Request', function ($request, $response) {
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end('<h1>Rand. #' . rand(1000, 9999) . '</h1>');
});
$server->start(); Upon sending
Swoole is missing something responsible for processing |
@NathanFreeman , by the way: when I started developing test scripts for Swoole, reloading did work. But after a few hours it disappeared and I started getting those errors. I can't imagine what caused this. Weird. |
You only need to send the signal to the master process; the child processes do not listen for the SIGUSR1 signal, so it won't have any effect. |
Should I see the changes in the script in the browser upon sending For example, if I first write then change it to then send Currently I don't see that any response changes. I only see them after restarting the server script. |
This is the expected behavior because the child process is forked from the parent process, so the code of the parent process remains unchanged |
Well, then can you please tell me how to reload the server without dropping the connections, like nginx |
Swoole does not support hot reload; you can only reload PHP files in the workerStart event. // test.php
function response($response) {
$response->end('Hello World');
} $server->on('WorkerStart', function (\Swoole\Server $server) {
require "test.php";
});
$server->on('Request', function ($request, $response) {
test($response);
});
$server->start(); |
Please answer these questions before submitting your issue.
Launched my script
php server.php
with the following code:Then sent
SIGUSR1
from the console:"ManagerStart" in the console. Then reloading after receiving
SIGUSR1
."ManagerStart" isn't printed in the console.
SIGUSR1
shows an error:However, "Connect" and "Request" messages are displayed correctly.
php --ri swoole
)?uname -a
&php -v
&gcc -v
) ?uname -a
Linux us 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
php -v
I didn't compiled Swoole with GCC, I installed it with
The text was updated successfully, but these errors were encountered: