fix: Fix OrderHandleJob to prevent Lock wait timeout.

This commit is contained in:
xboard
2024-07-19 02:30:51 +08:00
parent f9e4a978fd
commit 6a32cf28fb
+9 -7
View File
@@ -14,6 +14,7 @@ class OrderHandleJob implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $order; protected $order;
protected $tradeNo;
public $tries = 3; public $tries = 3;
public $timeout = 5; public $timeout = 5;
@@ -25,9 +26,7 @@ class OrderHandleJob implements ShouldQueue
public function __construct($tradeNo) public function __construct($tradeNo)
{ {
$this->onQueue('order_handle'); $this->onQueue('order_handle');
$this->order = Order::where('trade_no', $tradeNo) $this->tradeNo = $tradeNo;
->lockForUpdate()
->first();
} }
/** /**
@@ -37,12 +36,15 @@ class OrderHandleJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
if (!$this->order) return; $order = Order::where('trade_no', $this->tradeNo)
$orderService = new OrderService($this->order); ->lockForUpdate()
switch ($this->order->status) { ->first();
if (!$order) return;
$orderService = new OrderService($order);
switch ($order->status) {
// cancel // cancel
case 0: case 0:
if ($this->order->created_at <= (time() - 3600 * 2)) { if ($order->created_at <= (time() - 3600 * 2)) {
$orderService->cancel(); $orderService->cancel();
} }
break; break;