queue redis 的 onConsumeFailure捕获不到异常信息,不多说上代码;
<?php
namespace app\queue\redis;
use app\exceptor\custom\ParamException;
use GuzzleHttp\Exception\ServerException;
use support\Container;
use support\Log;
use Webman\RedisQueue\Consumer;
class Statistics implements Consumer
{
// 要消费的队列名
public $queue = 'statistics';
// 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
public $connection = 'default';
/**
* @throws ParamException
*/
public function consume($data)
{
echo "统计队列消费内容\n";
print_r($data);
return callStatistics($data);
/* try {
return callStatistics($data);
}
catch ( \Exception $e ) {
Log::error('statistics 统计队列消费捕获异常',[$e->getMessage(),[$e]]);
return $e->getMessage();
}*/
}
public function onConsumeFailure(\Throwable $e, $package)
{
echo "统计队列消费捕获异常\n";
echo $e->getMessage();
// 将接收的数据写入日志文件
$path = runtime_path().'/logs/redisQueue/' . date('Ym');
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$path .= '/' . date('Ymd') . '.txt';
file_put_contents($path, date('Y-m-d H:i:s')."-$this->queue" . "\n Message:".$e->getMessage() . "\n Content:" . json_encode($package,JSON_UNESCAPED_UNICODE ) . "\n\n", FILE_APPEND);
}
}
执行到 callStatistics 方法 ,我直接 抛出了异常, throw new ParamException('数据类型有误');
在命令面板有抛出异常,但是我在 onConsumeFailure 下面获取不到异常,命令面板也没有执行我echo 的提示 。
那位大佬遇到过这种问题,或者帮忙看看,需要怎么解决。
非常感谢。
API
Client::__construct()
Client::send()
Client::subscribe()
Client::unsubscribe()
Client::onConsumeFailure()
onConsumeFailure 不是写在Statistics里
我认为应该是什么地方发起的Client::send() 在他下方写Client::onConsumeFailure()
要不你看下https://www.workerman.net/doc/workerman/components/workerman-redis-queue.html 这里的示例
你可以试试看,老大的例子有的时候写的就是不太细,他可能认为大家是跟他一个水平.
谢谢,我是按 webman 里面的案例写的,https://www.workerman.net/doc/webman/queue/redis.html#%E6%B6%88%E8%B4%B9 ;后面我试试你提供的方法看,初始想着他这个写法应该会有优势,写法简单易懂就用了。
这个特性是其他开发者几个月前提交的,如果你没有触发可能是版本不是最新的,把 webman/redis-queue 和 workerman/redis-queue 升级到最新试下。
下次记住有问题先升级,还有提问的时候把相关组件版本号发出来。
composer info
命令能看到组件版本信息workerman/redis v2.0.1 ;
webman/redis-queue v1.2.4 ;
对应的是这两个的版本,
搜索过问答,没有看到相关的提问过,以为没有出现过,所以就发出来咨询一下。