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
{
// 要消費(fèi)的隊(duì)列名
public $queue = 'statistics';
// 連接名,對(duì)應(yīng) plugin/webman/redis-queue/redis.php 里的連接`
public $connection = 'default';
/**
* @throws ParamException
*/
public function consume($data)
{
echo "統(tǒng)計(jì)隊(duì)列消費(fèi)內(nèi)容\n";
print_r($data);
return callStatistics($data);
/* try {
return callStatistics($data);
}
catch ( \Exception $e ) {
Log::error('statistics 統(tǒng)計(jì)隊(duì)列消費(fèi)捕獲異常',[$e->getMessage(),[$e]]);
return $e->getMessage();
}*/
}
public function onConsumeFailure(\Throwable $e, $package)
{
echo "統(tǒng)計(jì)隊(duì)列消費(fèi)捕獲異常\n";
echo $e->getMessage();
// 將接收的數(shù)據(jù)寫入日志文件
$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);
}
}
執(zhí)行到 callStatistics 方法 ,我直接 拋出了異常, throw new ParamException('數(shù)據(jù)類型有誤');
在命令面板有拋出異常,但是我在 onConsumeFailure 下面獲取不到異常,命令面板也沒有執(zhí)行我echo 的提示 。
那位大佬遇到過這種問題,或者幫忙看看,需要怎么解決。
非常感謝。
API
Client::__construct()
Client::send()
Client::subscribe()
Client::unsubscribe()
Client::onConsumeFailure()
onConsumeFailure 不是寫在Statistics里
我認(rèn)為應(yīng)該是什么地方發(fā)起的Client::send() 在他下方寫Client::onConsumeFailure()
要不你看下http://wtbis.cn/doc/workerman/components/workerman-redis-queue.html 這里的示例
你可以試試看,老大的例子有的時(shí)候?qū)懙木褪遣惶?xì),他可能認(rèn)為大家是跟他一個(gè)水平.
謝謝,我是按 webman 里面的案例寫的,http://wtbis.cn/doc/webman/queue/redis.html#%E6%B6%88%E8%B4%B9 ;后面我試試你提供的方法看,初始想著他這個(gè)寫法應(yīng)該會(huì)有優(yōu)勢(shì),寫法簡(jiǎn)單易懂就用了。