mvccore / ext-tool-cli-winfork
MvcCore - 扩展 - 工具 - CLI - Windows Fork - .NET Framework 4 的 CLI 调用实用工具,通过 PHP 的 `shell_exec()` 或 `system()` 在后台启动新的进程。
v5.0.0
2022-05-12 08:03 UTC
Requires
- php: >=5.4.0
- civicrm/composer-compile-plugin: ^0.18
- mvccore/mvccore: ^5.1.23
This package is auto-updated.
Last update: 2024-09-17 19:18:54 UTC
README
Windows .NET Framework 4 的 CLI 调用实用工具,通过 PHP 的 shell_exec()
或 system()
在后台启动新的进程。
此扩展在通过 Composer 安装后,仅将预编译的二进制文件 Fork.exe
复制到应用程序目录 ./App/Cli
。
安装
composer require mvccore/ext-tool-cli-winfork
使用方法
在 Windows / Unix 上使用 MvcCore 框架运行 PHP 背景任务
<?php // standard web request process: include_once('vendor/autoload.php'); // prepare paths and params: $app = \MvcCore\Application::GetInstance(); $req = $app->GetRequest(); $indexScriptAbsPath = $req->GetDocumentRoot() . $req->GetScriptName(); $phpParams = "-d max_execution_time=0 -d memory_limit=-1"; $bgProcessParams = "controller=bg-process action=calculate"; $cliDirFullPath = implode('/', [ $req->GetAppRoot(), $app->GetAppDir(), $app->GetCliDir(), ]); // prepare bg command: $cmd = "php {$phpParams} {$indexScriptAbsPath} {$bgProcessParams}"; if (substr(mb_strtolower(PHP_OS), 0, 3) === 'win') { // Fork.exe automatically finds php.exe, php.bat or php.cmd in %PATH% $cmd = "Fork.exe {$cmd}"; } else { // Unix system needs to has php executable in $PATH environment variable // for user running web request scripts (eg: www, apache): $cmd = 'bash -c "exec nohup setsid ' . $cmd . ' > /dev/null 2>&1 &"'; } // start second bg process: $cwdBefore = getcwd(); chdir($cliDirFullPath); system($cmd); chdir($cwdBefore); // continue in the standard web request process without // waiting for the background process execution end...