优化同步
This commit is contained in:
parent
d4727afda4
commit
266f875c72
@ -5,6 +5,7 @@ namespace App\Domains\Virtual\Commands\Sync;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use App\Models\Card\Card;
|
use App\Models\Card\Card;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use App\Domains\Virtual\Jobs\CardSyncJob;
|
use App\Domains\Virtual\Jobs\CardSyncJob;
|
||||||
@ -40,16 +41,20 @@ class CardSync extends Command
|
|||||||
|
|
||||||
$page = 1;
|
$page = 1;
|
||||||
|
|
||||||
|
$jobs = new Collection();
|
||||||
|
|
||||||
while ($total) {
|
while ($total) {
|
||||||
dispatch(new CardSyncJob($page, $this->limit, $maxId));
|
$page++;
|
||||||
|
|
||||||
if ($page * $this->limit >= $total) {
|
if ($page * $this->limit >= $total) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$page++;
|
$jobs->push(new CardSyncJob($page, $this->limit, $maxId));
|
||||||
}
|
}
|
||||||
|
|
||||||
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
||||||
|
|
||||||
|
CardSyncJob::withChain($jobs->toArray())->dispatch(1, $this->limit, $maxId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,16 +46,20 @@ class LogSync extends Command
|
|||||||
|
|
||||||
$page = 1;
|
$page = 1;
|
||||||
|
|
||||||
|
$jobs = new Collection();
|
||||||
|
|
||||||
while ($total) {
|
while ($total) {
|
||||||
dispatch(new LogSyncJob($page, $this->limit, $maxId));
|
$page++;
|
||||||
|
|
||||||
if ($page * $this->limit >= $total) {
|
if ($page * $this->limit >= $total) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$page++;
|
$jobs->push(new LogSyncJob($page, $this->limit, $maxId));
|
||||||
}
|
}
|
||||||
|
|
||||||
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
app(ConfigService::class)->set(self::CURSOR_KEY, $nextId);
|
||||||
|
|
||||||
|
LogSyncJob::withChain($jobs->toArray())->dispatch(1, $this->limit, $maxId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,14 @@ use Illuminate\Support\Arr;
|
|||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Dipper\Foundation\Bus\Dispatchable;
|
use Dipper\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use App\Domains\Config\Services\ConfigService;
|
||||||
|
use App\Domains\Virtual\Commands\Sync\CardSync;
|
||||||
use App\Domains\Card\Repositories\BlocRepository;
|
use App\Domains\Card\Repositories\BlocRepository;
|
||||||
use App\Domains\Card\Repositories\CardRepository;
|
use App\Domains\Card\Repositories\CardRepository;
|
||||||
use App\Domains\Config\Services\ConfigService;
|
|
||||||
|
|
||||||
class CardSyncJob implements ShouldQueue
|
class CardSyncJob implements ShouldQueue
|
||||||
{
|
{
|
||||||
@ -54,6 +56,11 @@ class CardSyncJob implements ShouldQueue
|
|||||||
|
|
||||||
$res = $query->forPage($this->page, $this->limit)->get();
|
$res = $query->forPage($this->page, $this->limit)->get();
|
||||||
|
|
||||||
|
if (empty($res)) {
|
||||||
|
Log::notice('CardSyncJob not result!');
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
$array = [];
|
$array = [];
|
||||||
|
|
||||||
foreach ($res as $key => $value) {
|
foreach ($res as $key => $value) {
|
||||||
@ -89,8 +96,12 @@ class CardSyncJob implements ShouldQueue
|
|||||||
virtual_activated_at=excluded.virtual_activated_at,
|
virtual_activated_at=excluded.virtual_activated_at,
|
||||||
cancelled_at=excluded.cancelled_at';
|
cancelled_at=excluded.cancelled_at';
|
||||||
|
|
||||||
|
try {
|
||||||
$builder->connection->insert($sql, Arr::flatten($array, 1));
|
$builder->connection->insert($sql, Arr::flatten($array, 1));
|
||||||
|
|
||||||
app(CardRepository::class)->forgetCached();
|
app(CardRepository::class)->forgetCached();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$nextId = $res->first()->id - 1;
|
||||||
|
app(ConfigService::class)->set(CardSync::CURSOR_KEY, $nextId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,12 @@ use App\Models\Virtual\Order;
|
|||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Dipper\Foundation\Bus\Dispatchable;
|
use Dipper\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use App\Domains\Config\Services\ConfigService;
|
use App\Domains\Config\Services\ConfigService;
|
||||||
|
use App\Domains\Virtual\Commands\Sync\LogSync;
|
||||||
use App\Domains\Virtual\Services\ProductService;
|
use App\Domains\Virtual\Services\ProductService;
|
||||||
use App\Domains\Card\Repositories\BlocRepository;
|
use App\Domains\Card\Repositories\BlocRepository;
|
||||||
use App\Domains\Card\Repositories\CardRepository;
|
use App\Domains\Card\Repositories\CardRepository;
|
||||||
@ -73,6 +75,11 @@ class LogSyncJob implements ShouldQueue
|
|||||||
|
|
||||||
$res = $query->forPage($this->page, $this->limit)->get();
|
$res = $query->forPage($this->page, $this->limit)->get();
|
||||||
|
|
||||||
|
if (empty($res)) {
|
||||||
|
Log::notice('LogSyncJob not result!');
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
DB::beginTransaction();
|
DB::beginTransaction();
|
||||||
|
|
||||||
foreach ($res as $key => $value) {
|
foreach ($res as $key => $value) {
|
||||||
@ -143,6 +150,8 @@ class LogSyncJob implements ShouldQueue
|
|||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
DB::rollback();
|
DB::rollback();
|
||||||
|
$nextId = $res->first()->id - 1;
|
||||||
|
app(ConfigService::class)->set(LogSync::CURSOR_KEY, $nextId);
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user