update: order queue

This commit is contained in:
tokumeikoi 2021-09-04 16:31:25 +09:00
parent c2577e37c4
commit c80d93fa25
8 changed files with 67 additions and 43 deletions

View File

@ -2,6 +2,7 @@
namespace App\Console\Commands;
use App\Jobs\OrderHandleJob;
use App\Services\OrderService;
use Illuminate\Console\Command;
use App\Models\Order;
@ -45,20 +46,8 @@ class CheckOrder extends Command
ini_set('memory_limit', -1);
$orders = Order::whereIn('status', [0, 1])
->get();
foreach ($orders as $item) {
$orderService = new OrderService($item);
switch ($item->status) {
// cancel
case 0:
if ($item->created_at <= (time() - 1800)) {
$orderService->cancel();
}
break;
case 1:
$orderService->open();
break;
}
foreach ($orders as $order) {
OrderHandleJob::dispatch($order->trade_no);
}
}
}

View File

@ -78,16 +78,14 @@ class V2boardStatistics extends Command
private function statServer()
{
$endAt = strtotime(date('Y-m-d'));
$startAt = strtotime('-1 day', $endAt);
$startAt = strtotime(date('Y-m-d'));
$statistics = ServerLog::select([
'server_id',
'method as server_type',
DB::raw("sum(u) as u"),
DB::raw("sum(d) as d"),
])
->where('log_at', '>=', $startAt)
->where('log_at', '<', $endAt)
->where('log_at', $startAt)
->groupBy('server_id', 'method')
->get()
->toArray();

View File

@ -169,9 +169,8 @@ class OrderController extends Controller
}
// free process
if ($order->total_amount <= 0) {
$order->total_amount = 0;
$order->status = 1;
$order->save();
$orderService = new OrderService($order);
if (!$orderService->paid($order->trade_no)) abort(500, '');
return response([
'type' => -1,
'data' => true

View File

@ -0,0 +1,52 @@
<?php
namespace App\Jobs;
use App\Models\Order;
use App\Services\OrderService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class OrderHandleJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $order;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($tradeNo)
{
$this->onQueue('order_handle');
$this->order = Order::where('trade_no', $tradeNo)
->lockForUpdate()
->first();
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (!$this->order) return;
$orderService = new OrderService($this->order);
switch ($this->order->status) {
// cancel
case 0:
if ($this->order->created_at <= (time() - 1800)) {
$orderService->cancel();
}
break;
case 1:
$orderService->open();
break;
}
}
}

View File

@ -2,6 +2,7 @@
namespace App\Services;
use App\Jobs\OrderHandleJob;
use App\Models\Order;
use App\Models\Plan;
use App\Models\User;
@ -220,7 +221,9 @@ class OrderService
$order->status = 1;
$order->paid_at = time();
$order->callback_no = $callbackNo;
return $order->save();
if (!$order->save()) return false;
OrderHandleJob::dispatch($order->trade_no);
return true;
}
public function cancel():bool

View File

@ -165,24 +165,6 @@ return [
*/
'environments' => [
'production' => [
'V2board' => [
'connection' => 'redis',
'queue' => [
'traffic_fetch',
'server_log',
'send_email',
'send_telegram',
'stat_server'
],
'balance' => 'auto',
'minProcesses' => 1,
'maxProcesses' => 64,
'tries' => 1,
'nice' => 0,
],
],
'local' => [
'V2board' => [
'connection' => 'redis',
@ -191,7 +173,8 @@ return [
'server_log',
'send_email',
'send_telegram',
'stat_server'
'stat_server',
'order_handle'
],
'balance' => 'auto',
'minProcesses' => 1,

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long