fix price force refresh 2
This commit is contained in:
@@ -5,44 +5,79 @@ namespace App\Services;
|
||||
use App\Http\Controllers\Site\ReportController;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class LivePriceTickService
|
||||
{
|
||||
private const LOCK_SECONDS = 25;
|
||||
|
||||
public function tick(): void
|
||||
{
|
||||
$config = app(ConfigService::class);
|
||||
$sync = app(ProductPriceSyncService::class);
|
||||
|
||||
if ($config->isStore()) {
|
||||
$sync->syncStorePrices(true);
|
||||
$source = 'zaarbox';
|
||||
$sourceUrl = app(ZaarboxPriceService::class)->apiUrl();
|
||||
} else {
|
||||
$sync->syncAll(true);
|
||||
$source = $config->priceProvider() ?: 'legacy';
|
||||
$sourceUrl = $source === 'zaarbox'
|
||||
? app(ZaarboxPriceService::class)->apiUrl()
|
||||
: null;
|
||||
$lock = Cache::lock('prices:tick', self::LOCK_SECONDS);
|
||||
if (! $lock->get()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$liquidGold = (int) $config->getInt('montzar_live_price', 0);
|
||||
if ($liquidGold <= 0) {
|
||||
$liquidGold = app(ProductDisplayPriceService::class)->resolveHistoricalBasePrice();
|
||||
}
|
||||
try {
|
||||
$config = app(ConfigService::class);
|
||||
$sync = app(ProductPriceSyncService::class);
|
||||
$zaarbox = app(ZaarboxPriceService::class);
|
||||
|
||||
$activeProducts = Product::query()
|
||||
->where('active', 1)
|
||||
->whereNotNull('reading_method')
|
||||
->count();
|
||||
/*
|
||||
| ۱. montzar / zaarbox زنده + کش ۱۵ثانیهای /api/price/last
|
||||
| (قبل از sync محصولات تا آبشدهٔ «قیمت اپ» تازه باشد)
|
||||
*/
|
||||
$request = Request::create('/api/price/last', 'GET', ['force_refresh' => '1']);
|
||||
$response = app(ReportController::class)->lastPrice($request);
|
||||
$payload = json_decode($response->getContent(), true);
|
||||
if (! is_array($payload)) {
|
||||
$payload = [];
|
||||
}
|
||||
|
||||
app(PriceSyncLogService::class)->recordTick($source, $sourceUrl, $liquidGold, $activeProducts);
|
||||
/*
|
||||
| ۲. فید zaarbox برای محصولات متغیر (reading_method)
|
||||
*/
|
||||
if ($zaarbox->isEnabled() || Product::query()->whereNotNull('reading_method')->exists()) {
|
||||
try {
|
||||
$zaarbox->cachedPriceList(true);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('Zaarbox feed refresh in tick failed', ['message' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
$request = Request::create('/api/price/last', 'GET', ['force_refresh' => '1']);
|
||||
$response = app(ReportController::class)->lastPrice($request);
|
||||
$payload = json_decode($response->getContent(), true);
|
||||
/*
|
||||
| ۳. همگامسازی قیمت محصولات
|
||||
*/
|
||||
if ($config->isStore()) {
|
||||
$sync->syncStorePrices(true);
|
||||
$source = 'zaarbox';
|
||||
$sourceUrl = $zaarbox->apiUrl();
|
||||
} else {
|
||||
$sync->syncAll(true);
|
||||
$source = $config->priceProvider() ?: 'legacy';
|
||||
$sourceUrl = $source === 'zaarbox'
|
||||
? $zaarbox->apiUrl()
|
||||
: null;
|
||||
}
|
||||
|
||||
if (is_array($payload)) {
|
||||
app(PriceSnapshotService::class)->recordTick($payload);
|
||||
$liquidGold = (int) ($payload['price'] ?? $config->getInt('montzar_live_price', 0));
|
||||
if ($liquidGold <= 0) {
|
||||
$liquidGold = app(ProductDisplayPriceService::class)->resolveHistoricalBasePrice();
|
||||
}
|
||||
|
||||
$activeProducts = Product::query()
|
||||
->where('active', 1)
|
||||
->whereNotNull('reading_method')
|
||||
->count();
|
||||
|
||||
app(PriceSyncLogService::class)->recordTick($source, $sourceUrl, $liquidGold, $activeProducts);
|
||||
|
||||
if ($payload !== []) {
|
||||
app(PriceSnapshotService::class)->recordTick($payload);
|
||||
}
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user