FeedInsertPipeline.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Jobs\HomeFeedPipeline;
  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 Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  11. use App\Status;
  12. use App\Follower;
  13. use App\Services\FollowerService;
  14. use App\Services\HomeTimelineService;
  15. class FeedInsertPipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $status;
  19. public $timeout = 900;
  20. public $tries = 3;
  21. public $maxExceptions = 1;
  22. public $failOnTimeout = true;
  23. /**
  24. * The number of seconds after which the job's unique lock will be released.
  25. *
  26. * @var int
  27. */
  28. public $uniqueFor = 3600;
  29. /**
  30. * Get the unique ID for the job.
  31. */
  32. public function uniqueId(): string
  33. {
  34. return 'hfp:f-insert:sid:' . $this->status->id;
  35. }
  36. /**
  37. * Get the middleware the job should pass through.
  38. *
  39. * @return array<int, object>
  40. */
  41. public function middleware(): array
  42. {
  43. return [(new WithoutOverlapping("hfp:f-insert:sid:{$this->status->id}"))->shared()->dontRelease()];
  44. }
  45. /**
  46. * Create a new job instance.
  47. */
  48. public function __construct(Status $status)
  49. {
  50. $this->status = $status;
  51. }
  52. /**
  53. * Execute the job.
  54. */
  55. public function handle(): void
  56. {
  57. $status = $this->status;
  58. $sid = $status->id;
  59. $pid = $status->profile_id;
  60. $ids = FollowerService::localFollowerIds($pid);
  61. foreach($ids as $id) {
  62. HomeTimelineService::add($id, $sid);
  63. }
  64. }
  65. }