子沫
Think-swoole集成think-queue由于配置问题造成的队列无法监听解决办法
01/06
本文最后更新于2023年01月06日,已超过685天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
2023-01-06T05:46:39.png
问题:
如上图所示官方说的queue已经在think-swoole中集成了,但是我们将此配置在swoole.php中更改后,创建完任务后,队列不执行。
原因:
queue.php的配置和swoole.php的配置不一致造成的。
- queue配置
'connections' => [
'sync' => [
'type' => 'sync',
],
'database' => [
'type' => 'database',
'queue' => 'zimuoo',
'table' => 'jobs',
'connection' => null,
],
'redis' => [
'type' => 'redis',
'queue' => "zimuoo",
'host' => env('REDIS.REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS.PORT', 6379),
'password' => env('REDIS.REDIS_PASSWORD', ''),
'select' => env('REDIS.SELECT', 0),
'timeout' => 0,
'persistent' => false,
],
],
- swoole配置
//队列
'queue' => [
'enable' => true,
//键名是队列名称
'workers' => [
//下面参数是不设置时的默认配置
'default' => [
'delay' => 0,
'sleep' => 3,
'tries' => 0,
'timeout' => 60,
'worker_num' => 3,
],
//使用@符号后面可指定队列使用驱动
'default@connection' => [
//此处可不设置任何参数,使用上面的默认配置
],
],
],
代码分析
- 先看看think-swoole的启动命令文件中的queue监听,分析代码可以发现队列是
queue [$queue]
,但是咱们两边配置不一样,一个是zimuoo,一个是default,造成任务创建了,但是不执行。
protected function createQueueWorkers()
{
$workers = $this->getConfig('queue.workers', []);
foreach ($workers as $queue => $options) {
if (strpos($queue, '@') !== false) {
[$queue, $connection] = explode('@', $queue);
} else {
$connection = null;
}
$workerNum = Arr::get($options, 'worker_num', 1);
$this->addBatchWorker($workerNum, function (Process\Pool $pool) use ($options, $connection, $queue) {
$delay = Arr::get($options, 'delay', 0);
$sleep = Arr::get($options, 'sleep', 3);
$tries = Arr::get($options, 'tries', 0);
$timeout = Arr::get($options, 'timeout', 60);
/** @var Worker $worker */
$worker = $this->app->make(Worker::class);
while (true) {
$timer = Timer::after($timeout * 1000, function () use ($pool) {
$pool->getProcess()->exit();
});
$this->runWithBarrier([$this, 'runInSandbox'], function () use ($connection, $queue, $delay, $sleep, $tries, $worker) {
$worker->runNextJob($connection, $queue, $delay, $sleep, $tries);
});
Timer::clear($timer);
}
}, "queue [$queue]");
}
}
解决
只需要将 swoole.php中的队列驱动更改为一直即可。default=>zimuoo
版权属于:
Strjson博客-专注于各种精品源码、精品软件、技术教程分享、黑客技术、破解教程(爱你在心口难开、没事写一写)
本文链接:
https://jpgke.com/php/458.html(转载时请注明本文出处及文章链接)
作品采用: