laravel队列使用速率限制导致一堆超时的问题
有个队列业务使用了限速
class InfoPushQueue implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 2;
public $timeout = 86300;
/**
* InfoPushQueue constructor.
*/
public function __construct()
{
}
/**
* @throws \Illuminate\Contracts\Redis\LimiterTimeoutException
*/
public function handle()
{
Redis::throttle('key')->allow(2)->every(1)->then(function () {
//业务
}, function () {
return $this->release(10);
});
}
}
queue的配置是这样的
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 86400,
'block_for' => null,
],
比如有2000个任务在跑
如果我不限速的话 一秒会同时处理10来个,页面加载就慢了,cpu也跑满了,虽然慢但服务器还算是有响应。
然后我想优化下,改成1秒最多处理5个,没想到页面更慢了,cpu依然是跑满,而且服务器会无响应,还不如不限速的情况。
然后我再改小1秒2个,虽然服务器不慢了,但failed_job表会有一堆的超时记录
App\Jobs\InfoPushQueue has been attempted too many times or run too long. The job may have previously timed out.
而且timeout我都设置1天的时间,依然在几分钟后报超时,不知道怎么搞的?
supervisord是这样配的
[program:api]
process_name=%(program_name)s_%(process_num)02d
command=php /home/wwwroot/artisan queue:work redis --daemon --queue=high,default,low,1,2,3,4,5,6,7,8,9 --sleep=3 --tries=2 --backoff=3
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/home/wwwroot/storage/logs/worker.log
换种思路,使用延时队列
delay(Redis::get(‘xxx’))
xxx是Redis取出来的一个未来的unix时间戳,每个时间戳最多delay5次。然后Redis::increase
supervisord
的numprocs
改成1
。tries
是错误重试次数,不是速率限制。tries
),不一定是超时。如果出现重试,那么可以证明队列运行肯定是失败的。
看下日志报错
或者 改成同步测试
QUEUE_CONNECTION=sync
并没有发现
supervisor
配置中有定义timeout
参数被限速了算tries一次,要么加大要么再优化下业务的逻辑
我这个是每60秒限制2条通过