Error when trying create binary build of file a .phar from hyperf application
Hi @crazywhalecc! I have a new problem with build
New phpmicro update resolved the initial error from issue #468, but now I'm getting a new error.
I followed the instructions to remove the buildroot and source folders and run bin/spc del-download micro && bin/spc download micro
However, when I try to run the binary with ./my-binary it shows the hyperf commands normally
Now, if I actually try to run the server with the ./my-binary start command, it shows error logs
Can you find this file in your packaged phar? (not micro binary, just phar) https://github.com/hyperf/hyperf/blob/master/src/framework/src/Event/BeforeServerStart.php
And it would be better if you could provide phar build method.
Can you find this file in your packaged phar? (not micro binary, just phar) https://github.com/hyperf/hyperf/blob/master/src/framework/src/Event/BeforeServerStart.php
And it would be better if you could provide phar build method.
Yes, when I run the command directly in the phar file, it recognizes the BeforeServerStart file
The library used to build phar was hyperf/phar this is guide how to usage
Command used for build phar: swoole-cli -d "phar.readonly=0" bin/hyperf.php phar:build --name=hyperf.phar
⯠swoole-cli -d "phar.readonly=0" bin/hyperf.php phar:build --name=hyperf.phar
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[INFO] Creating phar hyperf.phar
[INFO] Adding main package "hyperf/hyperf-skeleton"
I followed all the specifications in the hyperf docs, even the part about moving the runtime folder to /tmp, then start the server
hallexcosta in github.com/hallexcosta/hyperf-binary via š v8.3.8 took 3s
⯠sudo swoole-cli hyperf.phar start
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#1 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#2 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#3 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#4 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#5 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#6 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#7 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
I forgot to ask earlier, how did you compile micro.sfx and php-cli? (Commands and operating system environment used)
I forgot to ask earlier, how did you compile micro.sfx and php-cli? (Commands and operating system environment used)
Operation System: Ubuntu 22.04
PHP Version: 8.1.28
PHP CLI in my local machie: https://github.com/swoole/swoole-cli/releases/tag/v5.1.2.0
Command build: set EXTENSIONS "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium" && ./bin/spc build --build-micro --with-micro-fake-cli "$EXTENSIONS"
Command combine: ./bin/spc micro:combine ../../hallexcosta/hyperf-binary/hyperf.phar -O ./my-hyperf-app
Try this? https://hyperf.wiki/3.1/#/en/phar?id=precautions
I briefly tried hyperf-skeleton locally and seemed to encounter the same error, but it turned out to be a problem with the pid file path, which should be a problem with hyperf itself.
Try this? https://hyperf.wiki/3.1/#/en/phar?id=precautions
I briefly tried hyperf-skeleton locally and seemed to encounter the same error, but it turned out to be a problem with the pid file path, which should be a problem with hyperf itself.
Yes, I moved the runtime folder into /tmp on my Ubuntu machine
To get to this problem, I needed to adjust the server.php, logger.php and config.php files to identify if php is running in the phar environment, if so, use the /tmp directory as reference to BASE_PATH, otherwise continue using BASE_PATH
$basePath = str_starts_with(BASE_PATH, 'phar') ? '/tmp' : BASE_PATH;
Example: logger.php
return [
'default' => [
'handler' => [
'class' => Monolog\Handler\StreamHandler::class,
'constructor' => [
'stream' => $basePath . '/runtime/logs/hyperf.log',
'level' => Monolog\Logger::DEBUG,
],
],
'formatter' => [
....
],
],
];
- I tried running without moving the
runtimefolder totmp, it sayshyperf.pidwas not found
[2024-06-24 12:38:49 @563350.0] WARNING file_put_contents(): open(/tmp/runtime/hyperf.pid) failed, Error: No such file or directory[2]
[INFO] Worker#0 started.
logs
⯠swoole-cli hyperf.phar start
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[2024-06-24 12:38:49 @563350.0] WARNING file_put_contents(): open(/tmp/runtime/hyperf.pid) failed, Error: No such file or directory[2]
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#1 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#2 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#3 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#4 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#5 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#6 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#7 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
^Cā
-
Now when execute
set EXTENSIONS "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium" && ./bin/spc download --for-extensions "$EXTENSIONS" --with-php=8.1 && ./bin/spc build --build-micro --with-micro-fake-cli "$EXTENSIONS"After build:spc micro:combine ../../hallexcosta/hyperf-binary/hyperf.phar -O ./my-hyperf-app -
I run the file with
swoole-cli hyperf.phar startand it works fine, now if I try to build the binary and run./my-hyperf-app startthen it gives me the initial error
I reset my settings and cloned the project, downloaded and built it again and now I'm getting an error even though I set the memory_limit as an argument when phar is built
Build command: spc build --build-micro "bcmath,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,iconv,mbregex,mbstring,mysqlnd,openssl,pcntl,pdo,pdo_mysql,pdo_sqlite,phar,posix,readline,simplexml,sockets,sodium,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib" -I "memory_limit=8G"
hallexcosta in static-php-cli on ī main [?] via š v8.2.13 took 9s
⯠spc micro:combine ../../hallexcosta/hyperf-binary/hyperf.phar -O ./my-hyperf-app
_ _ _ _
___| |_ __ _| |_(_) ___ _ __ | |__ _ __
/ __| __/ _` | __| |/ __|____| '_ \| '_ \| '_ \
\__ \ || (_| | |_| | (_|_____| |_) | | | | |_) |
|___/\__\__,_|\__|_|\___| | .__/|_| |_| .__/ v2.2.4
|_| |_|
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 68918384 bytes) in /home/hallexcosta/local/github.com/crazywhalecc/static-php-cli/src/SPC/command/MicroCombineCommand.php on line 89
The memory limit issue you mentioned is spc bug (the micro and phar file is too large), and I will fix it later. And I'm searching and testing hyperf packaging command now.
I tried hyperf locally, checked the source code, and also tried to package a pure swoole application. Only the hyperf package had this problem. Maybe the problem is caused by hyperf itself. We need to hand it over to the hyperf author to check the problem.
@wheakerd In theory, static-php does not have any special handling for PHARs when building micro SAPI. The only possible problem is reading the PHAR itself from the code in the PHAR.
But at present, except for Hyperf+Swoole, I have not received a minimal reproducible example. Maybe we can try whether the latest swoole has this problem.
Here's a possible workaround: https://github.com/crazywhalecc/static-php-cli/issues/479#issuecomment-2480521473
@wheakerd I just tested the latest Swoole, PHP and Hyperf frameworks. The problem can be solved after closing SWOOLE_HOOK_FILE, but there is still a problem if it is included. It can be determined that the problem is caused by Swoole's coroutine hook.
Update some details:
Before the class not found error occurs, the length of the obtained byt or text seems to be a fixed 411 bytes.
@wheakerd Because when packaged into binary, the binary file itself is both a PHP interpreter and a phar package. For the OS, it is an executable file, and for the PHP interpreter (itself) it is a phar, because micro has modified the phar reading code.
So the possible problem is that swoole's file hook caused problems with micro's patch to phar.
@wheakerd In theory, static-php does not have any special handling for PHARs when building micro SAPI. The only possible problem is reading the PHAR itself from the code in the PHAR.
But at present, except for Hyperf+Swoole, I have not received a minimal reproducible example. Maybe we can try whether the latest swoole has this problem.
Here's a possible workaround: #479 (comment)
This worked for me, I didn't test it too deeply but just closing SWOOLE HOOK FILE in bin/hyperf.php, building a new phar package and combining it again was able to start the application.
// ...
// ! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', Hyperf\Engine\DefaultOption::hookFlags());
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL & ~SWOOLE_HOOK_FILE);
// ...
logs
⯠./s3-app start
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#1 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#2 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
Thanks for this information š
P.S.: If anyone has anything (article, post, issue, explanation, anything) talking about SWOOLE_HOOK_FILE so I can understand a little more what this hook is for, it would help me a lot
for those who want to test the binary: https://github.com/HallexCosta/simple-s3-upload-service-with-hyperf/releases/tag/v1.0.0
@wheakerd In theory, static-php does not have any special handling for PHARs when building micro SAPI. The only possible problem is reading the PHAR itself from the code in the PHAR. But at present, except for Hyperf+Swoole, I have not received a minimal reproducible example. Maybe we can try whether the latest swoole has this problem. Here's a possible workaround: #479 (comment)
This worked for me, I didn't test it too deeply but just closing
SWOOLE HOOK FILEinbin/hyperf.php, building a newpharpackage and combining it again was able to start the application.// ... // ! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', Hyperf\Engine\DefaultOption::hookFlags()); ! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL & ~SWOOLE_HOOK_FILE); // ...logs
⯠./s3-app start [DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener. [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener. [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener. [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener. [DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener. [DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener. [INFO] Worker#0 started. [INFO] HTTP Server listening at 0.0.0.0:9501 [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener. [INFO] Worker#1 started. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener. [INFO] Worker#2 started. [DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.Thanks for this information š
P.S.: If anyone has anything (article, post, issue, explanation, anything) talking about
SWOOLE_HOOK_FILEso I can understand a little more what this hook is for, it would help me a lotfor those who want to test the binary: https://github.com/HallexCosta/simple-s3-upload-service-with-hyperf/releases/tag/v1.0.0
SWOOLE_HOOK_FILE are used by swoole to hook standard php file function such as file_get_contents, fopen to make it to non-blocking function. So if you disable this hook, and your app use php standard function it will block the event loop. but there is workaround to convert blocking function to non blocking function by using swoole Process
SWOOLE_HOOK_FILEare used by swoole to hook standard php file function such asfile_get_contents,fopento make it to non-blocking function. So if you disable this hook, and your app use php standard function it will block the event loop. but there is workaround to convert blocking function to non blocking function by using swooleProcess
@praswicaksono Thanks for the explanation, it's clearer now
Do you have any examples of how to convert a blocking function to a non-blocking function using Process of swoole?
workaround of this issue:
// add to startup php file
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL & ~SWOOLE_HOOK_FILE);