FollowServiceWarmCacheLargeIngestPipeline.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 App\Services\AccountService;
  10. use App\Services\FollowerService;
  11. use Cache;
  12. use DB;
  13. use Storage;
  14. use App\Follower;
  15. use App\Profile;
  16. class FollowServiceWarmCacheLargeIngestPipeline implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. public $profileId;
  20. public $followType;
  21. public $tries = 5;
  22. public $timeout = 5000;
  23. public $failOnTimeout = false;
  24. /**
  25. * Create a new job instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct($profileId, $followType = 'following')
  30. {
  31. $this->profileId = $profileId;
  32. $this->followType = $followType;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. $pid = $this->profileId;
  42. $type = $this->followType;
  43. if($type === 'followers') {
  44. $key = 'follow-warm-cache/' . $pid . '/followers.json';
  45. if(!Storage::exists($key)) {
  46. return;
  47. }
  48. $file = Storage::get($key);
  49. $json = json_decode($file, true);
  50. foreach($json as $id) {
  51. FollowerService::add($id, $pid, false);
  52. usleep(random_int(500, 3000));
  53. }
  54. sleep(5);
  55. Storage::delete($key);
  56. }
  57. if($type === 'following') {
  58. $key = 'follow-warm-cache/' . $pid . '/following.json';
  59. if(!Storage::exists($key)) {
  60. return;
  61. }
  62. $file = Storage::get($key);
  63. $json = json_decode($file, true);
  64. foreach($json as $id) {
  65. FollowerService::add($pid, $id, false);
  66. usleep(random_int(500, 3000));
  67. }
  68. sleep(5);
  69. Storage::delete($key);
  70. }
  71. sleep(random_int(2, 5));
  72. $files = Storage::files('follow-warm-cache/' . $pid);
  73. if(empty($files)) {
  74. Storage::deleteDirectory('follow-warm-cache/' . $pid);
  75. }
  76. }
  77. }