ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 不兼容修改-001 > 该修改是由于 Swoole 无法在协程中 fork 进程,因此只能通过该方式处理进程守护。 当你使用 `composer update` 命令升级后出现以下异常时,请根据以下提示修复 ## event 依赖找不到 启动时抛出以下异常 ``` [error] 2020-01-11 08:11:55.697 <802> Bean definition not found: event [code] 0 [type] Mix\Bean\Exception\NotFoundException ``` 再 manifest.php 中找到事件调度器的配置,增加 name => 'event' 配置: ~~~ // 事件调度器 [ // 名称 'name' => 'event', // 作用域 'scope' => \Mix\Bean\BeanDefinition::SINGLETON, // 类路径 'class' => \Mix\Event\EventDispatcher::class, // 构造函数注入 'constructorArgs' => [ \App\Common\Listeners\DatabaseListener::class, \App\Common\Listeners\RedisListener::class, ], ], ~~~ ## 后台执行失效 - 升级后 `-d` 后台执行失效,如下: ``` php bin/mix.php http:start -d ``` - 或者启动服务器时,抛出以下异常 ``` MacOS unsupport fork in coroutine, please use it before the Swoole\Coroutine\Scheduler start. ``` ### 解决方法 首先再 `Common/Listeners` 目录中创建一个 `CommandListener.php` ~~~ <?php namespace App\Common\Listeners; use Mix\Console\CommandLine\Flag; use Mix\Console\Event\CommandBeforeExecuteEvent; use Mix\Event\ListenerInterface; use Mix\Helper\ProcessHelper; /** * Class CommandListener * @package App\Common\Listeners * @author liu,jian <coder.keda@gmail.com> */ class CommandListener implements ListenerInterface { /** * 监听的事件 * @return array */ public function events(): array { // 要监听的事件数组,可监听多个事件 return [ CommandBeforeExecuteEvent::class, ]; } /** * 处理事件 * @param object $event * @throws \Swoole\Exception */ public function process(object $event) { // 事件触发后,会执行该方法 // 守护处理 if ($event instanceof CommandBeforeExecuteEvent) { switch ($event->command) { case \App\Http\Commands\StartCommand::class: case \App\WebSocket\Commands\StartCommand::class: case \App\Tcp\Commands\StartCommand::class: case \App\Udp\Commands\StartCommand::class: case \App\Sync\Commands\StartCommand::class: if (Flag::bool(['d', 'daemon'], false)) { ProcessHelper::daemon(); } break; } } } } ~~~ 然后再 manifest.php 中找到事件调度器的配置,将上面的监听器类注册进去: ~~~ // 事件调度器 [ // 名称 'name' => 'event', // 作用域 'scope' => \Mix\Bean\BeanDefinition::SINGLETON, // 类路径 'class' => \Mix\Event\EventDispatcher::class, // 构造函数注入 'constructorArgs' => [ \App\Common\Listeners\CommandListener::class, \App\Common\Listeners\DatabaseListener::class, \App\Common\Listeners\RedisListener::class, ], ], ~~~ 然后找到项目中全部的 StartCommand::class 文件中删除守护处理的相关代码: ``` // 守护处理 $daemon = Flag::bool(['d', 'daemon'], false); if ($daemon) { ProcessHelper::daemon(); } ``` ## DatabaseListener/RedisListener 监听命令数据失效 - 把 DatabaseListener 监听的事件从 ExecuteEvent::class 修改为 ExecutedEvent::class - 把 RedisListener 监听的事件从 ExecuteEvent::class 修改为 CalledEvent::class