1
0

ProfilePurgeNotificationsByDomain.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Jobs\ProfilePipeline;
  3. use Illuminate\Bus\Batchable;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Queue\Middleware\WithoutOverlapping;
  11. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  12. use App\Notification;
  13. use DB;
  14. use App\Services\NotificationService;
  15. class ProfilePurgeNotificationsByDomain implements ShouldQueue, ShouldBeUniqueUntilProcessing
  16. {
  17. use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $pid;
  19. protected $domain;
  20. public $timeout = 900;
  21. public $tries = 3;
  22. public $maxExceptions = 1;
  23. public $failOnTimeout = true;
  24. /**
  25. * The number of seconds after which the job's unique lock will be released.
  26. *
  27. * @var int
  28. */
  29. public $uniqueFor = 3600;
  30. /**
  31. * Get the unique ID for the job.
  32. */
  33. public function uniqueId(): string
  34. {
  35. return 'notify:v1:purge-by-domain:' . $this->pid . ':d-' . $this->domain;
  36. }
  37. /**
  38. * Get the middleware the job should pass through.
  39. *
  40. * @return array<int, object>
  41. */
  42. public function middleware(): array
  43. {
  44. return [(new WithoutOverlapping("notify:v1:purge-by-domain:{$this->pid}:d-{$this->domain}"))->shared()->dontRelease()];
  45. }
  46. /**
  47. * Create a new job instance.
  48. */
  49. public function __construct($pid, $domain)
  50. {
  51. $this->pid = $pid;
  52. $this->domain = $domain;
  53. }
  54. /**
  55. * Execute the job.
  56. */
  57. public function handle(): void
  58. {
  59. if ($this->batch()->cancelled()) {
  60. return;
  61. }
  62. $pid = $this->pid;
  63. $domain = $this->domain;
  64. $query = 'SELECT notifications.*
  65. FROM profiles
  66. JOIN notifications on profiles.id = notifications.actor_id
  67. WHERE notifications.profile_id = ?
  68. AND profiles.domain = ?';
  69. $params = [$pid, $domain];
  70. foreach(DB::cursor($query, $params) as $n) {
  71. if(!$n || !$n->id) {
  72. continue;
  73. }
  74. Notification::where('id', $n->id)->delete();
  75. NotificationService::del($pid, $n->id);
  76. }
  77. }
  78. }