1
0

FollowServiceWarmCache.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Jobs\FollowPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Queue\Middleware\WithoutOverlapping;
  10. use App\Services\AccountService;
  11. use App\Services\FollowerService;
  12. use Cache;
  13. use DB;
  14. use Storage;
  15. use App\Follower;
  16. use App\Profile;
  17. class FollowServiceWarmCache implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. public $profileId;
  21. public $tries = 5;
  22. public $timeout = 5000;
  23. public $failOnTimeout = false;
  24. /**
  25. * Get the middleware the job should pass through.
  26. *
  27. * @return array<int, object>
  28. */
  29. public function middleware(): array
  30. {
  31. return [(new WithoutOverlapping($this->profileId))->dontRelease()];
  32. }
  33. /**
  34. * Create a new job instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct($profileId)
  39. {
  40. $this->profileId = $profileId;
  41. }
  42. /**
  43. * Execute the job.
  44. *
  45. * @return void
  46. */
  47. public function handle()
  48. {
  49. $id = $this->profileId;
  50. if(Cache::has(FollowerService::FOLLOWERS_SYNC_KEY . $id) && Cache::has(FollowerService::FOLLOWING_SYNC_KEY . $id)) {
  51. return;
  52. }
  53. $account = AccountService::get($id, true);
  54. if(!$account) {
  55. Cache::put(FollowerService::FOLLOWERS_SYNC_KEY . $id, 1, 604800);
  56. Cache::put(FollowerService::FOLLOWING_SYNC_KEY . $id, 1, 604800);
  57. return;
  58. }
  59. $hasFollowerPostProcessing = false;
  60. $hasFollowingPostProcessing = false;
  61. if(Follower::whereProfileId($id)->orWhere('following_id', $id)->count()) {
  62. $following = [];
  63. $followers = [];
  64. foreach(Follower::where('following_id', $id)->orWhere('profile_id', $id)->lazyById(500) as $follow) {
  65. if($follow->following_id != $id && $follow->profile_id != $id) {
  66. continue;
  67. }
  68. if($follow->profile_id == $id) {
  69. $following[] = $follow->following_id;
  70. } else {
  71. $followers[] = $follow->profile_id;
  72. }
  73. }
  74. if(count($followers) > 100) {
  75. // store follower ids and process in another job
  76. Storage::put('follow-warm-cache/' . $id . '/followers.json', json_encode($followers));
  77. $hasFollowerPostProcessing = true;
  78. } else {
  79. foreach($followers as $follower) {
  80. FollowerService::add($follower, $id);
  81. }
  82. }
  83. if(count($following) > 100) {
  84. // store following ids and process in another job
  85. Storage::put('follow-warm-cache/' . $id . '/following.json', json_encode($following));
  86. $hasFollowingPostProcessing = true;
  87. } else {
  88. foreach($following as $following) {
  89. FollowerService::add($id, $following);
  90. }
  91. }
  92. }
  93. Cache::put(FollowerService::FOLLOWERS_SYNC_KEY . $id, 1, 604800);
  94. Cache::put(FollowerService::FOLLOWING_SYNC_KEY . $id, 1, 604800);
  95. $profile = Profile::find($id);
  96. if($profile) {
  97. $profile->following_count = DB::table('followers')->whereProfileId($id)->count();
  98. $profile->followers_count = DB::table('followers')->whereFollowingId($id)->count();
  99. $profile->save();
  100. }
  101. AccountService::del($id);
  102. if($hasFollowingPostProcessing) {
  103. FollowServiceWarmCacheLargeIngestPipeline::dispatch($id, 'following')->onQueue('follow');
  104. }
  105. if($hasFollowerPostProcessing) {
  106. FollowServiceWarmCacheLargeIngestPipeline::dispatch($id, 'followers')->onQueue('follow');
  107. }
  108. return;
  109. }
  110. }