Closed charlesgetup closed 1 year ago
The function argument, func_get_arg(0)
, in include func_get_arg(0);
is a file path. For example, /var/www/localhost/htdocs/app/core/view/backend_user/list.php
.
try to use include_once
or require_once
rather than include
and require
.
try to use
include_once
orrequire_once
rather thaninclude
andrequire
.
@NathanFreeman Thank you for your quick answer.
The included file is view template and it will be used to display dynamic data. For example, when using pagination, the same template file will be included in every page display. So I could not use include_once
or require_once
.
502 error code means there is a problem with upstream server , check if there are any log outputs.
Swoole will initialize all working processes over before receiving request.
@NathanFreeman
I have added some error log to help me debug. Here is the render
function with debug code.
public function render(array $data = array())
{
$this->data($data);
$path = ($this->engine->getResolveTemplatePath())($this->name);
try {
$level = ob_get_level();
error_log('!!! ' .$level .' - ' .print_r($data, true));
ob_start();
(function() {
extract($this->data);
error_log('include: ' .func_get_arg(0));
include func_get_arg(0);
})($path);
$content = ob_get_clean();
error_log("$level - done");
if (isset($this->layoutName)) {
$layout = $this->engine->make($this->layoutName);
$layout->sections = array_merge($this->sections, array('content' => $content));
$content = $layout->render($this->layoutData);
}
return $content;
} catch (Throwable $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}
throw $e;
}
}
I have tested this using a web page with pagination which displays the users in a table. When no new worker has been started, I will see the following debug output in error log.
!!! 1 - Array
(
[title] => List User
[totalUserAmount] => 10
[users] => Array
(
[0] => ...
)
... ...
)
include: /var/www/localhost/htdocs/app/core/view/backend_user/list.php
1 - done
But if new worker has been started, I got the following output.
!!! 1 - Array
(
[title] => List User
[totalUserAmount] => 10
[users] => Array
(
[0] => ...
)
... ...
)
include: /var/www/localhost/htdocs/app/core/view/backend_user/list.php
Worker #3 started
And I won't see 1 - done
output. The process is stuck there until timeout and a 502 error is sent.
Do we have any flag which can keep the job done by one worker? I tried to use Swoole\Lock
in Server
's onRequest
method, but the task worker still got started. And I am also not sure that this is the right direction to solve my issue or not.
Please advise. Thank you so much.
@NathanFreeman I have to go now. Thank you so much for your help and I will come back tomorrow to check updates.
Any help is appreciated. Thank you.
After a couple of days debug, I've finally found the issue.
The issue is about the include
function, but not the one in plates, it is the nested include
in my template partials. So this is not a Swoole issue. I will close this issue now.
During the debug, I found that I missed one super important Swoole event in my server setup. I think this may help someone else. So I will leave it here. And that is the one points me the right way.
$this->httpServer->on("**WorkerError**", \Closure::fromCallable(array($this, 'onServerWorkerErrorEventHandler')));
My outputs: Worker error: ID=3, pid=5374, exit_code=255. With the exit code, I realised that the issue is not caused by Swoole, it is my bad.
Please answer these questions before submitting your issue.
I am using Swoole v5.0.2 and plates php template. When the (view) template renders, sometimes Swoole starts a new worker in the middle of the render process. And the template could not finish rendering when new worker comes in. A 502 error is finally returned by the server. See the render function below. The new worker gets started when running this line of code,
include func_get_arg(0);
.I don't want the new worker to be started when running
include func_get_arg(0);
.A new worket has been started when running
include func_get_arg(0);
.php --ri swoole
)?uname -a
&php -v
&gcc -v
) ?I am using this Alpine Linux docker image,
FROM php:8.1.18-alpine3.17 AS base
.Any suggestion is appreciated. Thanks.