AvatarStorageLargePurge.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. use Illuminate\Support\Str;
  13. class AvatarStorageLargePurge implements ShouldQueue, ShouldBeUniqueUntilProcessing
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. public $avatar;
  17. public $tries = 3;
  18. public $maxExceptions = 3;
  19. public $timeout = 900;
  20. public $failOnTimeout = true;
  21. /**
  22. * The number of seconds after which the job's unique lock will be released.
  23. *
  24. * @var int
  25. */
  26. public $uniqueFor = 3600;
  27. /**
  28. * Get the unique ID for the job.
  29. */
  30. public function uniqueId(): string
  31. {
  32. return 'avatar:storage:lg-purge:' . $this->avatar->profile_id;
  33. }
  34. /**
  35. * Get the middleware the job should pass through.
  36. *
  37. * @return array<int, object>
  38. */
  39. public function middleware(): array
  40. {
  41. return [(new WithoutOverlapping("avatar-storage-purge:{$this->avatar->profile_id}"))->shared()->dontRelease()];
  42. }
  43. /**
  44. * Create a new job instance.
  45. */
  46. public function __construct(Avatar $avatar)
  47. {
  48. $this->avatar = $avatar->withoutRelations();
  49. }
  50. /**
  51. * Execute the job.
  52. */
  53. public function handle(): void
  54. {
  55. $avatar = $this->avatar;
  56. $disk = AvatarService::disk();
  57. $files = collect(AvatarService::storage($avatar));
  58. $curFile = Str::of($avatar->cdn_url)->explode('/')->last();
  59. $files = $files->filter(function($f) use($curFile) {
  60. return !$curFile || !str_ends_with($f, $curFile);
  61. })->each(function($name) use($disk) {
  62. $disk->delete($name);
  63. });
  64. return;
  65. }
  66. }