webman/queue 如何优雅抛出异常?

youfeed

throw new \Excption('异常');
这样异常给redis弄了一堆报错打印

尝试以下操作

1. config/app.php

debug=>false
error_reporting=>1024

这个是webman的错误打印配置

2.尝试使用trigger_error("超时重试2",E_USER_NOTICE)

还是会打印错误堆栈

找寻源码 webman/queue 是对 workerman/redis-queue的优雅封装,搞不定

因为我已经选择主动抛出异常重试,堆栈信息是否保存到redis起码可选的吧~ redis - fail表体积爆炸

代码分析 vendor/workerman/redis-queue/src/Client.php pull 200行

队列回流机制 就是 接收ExceptionError的错误,增加一个配置项根据 是否存储堆栈 $package['error']即可修复

$callback = $this->_subscribeQueues[$redis_key];
try {
\call_user_func($callback, $package['data']);
} catch (\Exception $e) {
if (++$package['attempts'] > $this->_options['max_attempts']) {
$package['error'] = (string) $e;
$this->fail($package);
} else {
$this->retry($package);
}
echo $e;
} catch (\Error $e) {
if (++$package['attempts'] > $this->_options['max_attempts']) {
$package['error'] = (string) $e;
$this->fail($package);
} else {
$this->retry($package);
}
echo $e;
}
346 2 2
2个回答

youfeed

主基础插件已提交修复 等待合并

我在已提交 pull request 如果官方合并,主动抛出异常 不会帮报错保存到 redis fail

开启打印错误堆栈到redis

config/plugin/webman/redis-queue/redis.php


<?php
return [
'default' => [
'host' => 'redis://127.0.0.1:6379',
'options' => [
'auth' => null, // 密码,字符串类型,可选参数
'db' => 0,            // 数据库
'prefix' => '',       // key 前缀
'max_attempts'  => 5, // 消费失败后,重试次数
'retry_seconds' => 5, // 重试间隔,单位秒
'debug' => true       // 默认false 为true主动抛出的异常 保存到失败队列
]
],
];
  • 暂无评论
walkor

composer require workerman/redis-queue ^1.2.0 webman/redis-queue ^1.3.1
升级下,现在不记录异常调用栈到redis了,记到rutime/log日志文件里了

🔝