队列执行的问题,谁给解答一下?
1. 运行环境
1). 当前使用的 Laravel 版本?
Laravel Framework 10.13.0
2). 当前使用的 php/php-fpm 版本?
PHP 版本:8.1.21
3). 当前系统
Ubuntu 20.04.6 LTS (Focal Fossa) x86_64
4). 业务环境
开发环境,使用了阿里云的CLB负载均衡,两台代码服务器,一台队列服务器
2. 问题描述?
使用了微信商家转账到零钱,同时设置了多个支付商户号。
转账成功后添加到队列中进行转账明细的查询,转账订单记录了使用那个商户号进行的转账,问题出现在当切换支付商户号时,查询转账记录一直返回NOT_FOUND,重启一下supervisor后就有好了。下面是相关的代码。
namespace App\Jobs\User;
class QueryTransferDetailsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $userActivity;
public $out_batch_no;
public $out_detail_no;
public function __construct($userActivity, $params)
{
$this->userActivity = $userActivity;
$this->out_batch_no = $params['out_batch_no'];
$this->out_detail_no = $params['out_detail_no'];
}
public function handle(): void
{
$nowUserActivityInfo = UserActivity::find($this->userActivity->id);
$this->db_wechat_pay_id = $nowUserActivityInfo->wechat_pay_id;
$payMent = new MerchantTransferDetails($nowUserActivityInfo->wechat_pay_id);
// 转账明细查询
$rest = $payMent->QueryTransferDetails($this->out_batch_no, $this->out_detail_no);
// 查询失败
if ($rest['code'] == false) {
...
} else {
...
}
}
}
<?php
namespace App\Lib\Wechat\Payment;
use App\Models\WechatPay;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\Fund\Transfer\QueryOutBatchDetailNoPlugin;
/**
* 商家转账明细查询
*/
class MerchantTransferDetails
{
public $wechat_pay_id;
public function __construct($wechat_pay_id)
{
$this->wechat_pay_id = $wechat_pay_id;
}
public function getPayConfigForDB($wechat_pay_id)
{
$config = config('yansongpay');
if (Cache::has('wechatPayKey_'.$wechat_pay_id)) {
$dbConfig = Cache::get('wechatPayKey_'.$wechat_pay_id);
} else {
$dbConfig = $this->getPayConfigByWechatPayId($wechat_pay_id);
}
if (empty($dbConfig)) {
throw new \Exception("请先设置支付配置");
}
$config['wechat']['default']['mch_id'] = $dbConfig['wechat_mch_id'];
if ($dbConfig['mch_secret_key_v2']) {
$config['wechat']['default']['mch_secret_key_v2'] = $dbConfig['mch_secret_key_v2'];
}
$config['wechat']['default']['mch_secret_key'] = $dbConfig['wechat_mch_secret_key'];
$config['wechat']['default']['mch_secret_cert'] = $dbConfig['wechat_mch_secret_cert'];
$config['wechat']['default']['mch_public_cert_path'] = $dbConfig['wechat_mch_public_cert_path'];
$config['wechat']['default']['mp_app_id'] = $dbConfig['wechat_mp_app_id'];
$config['wechat']['default']['wechat_public_cert_path'] = $dbConfig['wechat_public_cert_path'];
return $config;
}
protected function getPayConfigByWechatPayId($wechat_pay_id){
$dbConfigInfo = WechatPay::find($wechat_pay_id);
$dbConfig = [
'wechat_pay_id' => $dbConfigInfo->id,
'wechat_mch_id' => $dbConfigInfo->wechat_mch_id,
'mch_secret_key_v2' => $dbConfigInfo->mch_secret_key_v2,
'wechat_mch_secret_key' => $dbConfigInfo->wechat_mch_secret_key,
'wechat_mch_secret_cert' => $dbConfigInfo->wechat_mch_secret_cert,
'wechat_mch_public_cert_path' => $dbConfigInfo->wechat_mch_public_cert_path,
'wechat_mp_app_id' => $dbConfigInfo->wechat_mp_app_id,
'wechat_public_cert_path' => $dbConfigInfo->wechat_public_cert_path,
];
// 添加缓存
Cache::forever('wechatPayKey_'.$wechat_pay_id, $dbConfig);
return $dbConfig;
}
/**
* 通过商家明细单号查询明细单
* @param $out_batch_no 【商家批次单号】 商户系统内部的商家批次单号,在商户系统内部唯一
* @param $out_detail_no 【商家明细单号】 商户系统内部区分转账批次单下不同转账明细单的唯一标识
* @return void
*/
public function QueryTransferDetails($out_batch_no, $out_detail_no)
{
try {
// 初始支付配置
$config = $this->getPayConfigForDB($this->wechat_pay_id);
Pay::config($config);
$params = [
'out_batch_no' => $out_batch_no,
'out_detail_no' => $out_detail_no,
];
Log::info("【out_batch_no】:" . $out_batch_no);
Log::info("【out_detail_no】:" . $out_detail_no);
Log::info("【wechat_pay_id】:" . $this->wechat_pay_id);
$allPlugins = Pay::wechat()->mergeCommonPlugins([QueryOutBatchDetailNoPlugin::class]);
$result = Pay::wechat()->pay($allPlugins, $params);
$response_data = $result->toArray();
Log::info("【查询结果】:" . json_encode($response_data));
return [
'code' => true,
'res_code' => 'SUCCESS',
'msg' => "查询成功",
'data' => $response_data];
} catch (\Exception $exception) {
return ['code' => false, 'res_code' => $exception->getCode(), 'msg' => $exception->getMessage()];
}
}
}
是不是在事务里面丢队列的 如果是的话需要加
->afterCommit()
是不是你这个东西是个单例啊 感觉数据被污染了 以前请求的和这次的混合在一起了
@cccde 它是不是因为这个的原因?
更改了代码和配置 本来就得重启supervisor吧
job的东西修改了以后必须要重启进程才会生效
队列是常驻内存的,估计你用的微信拓展包里面是单例,在初始化第一个商户的信息时就保存,后面你再切换商户去做初始化还是之前的商户信息。