diff --git a/app/Http/Controllers/Site/ReportController.php b/app/Http/Controllers/Site/ReportController.php index 7ad6c03..f744114 100644 --- a/app/Http/Controllers/Site/ReportController.php +++ b/app/Http/Controllers/Site/ReportController.php @@ -22,6 +22,7 @@ use Illuminate\Support\Facades\Mail; use Morilog\Jalali\Jalalian; use App\Services\ConfigService; use App\Services\LivePriceCacheService; +use App\Services\PriceBroadcastService; use App\Services\ProductDisplayPriceService; @@ -132,7 +133,10 @@ class ReportController extends Controller $cache = app(LivePriceCacheService::class); $cache->markTick(); - return $cache->put($payload); + $result = $cache->put($payload); + app(PriceBroadcastService::class)->broadcastAfterPriceUpdate($result, true); + + return $result; } return $payload; diff --git a/app/Services/PriceBroadcastService.php b/app/Services/PriceBroadcastService.php index a53fcde..92ebccc 100644 --- a/app/Services/PriceBroadcastService.php +++ b/app/Services/PriceBroadcastService.php @@ -13,56 +13,53 @@ class PriceBroadcastService private const LIST_HASH_KEY = 'ws_broadcast:list_hash'; /** + * montzar فقط trigger «price_update» را به کلاینت‌ها relay می‌کند. + * * @param array|null $lastPayload */ - public function broadcastAfterPriceUpdate(?array $lastPayload = null): void + public function broadcastAfterPriceUpdate(?array $lastPayload = null, bool $force = false): void { $ws = app(WebsocketBroadcastService::class); if (! $ws->isEnabled()) { return; } + if ($force) { + $ws->sendEvent('price_update', []); + + return; + } + if ($lastPayload === null) { $cache = app(LivePriceCacheService::class); $lastPayload = $cache->get() ?? $cache->buildFromDatabase(); } - if (is_array($lastPayload) && $lastPayload !== []) { - $this->broadcastLastIfChanged($lastPayload); - } + $lastChanged = is_array($lastPayload) && $lastPayload !== [] + && $this->rememberHashIfChanged(self::LAST_HASH_KEY, $lastPayload); $listPayload = $this->buildProductListPayload(); - if ($listPayload !== null) { - $this->broadcastListIfChanged($listPayload); + $listChanged = $listPayload !== null + && $this->rememberHashIfChanged(self::LIST_HASH_KEY, $listPayload); + + if ($lastChanged || $listChanged) { + $ws->sendEvent('price_update', []); } } /** - * @param array $payload + * @param array|array $payload */ - public function broadcastLastIfChanged(array $payload): void + private function rememberHashIfChanged(string $cacheKey, array $payload): bool { $hash = md5(json_encode($payload, JSON_UNESCAPED_UNICODE)); - if (Cache::get(self::LAST_HASH_KEY) === $hash) { - return; + if (Cache::get($cacheKey) === $hash) { + return false; } - Cache::put(self::LAST_HASH_KEY, $hash, now()->addHour()); - app(WebsocketBroadcastService::class)->sendEvent('last', $payload); - } + Cache::put($cacheKey, $hash, now()->addHour()); - /** - * @param array $products - */ - public function broadcastListIfChanged(array $products): void - { - $hash = md5(json_encode($products, JSON_UNESCAPED_UNICODE)); - if (Cache::get(self::LIST_HASH_KEY) === $hash) { - return; - } - - Cache::put(self::LIST_HASH_KEY, $hash, now()->addHour()); - app(WebsocketBroadcastService::class)->sendEvent('list', ['data' => $products]); + return true; } /** diff --git a/app/Services/WebsocketBroadcastService.php b/app/Services/WebsocketBroadcastService.php index 845c594..7b35c6d 100644 --- a/app/Services/WebsocketBroadcastService.php +++ b/app/Services/WebsocketBroadcastService.php @@ -82,7 +82,7 @@ class WebsocketBroadcastService $url = $baseUrl.$this->triggerPath(); try { - Http::timeout(3) + $response = Http::timeout(3) ->withHeaders(['Content-Type' => 'application/json']) ->withOptions([ 'curl' => [ @@ -90,6 +90,15 @@ class WebsocketBroadcastService ], ]) ->post($url, $body); + + if (! $response->successful()) { + Log::warning('Websocket broadcast rejected', [ + 'trigger' => $trigger, + 'url' => $url, + 'status' => $response->status(), + 'body' => $response->body(), + ]); + } } catch (\Throwable $e) { Log::warning('Websocket broadcast failed', [ 'trigger' => $trigger,