From 1f75f5986f0aff7f95888b8bb3965f023ee0624f Mon Sep 17 00:00:00 2001 From: adonis Date: Tue, 7 Jul 2026 16:35:14 +0330 Subject: [PATCH] added test for channel 2 --- app/Services/LivePriceTickService.php | 8 ++-- app/Services/PriceChannelBroadcastService.php | 47 +++++++++++++++++-- config/logging.php | 2 +- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/app/Services/LivePriceTickService.php b/app/Services/LivePriceTickService.php index 7f32d8a..6b51d56 100644 --- a/app/Services/LivePriceTickService.php +++ b/app/Services/LivePriceTickService.php @@ -86,12 +86,10 @@ class LivePriceTickService app(LivePriceCacheService::class)->markTick(); // last از put داخل lastPrice؛ list بعد از sync محصولات - $listChanged = app(PriceBroadcastService::class)->broadcastListIfChanged(); + app(PriceBroadcastService::class)->broadcastListIfChanged(); - // بعد از هر تغییر قیمت، قیمت‌ها را به کانال‌های پیام‌رسان بفرست (با cooldown داخلی) - if ($listChanged) { - app(PriceChannelBroadcastService::class)->broadcastIfEligible(); - } + // ارسال قیمت به کانال‌ها — مستقل از SSE؛ تشخیص تغییر قیمت + cooldown داخل سرویس است + app(PriceChannelBroadcastService::class)->broadcastIfEligible(); } finally { $lock->release(); } diff --git a/app/Services/PriceChannelBroadcastService.php b/app/Services/PriceChannelBroadcastService.php index 53066a9..8353a57 100644 --- a/app/Services/PriceChannelBroadcastService.php +++ b/app/Services/PriceChannelBroadcastService.php @@ -20,6 +20,8 @@ class PriceChannelBroadcastService { private const COOLDOWN_CACHE_KEY = 'price_channel:last_sent_at'; + private const SIGNATURE_CACHE_KEY = 'price_channel:last_signature'; + private const SUPPORTED_MESSENGERS = ['bale', 'telegram', 'rubika']; /** مثقال بازار آبشده (گرم) — وقتی تنظیم change_mesghal خالی باشد */ @@ -42,6 +44,19 @@ class PriceChannelBroadcastService $targets = $this->resolveActiveTargets(); if ($targets === []) { + Log::debug('PriceChannel: enabled ولی هیچ مقصد فعالی نیست (توکن/مقصد را چک کن)'); + return; + } + + $products = $this->eligibleProducts(); + if ($products === []) { + Log::debug('PriceChannel: محصول واجد شرایطی نیست (active/قیمت/ساعت کاری)'); + return; + } + + // فقط وقتی قیمت واقعاً تغییر کرده باشد + $signature = $this->priceSignature($products); + if ($signature === Cache::get(self::SIGNATURE_CACHE_KEY)) { return; } @@ -49,21 +64,41 @@ class PriceChannelBroadcastService return; } - $message = $this->buildMessage(); + $message = $this->buildMessage($products); if ($message === '') { return; } + $results = []; foreach ($targets as $messenger => $target) { - $this->dispatchToMessenger($messenger, $target['token'], $target['chat_ids'], $message); + $results[$messenger] = $this->dispatchToMessenger($messenger, $target['token'], $target['chat_ids'], $message); } $this->markSent(); + Cache::put(self::SIGNATURE_CACHE_KEY, $signature, now()->addDay()); + + Log::info('PriceChannel: قیمت به کانال ارسال شد', [ + 'targets' => array_keys($targets), + 'products' => count($products), + 'results' => $results, + ]); } catch (\Throwable $e) { Log::warning('PriceChannelBroadcast failed', ['message' => $e->getMessage()]); } } + /** امضای قیمت محصولات واجد شرایط — برای تشخیص تغییر قیمت */ + private function priceSignature(array $products): string + { + $parts = []; + foreach ($products as $product) { + $parts[] = $product->id . ':' . (int) ($product->price ?? 0) + . ':' . ((int) ($product->open_buy)) . ((int) ($product->open_sell)); + } + + return md5(implode('|', $parts)); + } + /** * ارسال دستی (عیب‌یابی) — cooldown و شرط تغییر قیمت را نادیده می‌گیرد. * وضعیت روشن/خاموش، انتخاب پیام‌رسان، توکن و مقصد همچنان لازم است. @@ -84,6 +119,10 @@ class PriceChannelBroadcastService if ($message !== '' && $targets !== []) { $this->markSent(); + $products = $this->eligibleProducts(); + if ($products !== []) { + Cache::put(self::SIGNATURE_CACHE_KEY, $this->priceSignature($products), now()->addDay()); + } } return [ @@ -185,9 +224,9 @@ class PriceChannelBroadcastService /** * ساخت متن پیام قیمت — مرتب و بر اساس داده‌ی هر محصول. */ - public function buildMessage(): string + public function buildMessage(?array $products = null): string { - $products = $this->eligibleProducts(); + $products = $products ?? $this->eligibleProducts(); if ($products === []) { return ''; } diff --git a/config/logging.php b/config/logging.php index ca8e083..d477f12 100644 --- a/config/logging.php +++ b/config/logging.php @@ -51,7 +51,7 @@ return [ 'stack' => [ 'driver' => 'stack', - 'channels' => ['single'], + 'channels' => ['daily'], 'ignore_exceptions' => false, ],