
dispatch(new OrderUnpaidTimeEndJob($userId, $order->id));

延迟5秒 
<?php
namespace App\Jobs;
use App\Exceptions\BusinessException;
use App\Services\Order\OrderServices;
use App\Services\SystemServices;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class OrderUnpaidTimeEndJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $userId;
private $orderId;
public function __construct($userId, $orderId)
{
$this->userId = $userId;
$this->orderId = $orderId;
$delayTime = SystemServices::getInstance()->getOrderUnpaidDelayMinutes();
$this->delay(now()->addMinutes($delayTime));
}
public function handle()
{
try {
OrderServices::getInstance()->systemCancel($this->userId, $this->orderId);
} catch (BusinessException $exception) {
}
}
}
设置延时任务
public function getOrderUnpaidDelayMinutes()
{
return (int) $this->get(self::LITEMALL_ORDER_UNPAID);
}
public function systemCancel($userId, $orderId)
{
DB::transaction(function () use ($userId, $orderId) {
$this->cancel($userId, $orderId, 'system');
});
}
|