AdminRealtimeExchangeList.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Services\AdminRealtime;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Schema;
  5. final class AdminRealtimeExchangeList
  6. {
  7. public const RESOURCE = 'exchange_list';
  8. public const CHANNEL = 'admin.operation.exchange_list';
  9. public const EVENT = 'exchange_list.updated';
  10. /** Called inside the feed transaction while it holds the state row lock. */
  11. public static function collectChanges(): void
  12. {
  13. if (!Schema::hasTable('admin_exchange_list_changes')) return;
  14. $buffer = DB::table('admin_exchange_list_changes');
  15. $ids = (clone $buffer)->orderBy('id')->limit(2000)->pluck('id');
  16. if ($ids->isEmpty()) return;
  17. DB::table('admin_realtime_events')->insert([
  18. 'resource' => self::RESOURCE,
  19. 'resource_id' => '1',
  20. 'action' => 'updated',
  21. 'source' => 'bot-28',
  22. 'created_at' => now('Asia/Shanghai')->format('Y-m-d H:i:s'),
  23. ]);
  24. // Delete only the visible rows. A long transaction can commit a lower ID later.
  25. $buffer->whereIntegerInRaw('id', $ids->all())->delete();
  26. }
  27. }