AvatarStorageCleanup.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Jobs\AvatarPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  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\AvatarService;
  11. use App\Avatar;
  12. class AvatarStorageCleanup implements ShouldQueue, ShouldBeUniqueUntilProcessing
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. public $avatar;
  16. public $tries = 3;
  17. public $maxExceptions = 3;
  18. public $timeout = 900;
  19. public $failOnTimeout = true;
  20. /**
  21. * The number of seconds after which the job's unique lock will be released.
  22. *
  23. * @var int
  24. */
  25. public $uniqueFor = 3600;
  26. /**
  27. * Get the unique ID for the job.
  28. */
  29. public function uniqueId(): string
  30. {
  31. return 'avatar:storage:cleanup:' . $this->avatar->profile_id;
  32. }
  33. /**
  34. * Get the middleware the job should pass through.
  35. *
  36. * @return array<int, object>
  37. */
  38. public function middleware(): array
  39. {
  40. return [(new WithoutOverlapping("avatar-storage-cleanup:{$this->avatar->profile_id}"))->shared()->dontRelease()];
  41. }
  42. /**
  43. * Create a new job instance.
  44. */
  45. public function __construct(Avatar $avatar)
  46. {
  47. $this->avatar = $avatar->withoutRelations();
  48. }
  49. /**
  50. * Execute the job.
  51. */
  52. public function handle(): void
  53. {
  54. AvatarService::cleanup($this->avatar, true);
  55. return;
  56. }
  57. }