nguyenanhung / codeigniter3-swoole
Swoole 适配器用于 CodeIgniter v3 框架 - 基本简单轻量
v1.0.1
2022-07-25 07:42 UTC
Requires
- php: >=7.3.0
- ext-swoole: ^4.0.0
README
Swoole 适配器用于 CodeIgniter v3 框架
你想要长期任务?计时器?FPM 到 CLI?在 FPM 和 CLI 模式下重用代码?
"这很简单!"
此适配器将使在 CodeIgniter 框架中使用 swoole 变得非常简单。
有了这个适配器,你可以在任何地方(FPM)从你的代码中启动一个任务(CLI)。
这意味着你可以从 FPM 进程启动一个 CLI 任务。
安装
composer require nguyenanhung/codeigniter3-swoole
如何做
- 首先,当然你必须将
codeigniter3-swoole
安装到你的 CodeIgniter 项目中。 - (此步骤是可选的)从
config
复制这两个配置文件swoole.php
和timers.php
到你的app/config
文件夹。 - 启动 swoole 服务器
php index.php swoole/server/start
- 你现在可以使用
\nguyenanhung\CodeIgniter\Swoole\Core\Client::send($data)
启动一个任务! - 没有步骤 5。
什么是任务?
任务只是你的 CodeIgniter 控制器的一种方法,因此几乎任何控制器方法都可以用作任务。
让我们看看代码
\nguyenanhung\CodeIgniter\Swoole\Core\Client::send( [ 'route' => 'your/route/uri/to/a/method' 'params' => ['test' => 666] ]);
使用 route
来查找要调用的方法作为任务,而 params
是你可能想要传递给任务的参数数组。
所以,就是这样!
服务器 CLI 命令
// start the swoole server php index.php swoole/server/start // stop the swoole server php index.php swoole/server/stop // reload all wokers of swoole server php index.php swoole/server/reload
更多一点
步骤 2 中复制的文件是此适配器的配置文件。
swoole.php
文件可以设置主机、端口、日志文件等。
timers.php
文件可以为 swoole 服务器设置一些计时器方法,这些计时器将在服务器初始化时启动。
示例与以下相同。
class Test extends CI_Controller { // ------------------------------------------------------------------------------ /** * here's the task 'tests/test/task' */ public function task() { $data = $this->input->post(); // as you see, params worked like normally post data log_message('info', var_export($data, true)); } // ------------------------------------------------------------------------------ /** * here's the timer method * * you should copay timers.php to your config folder, * then add $timers['tests/test/task_timer'] = 10000; and start the swoole server. * * this method would be called every 10 seconds per time. */ public function task_timer() { log_message('info', 'timer works!'); } // ------------------------------------------------------------------------------ /** * send data to task */ public function send() { try { \nguyenanhung\CodeIgniter\Swoole\Core\Client::send( [ 'route' => 'tests/test/task', 'params' => ['hope' => 'it works!'], ]); } catch (\Exception $e) { log_message('error', $e->getMessage()); log_message('error', $e->getTraceAsString()); } } // ------------------------------------------------------------------------------ }
联系 & 支持
如果有任何问题 & 请求,请通过以下信息联系
来自越南的爱与祝福 <3