ProfileMigrationMoveFollowersPipeline.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Jobs\ProfilePipeline;
  3. use App\Follower;
  4. use App\Profile;
  5. use App\Services\AccountService;
  6. use Illuminate\Bus\Batchable;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\Middleware\WithoutOverlapping;
  13. use Illuminate\Queue\SerializesModels;
  14. class ProfileMigrationMoveFollowersPipeline implements ShouldBeUniqueUntilProcessing, ShouldQueue
  15. {
  16. use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. public $oldPid;
  18. public $newPid;
  19. public $timeout = 1400;
  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 'profile:migration:move-followers:oldpid-'.$this->oldPid.':newpid-'.$this->newPid;
  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('profile:migration:move-followers:oldpid-'.$this->oldPid.':newpid-'.$this->newPid))->shared()->dontRelease()];
  44. }
  45. /**
  46. * Create a new job instance.
  47. */
  48. public function __construct($oldPid, $newPid)
  49. {
  50. $this->oldPid = $oldPid;
  51. $this->newPid = $newPid;
  52. }
  53. /**
  54. * Execute the job.
  55. */
  56. public function handle(): void
  57. {
  58. if ($this->batch()->cancelled()) {
  59. return;
  60. }
  61. $og = Profile::find($this->oldPid);
  62. $ne = Profile::find($this->newPid);
  63. if (! $og || ! $ne || $og == $ne) {
  64. return;
  65. }
  66. $ne->followers_count = $og->followers_count;
  67. $ne->save();
  68. $og->followers_count = 0;
  69. $og->save();
  70. foreach (Follower::whereFollowingId($this->oldPid)->lazyById(200, 'id') as $follower) {
  71. try {
  72. $follower->following_id = $this->newPid;
  73. $follower->save();
  74. } catch (Exception $e) {
  75. $follower->delete();
  76. }
  77. }
  78. AccountService::del($this->oldPid);
  79. AccountService::del($this->newPid);
  80. }
  81. }