mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-18 05:04:51 +08:00
update to 2.5.0-b3 (build 410)
This commit is contained in:
parent
c185d20a93
commit
7e0fc1528a
@ -34,7 +34,8 @@
|
||||
"zhamao/connection-manager": "^1.0",
|
||||
"jelix/version": "^2.0",
|
||||
"league/climate": "^3.6",
|
||||
"psy/psysh": "^0.10"
|
||||
"psy/psysh": "@stable",
|
||||
"doctrine/orm": "^2.9"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-ctype": "Use C/C++ extension instead of polyfill will be more efficient",
|
||||
@ -45,7 +46,9 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"ZM\\": "src/ZM"
|
||||
"ZM\\": "src/ZM",
|
||||
"Module\\": "src/Module",
|
||||
"Custom\\": "src/Custom"
|
||||
},
|
||||
"files": [
|
||||
"src/ZM/global_functions.php"
|
||||
@ -55,4 +58,4 @@
|
||||
"swoole/ide-helper": "@dev",
|
||||
"phpunit/phpunit": "^8.5 || ^9.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -36,7 +36,8 @@ $config['swoole'] = [
|
||||
|
||||
/** 一些框架与Swoole运行时设置的调整 */
|
||||
$config['runtime'] = [
|
||||
'swoole_coroutine_hook_flags' => SWOOLE_HOOK_ALL & (~SWOOLE_HOOK_CURL)
|
||||
'swoole_coroutine_hook_flags' => SWOOLE_HOOK_ALL & (~SWOOLE_HOOK_CURL),
|
||||
'swoole_server_mode' => SWOOLE_BASE
|
||||
];
|
||||
|
||||
/** 轻量字符串缓存,默认开启 */
|
||||
@ -54,7 +55,7 @@ $config['worker_cache'] = [
|
||||
'transaction_timeout' => 30000
|
||||
];
|
||||
|
||||
/** MySQL数据库连接信息,host留空则启动时不创建sql连接池 */
|
||||
/** @deprecated 放弃使用,旧版数据库,请使用 mysql_config 和 doctrine/dbal 搭配使用 */
|
||||
$config['sql_config'] = [
|
||||
'sql_host' => '',
|
||||
'sql_port' => 3306,
|
||||
@ -69,6 +70,22 @@ $config['sql_config'] = [
|
||||
'sql_default_fetch_mode' => PDO::FETCH_ASSOC //added in 1.5.6
|
||||
];
|
||||
|
||||
/** MySQL数据库连接信息,host留空则启动时不创建sql连接池 */
|
||||
$config['mysql_config'] = [
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 33306,
|
||||
'unix_socket' => null,
|
||||
'username' => 'root',
|
||||
'password' => '123456',
|
||||
'dbname' => 'adb',
|
||||
'charset' => 'utf8mb4',
|
||||
'pool_size' => 64,
|
||||
'options' => [
|
||||
PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
||||
]
|
||||
];
|
||||
|
||||
/** Redis连接信息,host留空则启动时不创建Redis连接池 */
|
||||
$config['redis_config'] = [
|
||||
'host' => '',
|
||||
|
||||
@ -3,3 +3,5 @@
|
||||
这里会写一些常见的疑难解答,点击左侧问题名称打开对应解决方法。
|
||||
|
||||
如果框架运行过程中发现带有错误码(如 `E00034` 的形式),可以到 [错误码](/guide/errcode) 查看。
|
||||
|
||||
框架的常见问题见 [常见问题汇总](/faq/usual-question)。
|
||||
57
docs/faq/usual-question.md
Normal file
57
docs/faq/usual-question.md
Normal file
@ -0,0 +1,57 @@
|
||||
# 框架常见问题(持续更新)
|
||||
|
||||
## 如何正确地强制退出炸毛框架?
|
||||
|
||||
首先要知道一个概念,炸毛框架和传统的 PHP 以及其他如 Python 等语言的轻量框架都不同,框架启动后会依次启动 Master、Manager、Worker 等多个进程,而用户启动时入口的 PHP 进程就是 Master 进程,在一些对框架的正常中止、热重启上,我们给 Master 进程发送相应的 Linux 信号(如 SIGTERM)即可对整个框架的多个进程生效,无需给每个进程发送。
|
||||
|
||||
但是如果因为用户的误操作,导致炸毛框架其中的一个或多个进程阻塞,或者比如将框架挂在 screen 等守护但是守护服务进程被杀掉,总之就是无法使用 Ctrl+C 的方式正常关闭框架,这时就需要正确地杀掉所有框架进程(这固然可能会造成内存的缓存数据丢失)。
|
||||
|
||||
!!! warning "注意"
|
||||
|
||||
下方涉及 `ps` 命令后使用 `grep` 过滤的框架进程方式,如果你的服务器同时有其他使用 PHP 启动的服务,命令行刚好有 `server` 字样,可能会导致误杀,如果有影响的话,建议将 `grep server` 换成你启动时命令行的特殊参数或手动排除!
|
||||
|
||||
**一、**首先,使用 `ps`、`htop`、`netstat -nlp` 等命令确定框架的入口进程(也就是 Master 进程的 pid)。
|
||||
|
||||
确认方式示例如下:
|
||||
|
||||
- 如果你使用的是 >=2.4 版本的框架,在框架启动时就会在最先开始的 motd 上方显示 `master_pid`,如果你还能找到此处的显示,那么恭喜你,可以直接进行下面的第二步。
|
||||
- 如果你不能正常通过框架的方式找到 pid,可以通过命令 `ps aux | grep php | grep server` 的方式找到框架所有的进程。其中列出的相关框架的进程,可以寻找 pid 最小的进程,即为 Master 进程。关于如何区分进程对应关系,见本页 [使用 Linux 工具辨别框架进程]()。
|
||||
- 如果你对 `ps` 不熟悉,可以使用 `htop` 工具,使用 `F5 Tree` 方式显示,并且使用 `F4` 的 Filter,过滤 `php` 或 `bin/start` 等字样,找到进程树。
|
||||
|
||||
**二、**然后,确定框架是否正常运行且正常流程关闭。
|
||||
|
||||
如果框架能正常运行,比如可以通过访问浏览器的 `http://地址:端口/httpTimer` 等 HTTP 路由,可以使用 `SIGINT` 或 `SIGTERM` 信号正常关闭框架。我们假设 Master 进程的 pid 为 31234:`kill -TERM 31234` 或 `kill -INT 31234`,如果稍后使用 `ps aux | grep php | grep server` 命令发现没有进程存在(排除掉 grep 自身的进程),说明可以正常关闭,此关闭方法为正常停止流程,即保存了 `LightCache` 等内存缓存持久化的数据。
|
||||
|
||||
如果以上方式没有任何效果,继续看第三步。
|
||||
|
||||
**三、**不能正常流程关闭,需要手动杀掉所有进程。
|
||||
|
||||
首先使用 `ps aux | grep php | grep server | grep -v grep | awk '{print $2}'` 列出框架所有进程的 pid,确认无误后,在此条命令后接 `| xargs kill -9` 即可:
|
||||
|
||||
```bash
|
||||
# 列出进程,只显示包含php,只显示包含server,排除grep本身进程,显示第二列的pid,使用xargs循环kill这里面的进程
|
||||
ps aux | grep php | grep server | grep -v grep | awk '{print $2}' | xargs kill -9
|
||||
```
|
||||
|
||||
## 如何使用 Linux 工具查看框架进程状态?
|
||||
|
||||
框架有多个进程,有时候我们需要通过监视进程状态来确定框架是否正常运行或查看框架的资源占用率。首先一个大概念,老生常谈,炸毛框架由 Master、Manager、Worker(、TaskWorker)进程组成的。
|
||||
|
||||
如果使用 htop 工具,就比较简单,比如我启动了一个应用,使用炸毛框架编写的垃圾分类小程序 API 服务器,在 htop 命令后找到如图这部分(下面的树状图是按 F5 后切换为树状显示,避免进程刷太快可以输入 `Shift+z`):
|
||||
|
||||

|
||||
|
||||
其中,`-zsh` 下有唯一一个 php 进程,在图中对应的第一列 pid 为 `16258`,代表 Master 进程。
|
||||
|
||||
Master 进程下的唯一一个子进程(白色的是进程,绿色是线程),在图中对应的 pid 为 `16263`,代表 Manager 进程,用作管理 Worker 进程。
|
||||
|
||||
Manager 进程下的子进程,连号部分为对应的 Worker 进程,比如图中的 `16266`,`16267`,`16268`,`16269` 分别代表 `Worker #0`,`Worker #1`,`Worker #2`,`Worker #3` 四个 Worker 进程。
|
||||
|
||||
如果你还设置了 TaskWorker 进程,TaskWorker 进程的 pid 会和 Worker 进程一样是连续的,一般会接在 Worker 进程后面。
|
||||
|
||||
`htop` 使用方向键选择进程,选择到对应进程后可以使用 `F9` 来选择 kill 指令,比如让框架热重启,可以将光标移到 Master 进程上,使用 `SIGUSR1`:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
@ -2,8 +2,6 @@
|
||||
|
||||
> 本文档为炸毛框架 v2 版本,如需查看 v1 版本,[点我](https://docs-v1.zhamao.xin/)。
|
||||
|
||||
> 如果是从 v1.x 版本升级到 v2.x,[点我看升级指南](/advanced/to-v2/)。
|
||||
|
||||
!!! tip "提示"
|
||||
|
||||
编写文档需要较大精力,你也可以参与到本文档的建设中来,比如找错字,增加或更正内容,每页文档可直接点击右上方铅笔图标直接跳转至 GitHub 进行编辑,编辑后自动 Fork 并生成 Pull Request,以此来贡献此文档!
|
||||
|
||||
@ -2,6 +2,41 @@
|
||||
|
||||
这里将会记录各个主版本的框架升级后,涉及 `global.php` 的更新日志,你可以根据这里描述的内容与你的旧配置文件进行合并。
|
||||
|
||||
## v2.5.0 (build 410)
|
||||
|
||||
- 新增 `$config['runtime']` 运行时设置。
|
||||
- 删除 `$config['server_event_handler_class']`,默认在启动时全局扫描。
|
||||
- 新增 `$config['module_loader']` 模块/插件 打包配置选项。
|
||||
- 新增 `$config['mysql_config']`,取代原先的 `$config['sql_config']`,此外废弃原先的MySQL 查询器 `\ZM\DB\DB` 类。
|
||||
|
||||
更新部分:
|
||||
|
||||
```php
|
||||
/** 一些框架与Swoole运行时设置的调整 */
|
||||
$config['runtime'] = [
|
||||
'swoole_coroutine_hook_flags' => SWOOLE_HOOK_ALL & (~SWOOLE_HOOK_CURL)
|
||||
];
|
||||
|
||||
/** MySQL数据库连接信息,host留空则启动时不创建sql连接池 */
|
||||
$config['mysql_config'] = [
|
||||
'host' => '',
|
||||
'port' => 3306,
|
||||
'unix_socket' => null,
|
||||
'username' => 'root',
|
||||
'password' => '123456',
|
||||
'dbname' => 'adb',
|
||||
'charset' => 'utf8mb4',
|
||||
'pool_size' => 64,
|
||||
'options' => [
|
||||
PDO::ATTR_STRINGIFY_FETCHES => false,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
||||
]
|
||||
];
|
||||
|
||||
/** 注册 Swoole Server 事件注解的类列表(deleted) */
|
||||
// 删除
|
||||
```
|
||||
|
||||
## v2.4.0 (build 400)
|
||||
- 调整 `$config['modules']['onebot']` 配置项到 `$config['onebot']`,旧版本的此段会向下兼容,建议更新,
|
||||
- 新增 `$config['remote_terminal']` 远程终端的配置项,新增此段即可。
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
- 新增全新的模块系统,可打包模块(src 目录下的子目录用户逻辑代码)为 phar 格式进行分发和版本备份。
|
||||
- 全局配置文件新增 `module_loader` 项,用于配置外部模块加载的一些设置。
|
||||
- 全局配置文件新增 `runtime` 配置项,可自定义配置 Swoole 的一些运行时参数,目前可配置一键协程化的 Hook 参数。
|
||||
- 全局配置文件新增 `runtime` 配置项,可自定义配置 Swoole 的一些运行时参数,目前可配置一键协程化的 Hook 参数和 Swoole Server 的启动模式。
|
||||
- 新增 `module:list` 命令,用于查看未打包和已打包的模块列表。
|
||||
- 新增 `module:pack` 命令,用于打包现有 src 目录下的模块。
|
||||
- 新增 `module:unpack` 命令,用于解包现有的 phar 模块包。
|
||||
@ -30,6 +30,9 @@
|
||||
- EventDispatcher 新增方法 `getEid()` 和 `getClass()`,分别用于获取事件分发 ID 和注解事件的注解类名称。
|
||||
- 新增 EventTracer,用于追踪事件的调用栈。
|
||||
- 中间件支持传参。
|
||||
- MySQL 数据库查询器改为使用 `doctrine/dbal` 组件,更灵活和稳定。
|
||||
- 新增对 `SWOOLE_BASE` 模式的支持(支持只启动一个进程的 Server)。
|
||||
-
|
||||
|
||||
以下是版本**修改内容**:
|
||||
|
||||
@ -54,6 +57,11 @@
|
||||
- 注解 `@OnSetup` 和 `@SwooleHandler` 可直接使用,无需设置 `server_event_handler_class` 即可。
|
||||
- 修复框架在一些非正常终端中运行时导致错误的问题。
|
||||
- 使用 `--debug-mode` 参数时,自动开启热更新。
|
||||
- 修复脚手架在使用 composer 更新后检查全局配置功能的 bug。
|
||||
- 修复重启和关闭框架时造成的非正常连接断开。
|
||||
- 改用独立进程监听文件变化和终端输入。
|
||||
- 修复有协程中断的任务时停止服务器会报 Swoole 警告的 bug。
|
||||
- 修复连接被反复断开的问题。
|
||||
|
||||
**对目录的定义解释**:
|
||||
|
||||
@ -82,7 +90,6 @@
|
||||
- 生成 systemd 配置文件的命令 `systemd:generate` 变成 `generate:systemd`。
|
||||
- 全局配置文件中的 `zm_data` 的父目录由 `__DIR__ . "/../"` 改为 `WORKING_DIR`。
|
||||
- 2.5 版本将 ZMRobot 类中的所有函数方法都移动到了 `OneBotV11` 类中,但原先的 ZMRobot 还可以使用。
|
||||
-
|
||||
|
||||
## v2.4.4 (build 405)
|
||||
|
||||
|
||||
@ -114,6 +114,7 @@ nav:
|
||||
- 编写管理员才能触发的功能: advanced/example/admin.md
|
||||
- FAQ:
|
||||
- FAQ: faq/FAQ.md
|
||||
- 框架常见问题(持续更新): faq/usual-question.md
|
||||
- 启动时报错 Address already in use: faq/address-already-in-use.md
|
||||
- 出现 deadlock 字样: faq/display-deadlock.md
|
||||
- 使用 LightCache 关闭时无法正常保存持久化: faq/light-cache-wrong.md
|
||||
|
||||
@ -6,6 +6,7 @@ namespace ZM\Command;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use ZM\Config\ZMConfig;
|
||||
|
||||
class CheckConfigCommand extends Command
|
||||
{
|
||||
@ -49,7 +50,10 @@ class CheckConfigCommand extends Command
|
||||
* @noinspection PhpIncludeInspection
|
||||
*/
|
||||
private function check($remote, $local, OutputInterface $out) {
|
||||
$local_file = include_once getcwd() . "/config/".$local;
|
||||
$local_file = include_once WORKING_DIR . "/config/".$local;
|
||||
if ($local_file === true) {
|
||||
$local_file = ZMConfig::get("global");
|
||||
}
|
||||
foreach($remote as $k => $v) {
|
||||
if (!isset($local_file[$k])) {
|
||||
$out->writeln("<comment>配置文件 ".$local . " 需要更新!(当前配置文件缺少 `$k` 字段配置)</comment>");
|
||||
|
||||
@ -28,8 +28,8 @@ class ConsoleApplication extends Application
|
||||
{
|
||||
private static $obj = null;
|
||||
|
||||
const VERSION_ID = 409;
|
||||
const VERSION = "2.5.0-b2";
|
||||
const VERSION_ID = 410;
|
||||
const VERSION = "2.5.0-b3";
|
||||
|
||||
/**
|
||||
* @throws InitException
|
||||
|
||||
@ -15,6 +15,11 @@ use PDOStatement;
|
||||
use Swoole\Database\PDOStatementProxy;
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class DB
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class DB
|
||||
{
|
||||
private static $table_list = [];
|
||||
@ -64,13 +69,13 @@ class DB
|
||||
*/
|
||||
public static function unprepared($line): bool {
|
||||
try {
|
||||
$conn = SqlPoolStorage::$sql_pool->get();
|
||||
$conn = SqlPoolStorage::$sql_pool->getConnection();
|
||||
if ($conn === false) {
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DbException("无法连接SQL!" . $line);
|
||||
}
|
||||
$result = $conn->query($line) === false ? false : true;
|
||||
SqlPoolStorage::$sql_pool->put($conn);
|
||||
SqlPoolStorage::$sql_pool->putConnection($conn);
|
||||
return $result;
|
||||
} catch (DBException $e) {
|
||||
Console::warning($e->getMessage());
|
||||
@ -89,20 +94,20 @@ class DB
|
||||
if (!is_array($params)) $params = [$params];
|
||||
Console::debug("MySQL: " . $line . " | " . implode(", ", $params));
|
||||
try {
|
||||
$conn = SqlPoolStorage::$sql_pool->get();
|
||||
$conn = SqlPoolStorage::$sql_pool->getConnection();
|
||||
if ($conn === false) {
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DbException("无法连接SQL!" . $line);
|
||||
}
|
||||
$ps = $conn->prepare($line);
|
||||
if ($ps === false) {
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
/** @noinspection PhpUndefinedFieldInspection */
|
||||
throw new DbException("SQL语句查询错误," . $line . ",错误信息:" . $conn->error);
|
||||
} else {
|
||||
if (!($ps instanceof PDOStatement) && !($ps instanceof PDOStatementProxy)) {
|
||||
var_dump($ps);
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DbException("语句查询错误!返回的不是 PDOStatement" . $line);
|
||||
}
|
||||
if ($params == []) $result = $ps->execute();
|
||||
@ -110,11 +115,11 @@ class DB
|
||||
$result = $ps->execute([$params]);
|
||||
} else $result = $ps->execute($params);
|
||||
if ($result !== true) {
|
||||
SqlPoolStorage::$sql_pool->put(null);
|
||||
SqlPoolStorage::$sql_pool->putConnection(null);
|
||||
throw new DBException("语句[$line]错误!" . $ps->errorInfo()[2]);
|
||||
//echo json_encode(debug_backtrace(), 128 | 256);
|
||||
}
|
||||
SqlPoolStorage::$sql_pool->put($conn);
|
||||
SqlPoolStorage::$sql_pool->putConnection($conn);
|
||||
return $ps->fetchAll($fetch_mode);
|
||||
}
|
||||
} catch (DbException $e) {
|
||||
|
||||
@ -6,6 +6,11 @@ namespace ZM\DB;
|
||||
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class DeleteBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class DeleteBody
|
||||
{
|
||||
use WhereBody;
|
||||
|
||||
@ -6,6 +6,11 @@ namespace ZM\DB;
|
||||
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class InsertBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class InsertBody
|
||||
{
|
||||
/**
|
||||
|
||||
@ -6,6 +6,11 @@ namespace ZM\DB;
|
||||
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class SelectBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class SelectBody
|
||||
{
|
||||
use WhereBody;
|
||||
|
||||
@ -5,7 +5,11 @@
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
|
||||
/**
|
||||
* Class Table
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class Table
|
||||
{
|
||||
private $table_name;
|
||||
|
||||
@ -6,6 +6,11 @@ namespace ZM\DB;
|
||||
|
||||
use ZM\Exception\DbException;
|
||||
|
||||
/**
|
||||
* Class UpdateBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
class UpdateBody
|
||||
{
|
||||
use WhereBody;
|
||||
|
||||
@ -3,7 +3,11 @@
|
||||
|
||||
namespace ZM\DB;
|
||||
|
||||
|
||||
/**
|
||||
* Trait WhereBody
|
||||
* @package ZM\DB
|
||||
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
|
||||
*/
|
||||
trait WhereBody
|
||||
{
|
||||
protected $where_thing = [];
|
||||
|
||||
@ -5,7 +5,9 @@ namespace ZM\Event\SwooleEvent;
|
||||
|
||||
|
||||
use Swoole\Process;
|
||||
use Swoole\WebSocket\Server;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\ConnectionManager\ManagerGM;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
|
||||
@ -16,11 +18,16 @@ use ZM\Event\SwooleEvent;
|
||||
*/
|
||||
class OnBeforeReload implements SwooleEvent
|
||||
{
|
||||
public function onCall() {
|
||||
public function onCall(Server $server) {
|
||||
Console::info(Console::setColor("Reloading server...", "gold"));
|
||||
for ($i = 0; $i < ZM_WORKER_NUM; ++$i) {
|
||||
Process::kill(zm_atomic("_#worker_".$i)->get(), SIGUSR1);
|
||||
Process::kill(zm_atomic("_#worker_" . $i)->get(), SIGUSR1);
|
||||
}
|
||||
foreach ($server->connections as $fd) {
|
||||
if (ManagerGM::get($fd) !== null) $server->disconnect($fd);
|
||||
else $server->close($fd);
|
||||
}
|
||||
|
||||
usleep(800 * 1000);
|
||||
}
|
||||
}
|
||||
@ -6,12 +6,20 @@
|
||||
namespace ZM\Event\SwooleEvent;
|
||||
|
||||
|
||||
use Error;
|
||||
use Exception;
|
||||
use Swoole\Event;
|
||||
use Swoole\Process;
|
||||
use Swoole\Server;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
use ZM\Framework;
|
||||
use ZM\Store\ZMBuf;
|
||||
use ZM\Utils\DataProvider;
|
||||
use ZM\Utils\SignalListener;
|
||||
use ZM\Utils\Terminal;
|
||||
use ZM\Utils\ZMUtil;
|
||||
|
||||
/**
|
||||
* Class OnManagerStart
|
||||
@ -20,10 +28,68 @@ use ZM\Utils\SignalListener;
|
||||
*/
|
||||
class OnManagerStart implements SwooleEvent
|
||||
{
|
||||
/** @var null|Process */
|
||||
public static $process = null;
|
||||
|
||||
public function onCall(Server $server) {
|
||||
Console::debug("Calling onManagerStart event(1)");
|
||||
if (!Framework::$argv["disable-safe-exit"]) {
|
||||
SignalListener::signalManager();
|
||||
}
|
||||
self::$process = new Process(function() {
|
||||
swoole_set_process_name($_SERVER["_"]." {ext-proc}");
|
||||
if (Framework::$argv["watch"]) {
|
||||
if (extension_loaded('inotify')) {
|
||||
Console::info("Enabled File watcher, framework will reload automatically.");
|
||||
/** @noinspection PhpUndefinedFieldInspection */
|
||||
Framework::$server->inotify = $fd = inotify_init();
|
||||
$this->addWatcher(DataProvider::getSourceRootDir() . "/src", $fd);
|
||||
Event::add($fd, function () use ($fd) {
|
||||
$r = inotify_read($fd);
|
||||
Console::verbose("File updated: " . $r[0]["name"]);
|
||||
ZMUtil::reload();
|
||||
});
|
||||
} else {
|
||||
Console::warning(zm_internal_errcode("E00024") . "You have not loaded \"inotify\" extension, please install first.");
|
||||
}
|
||||
}
|
||||
if (Framework::$argv["interact"]) {
|
||||
Console::info("Interact mode");
|
||||
ZMBuf::$terminal = $r = STDIN;
|
||||
Event::add($r, function () use ($r) {
|
||||
$fget = fgets($r);
|
||||
if ($fget === false) {
|
||||
Event::del($r);
|
||||
return;
|
||||
}
|
||||
$var = trim($fget);
|
||||
if ($var == "stop") Event::del($r);
|
||||
try {
|
||||
Terminal::executeCommand($var);
|
||||
} catch (Exception $e) {
|
||||
Console::error(zm_internal_errcode("E00025") . "Uncaught exception " . get_class($e) . ": " . $e->getMessage() . " at " . $e->getFile() . "(" . $e->getLine() . ")");
|
||||
} catch (Error $e) {
|
||||
Console::error(zm_internal_errcode("E00025") . "Uncaught error " . get_class($e) . ": " . $e->getMessage() . " at " . $e->getFile() . "(" . $e->getLine() . ")");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
self::$process->set(['enable_coroutine' => true]);
|
||||
self::$process->start();
|
||||
Console::verbose("进程 Manager 已启动");
|
||||
}
|
||||
|
||||
private function addWatcher($maindir, $fd) {
|
||||
$dir = scandir($maindir);
|
||||
if ($dir[0] == ".") {
|
||||
unset($dir[0], $dir[1]);
|
||||
}
|
||||
foreach ($dir as $subdir) {
|
||||
if (is_dir($maindir . "/" . $subdir)) {
|
||||
Console::debug("添加监听目录:" . $maindir . "/" . $subdir);
|
||||
inotify_add_watch($fd, $maindir . "/" . $subdir, IN_ATTRIB | IN_ISDIR);
|
||||
$this->addWatcher($maindir . "/" . $subdir, $fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@
|
||||
namespace ZM\Event\SwooleEvent;
|
||||
|
||||
|
||||
use Swoole\Process;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
@ -16,6 +17,11 @@ use ZM\Event\SwooleEvent;
|
||||
class OnManagerStop implements SwooleEvent
|
||||
{
|
||||
public function onCall() {
|
||||
if (OnManagerStart::$process !== null) {
|
||||
if (Process::kill(OnManagerStart::$process->pid, 0)) {
|
||||
Process::kill(OnManagerStart::$process->pid, SIGTERM);
|
||||
}
|
||||
}
|
||||
Console::verbose("进程 Manager 已停止!");
|
||||
}
|
||||
}
|
||||
@ -26,55 +26,11 @@ use ZM\Utils\ZMUtil;
|
||||
class OnStart implements SwooleEvent
|
||||
{
|
||||
public function onCall(Server $server) {
|
||||
Console::debug("Calling onStart event(1)");
|
||||
if (!Framework::$argv["disable-safe-exit"]) {
|
||||
SignalListener::signalMaster($server);
|
||||
}
|
||||
if (Framework::$argv["watch"]) {
|
||||
if (extension_loaded('inotify')) {
|
||||
Console::info("Enabled File watcher, framework will reload automatically.");
|
||||
/** @noinspection PhpUndefinedFieldInspection */
|
||||
Framework::$server->inotify = $fd = inotify_init();
|
||||
$this->addWatcher(DataProvider::getSourceRootDir() . "/src", $fd);
|
||||
Event::add($fd, function () use ($fd) {
|
||||
$r = inotify_read($fd);
|
||||
Console::verbose("File updated: " . $r[0]["name"]);
|
||||
ZMUtil::reload();
|
||||
});
|
||||
} else {
|
||||
Console::warning(zm_internal_errcode("E00024") . "You have not loaded \"inotify\" extension, please install first.");
|
||||
}
|
||||
}
|
||||
if (Framework::$argv["interact"]) {
|
||||
ZMBuf::$terminal = $r = STDIN;
|
||||
Event::add($r, function () use ($r) {
|
||||
$fget = fgets($r);
|
||||
if ($fget === false) {
|
||||
Event::del($r);
|
||||
return;
|
||||
}
|
||||
$var = trim($fget);
|
||||
try {
|
||||
Terminal::executeCommand($var);
|
||||
} catch (Exception $e) {
|
||||
Console::error(zm_internal_errcode("E00025") . "Uncaught exception " . get_class($e) . ": " . $e->getMessage() . " at " . $e->getFile() . "(" . $e->getLine() . ")");
|
||||
} catch (Error $e) {
|
||||
Console::error(zm_internal_errcode("E00025") . "Uncaught error " . get_class($e) . ": " . $e->getMessage() . " at " . $e->getFile() . "(" . $e->getLine() . ")");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private function addWatcher($maindir, $fd) {
|
||||
$dir = scandir($maindir);
|
||||
if ($dir[0] == ".") {
|
||||
unset($dir[0], $dir[1]);
|
||||
}
|
||||
foreach ($dir as $subdir) {
|
||||
if (is_dir($maindir . "/" . $subdir)) {
|
||||
Console::debug("添加监听目录:" . $maindir . "/" . $subdir);
|
||||
inotify_add_watch($fd, $maindir . "/" . $subdir, IN_ATTRIB | IN_ISDIR);
|
||||
$this->addWatcher($maindir . "/" . $subdir, $fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -4,12 +4,13 @@
|
||||
namespace ZM\Event\SwooleEvent;
|
||||
|
||||
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\ConnectionManager\ManagerGM;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
use ZM\Store\LightCacheInside;
|
||||
|
||||
/**
|
||||
* Class OnWorkerExit
|
||||
@ -20,9 +21,10 @@ class OnWorkerExit implements SwooleEvent
|
||||
{
|
||||
public function onCall(Server $server, $worker_id) {
|
||||
Timer::clearAll();
|
||||
foreach($server->connections as $v) {
|
||||
$server->close($v);
|
||||
Console::info("Closing connection #".$v);
|
||||
foreach((LightCacheInside::get("wait_api", "wait_api") ?? []) as $v) {
|
||||
if (($v["worker_id"] ?? -1) == $worker_id && isset($v["coroutine"])) {
|
||||
Coroutine::resume($v["coroutine"]);
|
||||
}
|
||||
}
|
||||
Console::info("正在结束 Worker #".$worker_id.",进程内可能有事务在运行...");
|
||||
}
|
||||
|
||||
@ -9,7 +9,6 @@ use PDO;
|
||||
use ReflectionException;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Database\PDOConfig;
|
||||
use Swoole\Database\PDOPool;
|
||||
use Swoole\Process;
|
||||
use Swoole\Server;
|
||||
use ZM\Annotation\AnnotationParser;
|
||||
@ -28,6 +27,7 @@ use ZM\Exception\DbException;
|
||||
use ZM\Exception\ZMException;
|
||||
use ZM\Framework;
|
||||
use ZM\Module\QQBot;
|
||||
use ZM\MySQL\MySQLPool;
|
||||
use ZM\Store\LightCacheInside;
|
||||
use ZM\Store\MySQL\SqlPoolStorage;
|
||||
use ZM\Store\Redis\ZMRedisPool;
|
||||
@ -42,11 +42,13 @@ use ZM\Utils\SignalListener;
|
||||
class OnWorkerStart implements SwooleEvent
|
||||
{
|
||||
public function onCall(Server $server, $worker_id) {
|
||||
Console::debug("Calling onWorkerStart event(1)");
|
||||
if (!Framework::$argv["disable-safe-exit"]) {
|
||||
SignalListener::signalWorker($server, $worker_id);
|
||||
}
|
||||
unset(Context::$context[Coroutine::getCid()]);
|
||||
if ($server->taskworker === false) {
|
||||
|
||||
zm_atomic("_#worker_" . $worker_id)->set($server->worker_pid);
|
||||
if (LightCacheInside::get("wait_api", "wait_api") !== null) {
|
||||
LightCacheInside::unset("wait_api", "wait_api");
|
||||
@ -54,9 +56,11 @@ class OnWorkerStart implements SwooleEvent
|
||||
try {
|
||||
register_shutdown_function(function () use ($server) {
|
||||
$error = error_get_last();
|
||||
if (($error["type"] ?? -1) != 0) {
|
||||
if (($error["type"] ?? 0) != 0) {
|
||||
Console::error(zm_internal_errcode("E00027") . "Internal fatal error: " . $error["message"] . " at " . $error["file"] . "({$error["line"]})");
|
||||
zm_dump($error);
|
||||
} elseif (!isset($error["type"])) {
|
||||
return;
|
||||
}
|
||||
//DataProvider::saveBuffer();
|
||||
/** @var Server $server */
|
||||
@ -68,42 +72,9 @@ class OnWorkerStart implements SwooleEvent
|
||||
Framework::$server = $server;
|
||||
//ZMBuf::resetCache(); //清空变量缓存
|
||||
//ZMBuf::set("wait_start", []); //添加队列,在workerStart运行完成前先让其他协程等待执行
|
||||
foreach ($server->connections as $v) {
|
||||
$server->close($v);
|
||||
}
|
||||
|
||||
//TODO: 单独抽出来MySQL和Redis连接池
|
||||
if (ZMConfig::get("global", "sql_config")["sql_host"] != "") {
|
||||
if (SqlPoolStorage::$sql_pool !== null) {
|
||||
SqlPoolStorage::$sql_pool->close();
|
||||
SqlPoolStorage::$sql_pool = null;
|
||||
}
|
||||
Console::info("新建SQL连接池中");
|
||||
ob_start();
|
||||
phpinfo(); //这个phpinfo是有用的,不能删除
|
||||
$str = ob_get_clean();
|
||||
$str = explode("\n", $str);
|
||||
foreach ($str as $v) {
|
||||
$v = trim($v);
|
||||
if ($v == "") continue;
|
||||
if (mb_strpos($v, "API Extensions") === false) continue;
|
||||
if (mb_strpos($v, "pdo_mysql") === false) {
|
||||
throw new DbException(zm_internal_errcode("E00028") . "未安装 mysqlnd php-mysql扩展。");
|
||||
}
|
||||
}
|
||||
$sql = ZMConfig::get("global", "sql_config");
|
||||
SqlPoolStorage::$sql_pool = new PDOPool((new PDOConfig())
|
||||
->withHost($sql["sql_host"])
|
||||
->withPort($sql["sql_port"])
|
||||
// ->withUnixSocket('/tmp/mysql.sock')
|
||||
->withDbName($sql["sql_database"])
|
||||
->withCharset('utf8mb4')
|
||||
->withUsername($sql["sql_username"])
|
||||
->withPassword($sql["sql_password"])
|
||||
->withOptions($sql["sql_options"] ?? [PDO::ATTR_STRINGIFY_FETCHES => false])
|
||||
);
|
||||
DB::initTableList();
|
||||
}
|
||||
$this->initMySQLPool();
|
||||
|
||||
// 开箱即用的Redis
|
||||
$redis = ZMConfig::get("global", "redis_config");
|
||||
@ -114,10 +85,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
|
||||
$this->loadAnnotations(); //加载composer资源、phar外置包、注解解析注册等
|
||||
|
||||
//echo json_encode(debug_backtrace(), 128|256);
|
||||
|
||||
EventManager::registerTimerTick(); //启动计时器
|
||||
//ZMBuf::unsetCache("wait_start");
|
||||
set_coroutine_params(["server" => $server, "worker_id" => $worker_id]);
|
||||
$dispatcher = new EventDispatcher(OnStart::class);
|
||||
$dispatcher->setRuleFunction(function ($v) {
|
||||
@ -163,24 +131,9 @@ class OnWorkerStart implements SwooleEvent
|
||||
* @throws Exception
|
||||
*/
|
||||
private function loadAnnotations() {
|
||||
//加载phar包
|
||||
/*Console::debug("加载外部phar包中");
|
||||
$dir = DataProvider::getWorkingDir() . "/resources/package/";
|
||||
if (version_compare(SWOOLE_VERSION, "4.4.0", ">=")) Timer::clearAll();
|
||||
if (is_dir($dir)) {
|
||||
$list = scandir($dir);
|
||||
unset($list[0], $list[1]);
|
||||
foreach ($list as $v) {
|
||||
if (is_dir($dir . $v)) continue;
|
||||
if (pathinfo($dir . $v, 4) == "phar") {
|
||||
Console::debug("加载Phar: " . $dir . $v . " 中");
|
||||
require_once($dir . $v);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
//加载各个模块的注解类,以及反射
|
||||
Console::debug("检索Module中");
|
||||
Console::debug("Mapping annotations");
|
||||
$parser = new AnnotationParser();
|
||||
$composer = json_decode(file_get_contents(DataProvider::getSourceRootDir() . "/composer.json"), true);
|
||||
foreach ($composer["autoload"]["psr-4"] as $k => $v) {
|
||||
@ -195,10 +148,10 @@ class OnWorkerStart implements SwooleEvent
|
||||
EventManager::loadEventByParser($parser); //加载事件
|
||||
|
||||
//加载自定义的全局函数
|
||||
Console::debug("加载自定义上下文中...");
|
||||
Console::debug("Loading context class...");
|
||||
$context_class = ZMConfig::get("global", "context_class");
|
||||
if (!is_a($context_class, ContextInterface::class, true)) {
|
||||
throw new ZMException(zm_internal_errcode("E00032") ."Context class must implemented from ContextInterface!");
|
||||
throw new ZMException(zm_internal_errcode("E00032") . "Context class must implemented from ContextInterface!");
|
||||
}
|
||||
|
||||
//加载插件
|
||||
@ -207,6 +160,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
["status" => true, "single_bot_mode" => false, "message_level" => 99999];
|
||||
|
||||
if ($obb_onebot["status"]) {
|
||||
Console::debug("OneBot support enabled, listening OneBot event(3).");
|
||||
$obj = new OnSwooleEvent();
|
||||
$obj->class = QQBot::class;
|
||||
$obj->method = 'handleByEvent';
|
||||
@ -223,4 +177,63 @@ class OnWorkerStart implements SwooleEvent
|
||||
|
||||
//TODO: 编写加载外部插件的方式
|
||||
}
|
||||
|
||||
private function initMySQLPool() {
|
||||
if (SqlPoolStorage::$sql_pool !== null) {
|
||||
SqlPoolStorage::$sql_pool->close();
|
||||
SqlPoolStorage::$sql_pool = null;
|
||||
}
|
||||
$real_conf = [];
|
||||
if (isset(ZMConfig::get("global", "sql_config")["sql_host"])) {
|
||||
if (ZMConfig::get("global", "sql_config")["sql_host"] != "") {
|
||||
if (\server()->worker_id === 0) {
|
||||
Console::warning("使用 'sql_config' 配置项和 DB 数据库查询构造器进行查询数据库可能会在下一个大版本中废弃,请使用 'mysql_config' 搭配 doctrine dbal 使用!");
|
||||
Console::warning("详见: `https://framework.zhamao.xin/`");
|
||||
}
|
||||
$origin_conf = ZMConfig::get("global", "sql_config");
|
||||
$real_conf = [
|
||||
"host" => $origin_conf["sql_host"],
|
||||
"port" => $origin_conf["sql_port"],
|
||||
"username" => $origin_conf["sql_username"],
|
||||
"password" => $origin_conf["sql_password"],
|
||||
"dbname" => $origin_conf["sql_database"],
|
||||
"options" => $origin_conf["sql_options"],
|
||||
'unix_socket' => null,
|
||||
'charset' => 'utf8mb4',
|
||||
'pool_size' => 64
|
||||
];
|
||||
}
|
||||
}
|
||||
if (isset(ZMConfig::get("global", "mysql_config")["host"])) {
|
||||
if (ZMConfig::get("global", "mysql_config")["host"] != "") {
|
||||
$real_conf = ZMConfig::get("global", "mysql_config");
|
||||
}
|
||||
}
|
||||
if (!empty($real_conf)) {
|
||||
Console::info("Connecting to MySQL pool");
|
||||
ob_start();
|
||||
phpinfo(); //这个phpinfo是有用的,不能删除
|
||||
$str = ob_get_clean();
|
||||
$str = explode("\n", $str);
|
||||
foreach ($str as $v) {
|
||||
$v = trim($v);
|
||||
if ($v == "") continue;
|
||||
if (mb_strpos($v, "API Extensions") === false) continue;
|
||||
if (mb_strpos($v, "pdo_mysql") === false) {
|
||||
throw new DbException(zm_internal_errcode("E00028") . "未安装 mysqlnd php-mysql扩展。");
|
||||
}
|
||||
}
|
||||
SqlPoolStorage::$sql_pool = new MySQLPool((new PDOConfig())
|
||||
->withHost($real_conf["host"])
|
||||
->withPort($real_conf["port"])
|
||||
// ->withUnixSocket('/tmp/mysql.sock')
|
||||
->withDbName($real_conf["dbname"])
|
||||
->withCharset($real_conf["charset"])
|
||||
->withUsername($real_conf["username"])
|
||||
->withPassword($real_conf["password"])
|
||||
->withOptions($real_conf["options"] ?? [PDO::ATTR_STRINGIFY_FETCHES => false])
|
||||
);
|
||||
DB::initTableList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -22,6 +22,6 @@ class OnWorkerStop implements SwooleEvent
|
||||
if ($worker_id == (ZMConfig::get("worker_cache")["worker"] ?? 0)) {
|
||||
LightCache::savePersistence();
|
||||
}
|
||||
Console::verbose(($server->taskworker ? "Task" : "") . "Worker #$worker_id 已停止");
|
||||
Console::verbose(($server->taskworker ? "Task" : "") . "Worker #$worker_id 已停止: ".$server->getWorkerStatus($worker_id));
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,7 @@ namespace ZM;
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Error;
|
||||
use Exception;
|
||||
use Phar;
|
||||
use Swoole\Server\Port;
|
||||
use Throwable;
|
||||
use ZM\Config\ZMConfig;
|
||||
@ -111,8 +112,15 @@ class Framework
|
||||
|
||||
// 打印初始信息
|
||||
$out["listen"] = ZMConfig::get("global", "host") . ":" . ZMConfig::get("global", "port");
|
||||
if (!isset($this->server_set["worker_num"])) $out["worker"] = swoole_cpu_num() . " (auto)";
|
||||
else $out["worker"] = $this->server_set["worker_num"];
|
||||
if (!isset($this->server_set["worker_num"])) {
|
||||
if ((ZMConfig::get("global", "runtime")["swoole_server_mode"]) == SWOOLE_PROCESS) {
|
||||
$out["worker"] = swoole_cpu_num() . " (auto)";
|
||||
} else {
|
||||
$out["single_proc_mode"] = "true";
|
||||
}
|
||||
} else {
|
||||
$out["worker"] = $this->server_set["worker_num"];
|
||||
}
|
||||
$out["environment"] = $args["env"] === null ? "default" : $args["env"];
|
||||
$out["log_level"] = Console::getLevel();
|
||||
$out["version"] = ZM_VERSION . (LOAD_MODE == 0 ? (" (build " . ZM_VERSION_ID . ")") : "");
|
||||
@ -128,6 +136,10 @@ class Framework
|
||||
$conf = ZMConfig::get("global", "sql_config");
|
||||
$out["mysql_pool"] = $conf["sql_database"] . "@" . $conf["sql_host"] . ":" . $conf["sql_port"];
|
||||
}
|
||||
if (ZMConfig::get("global", "mysql_config")["host"] !== "") {
|
||||
$conf = ZMConfig::get("global", "mysql_config");
|
||||
$out["mysql"] = $conf["dbname"] . "@" . $conf["host"] . ":" . $conf["port"];
|
||||
}
|
||||
if (ZMConfig::get("global", "redis_config")["host"] !== "") {
|
||||
$conf = ZMConfig::get("global", "redis_config");
|
||||
$out["redis_pool"] = $conf["host"] . ":" . $conf["port"];
|
||||
@ -151,7 +163,11 @@ class Framework
|
||||
}
|
||||
|
||||
|
||||
self::$server = new Server(ZMConfig::get("global", "host"), ZMConfig::get("global", "port"));
|
||||
self::$server = new Server(
|
||||
ZMConfig::get("global", "host"),
|
||||
ZMConfig::get("global", "port"),
|
||||
ZMConfig::get("global", "runtime")["swoole_server_mode"] ?? SWOOLE_PROCESS
|
||||
);
|
||||
|
||||
if ($add_port) {
|
||||
$conf = ZMConfig::get("global", "remote_terminal") ?? [
|
||||
@ -310,10 +326,13 @@ class Framework
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @noinspection PhpIncludeInspection
|
||||
*/
|
||||
private function loadServerEvents() {
|
||||
if (\Phar::running() !== "") {
|
||||
if (Phar::running() !== "") {
|
||||
ob_start();
|
||||
$r = include_once DataProvider::getFrameworkRootDir() . "/src/ZM/script_setup_loader.php";
|
||||
include_once DataProvider::getFrameworkRootDir() . "/src/ZM/script_setup_loader.php";
|
||||
$r = ob_get_clean();
|
||||
$result_code = 0;
|
||||
} else {
|
||||
@ -440,7 +459,7 @@ class Framework
|
||||
}
|
||||
}
|
||||
}
|
||||
$global_hook = ZMConfig::get("global", 'runtime')['swoole_coroutine_hook_flags'] ?? SWOOLE_HOOK_ALL & (~SWOOLE_HOOK_CURL);
|
||||
$global_hook = ZMConfig::get("global", 'runtime')['swoole_coroutine_hook_flags'] ?? (SWOOLE_HOOK_ALL & (~SWOOLE_HOOK_CURL));
|
||||
if ($coroutine_mode && $global_hook === false) Runtime::enableCoroutine(true, $global_hook);
|
||||
else Runtime::enableCoroutine(false, SWOOLE_HOOK_ALL);
|
||||
}
|
||||
|
||||
94
src/ZM/MySQL/MySQLConnection.php
Normal file
94
src/ZM/MySQL/MySQLConnection.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Driver\Connection;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
use Swoole\Database\PDOProxy;
|
||||
use Swoole\Database\PDOStatementProxy;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Exception\DbException;
|
||||
use ZM\Store\MySQL\SqlPoolStorage;
|
||||
|
||||
class MySQLConnection implements Connection
|
||||
{
|
||||
/** @var PDO|PDOProxy */
|
||||
private $conn;
|
||||
|
||||
public function __construct() {
|
||||
Console::info("Constructing...");
|
||||
$this->conn = SqlPoolStorage::$sql_pool->getConnection();
|
||||
}
|
||||
|
||||
public function prepare($sql, $options = []) {
|
||||
try {
|
||||
$statement = $this->conn->prepare($sql, $options);
|
||||
assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement));
|
||||
} catch (PDOException $exception) {
|
||||
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
return new MySQLStatement($statement);
|
||||
}
|
||||
|
||||
public function query(...$args) {
|
||||
try {
|
||||
$statement = $this->conn->query(...$args);
|
||||
assert(($statement instanceof PDOStatementProxy) || ($statement instanceof PDOStatement));
|
||||
} catch (PDOException $exception) {
|
||||
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
return new MySQLStatement($statement);
|
||||
}
|
||||
|
||||
public function quote($value, $type = ParameterType::STRING) {
|
||||
return $this->conn->quote($value, $type);
|
||||
}
|
||||
|
||||
public function exec($sql) {
|
||||
try {
|
||||
$statement = $this->conn->exec($sql);
|
||||
assert($statement !== false);
|
||||
return $statement;
|
||||
} catch (PDOException $exception) {
|
||||
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function lastInsertId($name = null) {
|
||||
try {
|
||||
return $name === null ? $this->conn->lastInsertId() : $this->conn->lastInsertId($name);
|
||||
} catch (PDOException $exception) {
|
||||
throw new DbException($exception->getMessage(), $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
public function beginTransaction() {
|
||||
return $this->conn->beginTransaction();
|
||||
}
|
||||
|
||||
public function commit() {
|
||||
return $this->conn->commit();
|
||||
}
|
||||
|
||||
public function rollBack() {
|
||||
return $this->conn->rollBack();
|
||||
}
|
||||
|
||||
public function errorCode() {
|
||||
return $this->conn->errorCode();
|
||||
}
|
||||
|
||||
public function errorInfo() {
|
||||
return $this->conn->errorInfo();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
Console::info("Destructing!!!");
|
||||
SqlPoolStorage::$sql_pool->putConnection($this->conn);
|
||||
}
|
||||
}
|
||||
40
src/ZM/MySQL/MySQLDriver.php
Normal file
40
src/ZM/MySQL/MySQLDriver.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Driver;
|
||||
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
||||
use Doctrine\DBAL\Schema\MySqlSchemaManager;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Console\Console;
|
||||
|
||||
class MySQLDriver implements Driver
|
||||
{
|
||||
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) {
|
||||
Console::info("Requiring new connection");
|
||||
return new MySQLConnection();
|
||||
}
|
||||
|
||||
public function getDatabasePlatform(): MySqlPlatform {
|
||||
return new MySqlPlatform();
|
||||
}
|
||||
|
||||
public function getSchemaManager($conn) {
|
||||
return new MySqlSchemaManager($conn);
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return 'pdo_mysql_pool';
|
||||
}
|
||||
|
||||
public function getDatabase($conn) {
|
||||
$params = ZMConfig::get("global", "mysql_config");
|
||||
|
||||
if (isset($params['dbname'])) {
|
||||
return $params['dbname'];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
20
src/ZM/MySQL/MySQLManager.php
Normal file
20
src/ZM/MySQL/MySQLManager.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\DriverManager;
|
||||
use Doctrine\DBAL\Exception;
|
||||
|
||||
class MySQLManager
|
||||
{
|
||||
/**
|
||||
* @return Connection
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getConnection() {
|
||||
return DriverManager::getConnection(["driverClass" => MySQLDriver::class]);
|
||||
}
|
||||
}
|
||||
37
src/ZM/MySQL/MySQLPool.php
Normal file
37
src/ZM/MySQL/MySQLPool.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
||||
|
||||
/** @noinspection PhpReturnDocTypeMismatchInspection */
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use PDO;
|
||||
use Swoole\Database\PDOConfig;
|
||||
use Swoole\Database\PDOPool;
|
||||
use Swoole\Database\PDOProxy;
|
||||
|
||||
class MySQLPool extends PDOPool
|
||||
{
|
||||
private $count = 0;
|
||||
|
||||
public function __construct(PDOConfig $config, int $size = self::DEFAULT_SIZE) {
|
||||
parent::__construct($config, $size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PDO|PDOProxy|void
|
||||
*/
|
||||
public function getConnection() {
|
||||
$this->count++;
|
||||
return parent::get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PDO|PDOProxy $connection
|
||||
*/
|
||||
public function putConnection($connection) {
|
||||
$this->count--;
|
||||
parent::put($connection);
|
||||
}
|
||||
}
|
||||
78
src/ZM/MySQL/MySQLStatement.php
Normal file
78
src/ZM/MySQL/MySQLStatement.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Driver\Statement;
|
||||
use Doctrine\DBAL\Driver\StatementIterator;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use Swoole\Database\PDOStatementProxy;
|
||||
|
||||
class MySQLStatement implements Statement, \IteratorAggregate
|
||||
{
|
||||
/** @var PDOStatement|PDOStatementProxy */
|
||||
private $statement;
|
||||
|
||||
public function __construct($obj) {
|
||||
$this->statement = $obj;
|
||||
}
|
||||
|
||||
public function closeCursor() {
|
||||
return $this->statement->closeCursor();
|
||||
}
|
||||
|
||||
public function columnCount() {
|
||||
return $this->statement->columnCount();
|
||||
}
|
||||
|
||||
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = []) {
|
||||
return $this->statement->setFetchMode($fetchMode, $arg2, $arg3);
|
||||
}
|
||||
|
||||
public function fetch($fetchMode = PDO::FETCH_ASSOC, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {
|
||||
return $this->statement->fetch($fetchMode, $cursorOrientation, $cursorOffset);
|
||||
}
|
||||
|
||||
public function fetchAll($fetchMode = PDO::FETCH_ASSOC, $fetchArgument = null, $ctorArgs = null) {
|
||||
return $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
|
||||
}
|
||||
|
||||
public function fetchColumn($columnIndex = 0) {
|
||||
return $this->statement->fetchColumn($columnIndex);
|
||||
}
|
||||
|
||||
public function bindValue($param, $value, $type = ParameterType::STRING) {
|
||||
return $this->statement->bindValue($param, $value, $type);
|
||||
}
|
||||
|
||||
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null) {
|
||||
return $this->statement->bindParam($param, $variable, $type, $length);
|
||||
}
|
||||
|
||||
public function errorCode() {
|
||||
return $this->statement->errorCode();
|
||||
}
|
||||
|
||||
public function errorInfo() {
|
||||
return $this->statement->errorInfo();
|
||||
}
|
||||
|
||||
public function execute($params = null) {
|
||||
return $this->statement->execute($params);
|
||||
}
|
||||
|
||||
public function rowCount() {
|
||||
return $this->statement->rowCount();
|
||||
}
|
||||
|
||||
public function getIterator() {
|
||||
return new StatementIterator($this);
|
||||
}
|
||||
|
||||
public function current() {
|
||||
return $this->statement->current();
|
||||
}
|
||||
}
|
||||
18
src/ZM/MySQL/MySQLWrapper.php
Normal file
18
src/ZM/MySQL/MySQLWrapper.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\MySQL;
|
||||
|
||||
|
||||
class MySQLWrapper
|
||||
{
|
||||
public $connection;
|
||||
|
||||
public function __construct() {
|
||||
$this->connection = MySQLManager::getConnection();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->connection->close();
|
||||
}
|
||||
}
|
||||
@ -4,10 +4,10 @@
|
||||
namespace ZM\Store\MySQL;
|
||||
|
||||
|
||||
use Swoole\Database\PDOPool;
|
||||
use ZM\MySQL\MySQLPool;
|
||||
|
||||
class SqlPoolStorage
|
||||
{
|
||||
/** @var PDOPool */
|
||||
/** @var MySQLPool */
|
||||
public static $sql_pool = null;
|
||||
}
|
||||
|
||||
@ -4,14 +4,9 @@
|
||||
namespace ZM\Utils;
|
||||
|
||||
|
||||
use Swoole\Event;
|
||||
use Swoole\Process;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Framework;
|
||||
use ZM\Store\ZMBuf;
|
||||
use ZM\Utils\Manager\ProcessManager;
|
||||
|
||||
/**
|
||||
* 炸毛框架的Linux signal管理类
|
||||
@ -26,6 +21,7 @@ class SignalListener
|
||||
* @param Server $server
|
||||
*/
|
||||
public static function signalMaster(Server $server) {
|
||||
Console::debug("Listening Master SIGINT");
|
||||
Process::signal(SIGINT, function () use ($server) {
|
||||
if (zm_atomic("_int_is_reload")->get() === 1) {
|
||||
zm_atomic("_int_is_reload")->set(0);
|
||||
@ -33,10 +29,6 @@ class SignalListener
|
||||
} else {
|
||||
echo "\r";
|
||||
Console::warning("Server interrupted(SIGINT) on Master.");
|
||||
if ((Framework::$server->inotify ?? null) !== null)
|
||||
/** @noinspection PhpUndefinedFieldInspection */ Event::del(Framework::$server->inotify);
|
||||
if (ZMBuf::$terminal !== null)
|
||||
Event::del(ZMBuf::$terminal);
|
||||
Process::kill($server->master_pid, SIGTERM);
|
||||
}
|
||||
});
|
||||
@ -47,8 +39,17 @@ class SignalListener
|
||||
*/
|
||||
public static function signalManager() {
|
||||
$func = function () {
|
||||
Console::verbose("Interrupted in manager!");
|
||||
if (\server()->master_pid == \server()->manager_pid) {
|
||||
echo "\r";
|
||||
Console::warning("Server interrupted(SIGINT) on Manager.");
|
||||
swoole_timer_after(2, function() {
|
||||
Process::kill(posix_getpid(), SIGTERM);
|
||||
});
|
||||
} else {
|
||||
Console::verbose("Interrupted in manager!");
|
||||
}
|
||||
};
|
||||
Console::debug("Listening Manager SIGINT");
|
||||
if (version_compare(SWOOLE_VERSION, "4.6.7") >= 0) {
|
||||
Process::signal(SIGINT, $func);
|
||||
} elseif (extension_loaded("pcntl")) {
|
||||
@ -62,14 +63,17 @@ class SignalListener
|
||||
* @param $worker_id
|
||||
*/
|
||||
public static function signalWorker(Server $server, $worker_id) {
|
||||
Console::debug("Listening Worker #".$worker_id." SIGINT");
|
||||
Process::signal(SIGINT, function () use ($worker_id, $server) {
|
||||
if ($server->master_pid == $server->worker_pid) {
|
||||
echo "\r";
|
||||
Console::warning("Server interrupted(SIGINT) on Worker.");
|
||||
swoole_timer_after(2, function() {
|
||||
Process::kill(posix_getpid(), SIGTERM);
|
||||
});
|
||||
}
|
||||
//Console::verbose("Interrupted in worker");
|
||||
// do nothing
|
||||
});
|
||||
if ($server->taskworker === false) {
|
||||
Process::signal(SIGUSR1, function () use ($worker_id) {
|
||||
Timer::clearAll();
|
||||
ProcessManager::resumeAllWorkerCoroutines();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/ZM/script_orm_bootstrap.php
Normal file
18
src/ZM/script_orm_bootstrap.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
// TODO 配置 ORM
|
||||
use Doctrine\ORM\Tools\Setup;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
|
||||
$paths = array();
|
||||
$isDevMode = false;
|
||||
|
||||
// the connection configuration
|
||||
$dbParams = array(
|
||||
'driver' => 'pdo_mysql',
|
||||
'user' => 'root',
|
||||
'password' => '',
|
||||
'dbname' => 'foo',
|
||||
);
|
||||
|
||||
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
|
||||
$entityManager = EntityManager::create($dbParams, $config);
|
||||
Loading…
x
Reference in New Issue
Block a user