AccountPostCountStatUpdate.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Profile;
  4. use App\Services\Account\AccountStatService;
  5. use App\Services\AccountService;
  6. use App\Status;
  7. use Illuminate\Console\Command;
  8. class AccountPostCountStatUpdate extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'app:account-post-count-stat-update';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Update post counts from recent activities';
  22. /**
  23. * Execute the console command.
  24. */
  25. public function handle()
  26. {
  27. $chunkSize = 100;
  28. $lastId = 0;
  29. while (true) {
  30. $ids = AccountStatService::getPostCountChunk($lastId, $chunkSize);
  31. if (empty($ids)) {
  32. break;
  33. }
  34. foreach ($ids as $id) {
  35. $this->processAccount($id);
  36. $lastId = $id;
  37. }
  38. if (function_exists('gc_collect_cycles')) {
  39. gc_collect_cycles();
  40. }
  41. }
  42. return 0;
  43. }
  44. private function processAccount($id)
  45. {
  46. $acct = AccountService::get($id, true);
  47. if (! $acct) {
  48. AccountStatService::removeFromPostCount($id);
  49. return;
  50. }
  51. $statusCount = Status::whereProfileId($id)->count();
  52. if ($statusCount != $acct['statuses_count']) {
  53. $profile = Profile::find($id);
  54. if (! $profile) {
  55. AccountStatService::removeFromPostCount($id);
  56. return;
  57. }
  58. $profile->status_count = $statusCount;
  59. $profile->save();
  60. AccountService::del($id);
  61. }
  62. AccountStatService::removeFromPostCount($id);
  63. }
  64. }