added wss tools 3

This commit is contained in:
2026-06-18 15:25:49 +03:30
parent 30b0e480c3
commit 2a77c24dcc
3 changed files with 37 additions and 27 deletions
@@ -22,6 +22,7 @@ use Illuminate\Support\Facades\Mail;
use Morilog\Jalali\Jalalian; use Morilog\Jalali\Jalalian;
use App\Services\ConfigService; use App\Services\ConfigService;
use App\Services\LivePriceCacheService; use App\Services\LivePriceCacheService;
use App\Services\PriceBroadcastService;
use App\Services\ProductDisplayPriceService; use App\Services\ProductDisplayPriceService;
@@ -132,7 +133,10 @@ class ReportController extends Controller
$cache = app(LivePriceCacheService::class); $cache = app(LivePriceCacheService::class);
$cache->markTick(); $cache->markTick();
return $cache->put($payload); $result = $cache->put($payload);
app(PriceBroadcastService::class)->broadcastAfterPriceUpdate($result, true);
return $result;
} }
return $payload; return $payload;
+22 -25
View File
@@ -13,56 +13,53 @@ class PriceBroadcastService
private const LIST_HASH_KEY = 'ws_broadcast:list_hash'; private const LIST_HASH_KEY = 'ws_broadcast:list_hash';
/** /**
* montzar فقط trigger «price_update» را به کلاینت‌ها relay می‌کند.
*
* @param array<string, mixed>|null $lastPayload * @param array<string, mixed>|null $lastPayload
*/ */
public function broadcastAfterPriceUpdate(?array $lastPayload = null): void public function broadcastAfterPriceUpdate(?array $lastPayload = null, bool $force = false): void
{ {
$ws = app(WebsocketBroadcastService::class); $ws = app(WebsocketBroadcastService::class);
if (! $ws->isEnabled()) { if (! $ws->isEnabled()) {
return; return;
} }
if ($force) {
$ws->sendEvent('price_update', []);
return;
}
if ($lastPayload === null) { if ($lastPayload === null) {
$cache = app(LivePriceCacheService::class); $cache = app(LivePriceCacheService::class);
$lastPayload = $cache->get() ?? $cache->buildFromDatabase(); $lastPayload = $cache->get() ?? $cache->buildFromDatabase();
} }
if (is_array($lastPayload) && $lastPayload !== []) { $lastChanged = is_array($lastPayload) && $lastPayload !== []
$this->broadcastLastIfChanged($lastPayload); && $this->rememberHashIfChanged(self::LAST_HASH_KEY, $lastPayload);
}
$listPayload = $this->buildProductListPayload(); $listPayload = $this->buildProductListPayload();
if ($listPayload !== null) { $listChanged = $listPayload !== null
$this->broadcastListIfChanged($listPayload); && $this->rememberHashIfChanged(self::LIST_HASH_KEY, $listPayload);
if ($lastChanged || $listChanged) {
$ws->sendEvent('price_update', []);
} }
} }
/** /**
* @param array<string, mixed> $payload * @param array<string, mixed>|array<int, mixed> $payload
*/ */
public function broadcastLastIfChanged(array $payload): void private function rememberHashIfChanged(string $cacheKey, array $payload): bool
{ {
$hash = md5(json_encode($payload, JSON_UNESCAPED_UNICODE)); $hash = md5(json_encode($payload, JSON_UNESCAPED_UNICODE));
if (Cache::get(self::LAST_HASH_KEY) === $hash) { if (Cache::get($cacheKey) === $hash) {
return; return false;
} }
Cache::put(self::LAST_HASH_KEY, $hash, now()->addHour()); Cache::put($cacheKey, $hash, now()->addHour());
app(WebsocketBroadcastService::class)->sendEvent('last', $payload);
}
/** return true;
* @param array<int, mixed> $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]);
} }
/** /**
+10 -1
View File
@@ -82,7 +82,7 @@ class WebsocketBroadcastService
$url = $baseUrl.$this->triggerPath(); $url = $baseUrl.$this->triggerPath();
try { try {
Http::timeout(3) $response = Http::timeout(3)
->withHeaders(['Content-Type' => 'application/json']) ->withHeaders(['Content-Type' => 'application/json'])
->withOptions([ ->withOptions([
'curl' => [ 'curl' => [
@@ -90,6 +90,15 @@ class WebsocketBroadcastService
], ],
]) ])
->post($url, $body); ->post($url, $body);
if (! $response->successful()) {
Log::warning('Websocket broadcast rejected', [
'trigger' => $trigger,
'url' => $url,
'status' => $response->status(),
'body' => $response->body(),
]);
}
} catch (\Throwable $e) { } catch (\Throwable $e) {
Log::warning('Websocket broadcast failed', [ Log::warning('Websocket broadcast failed', [
'trigger' => $trigger, 'trigger' => $trigger,