CreateAvatar.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\Avatar;
  11. use App\Profile;
  12. class CreateAvatar implements ShouldQueue, ShouldBeUniqueUntilProcessing
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. public $profile;
  16. public $tries = 3;
  17. public $maxExceptions = 3;
  18. public $timeout = 900;
  19. public $failOnTimeout = true;
  20. /**
  21. * Delete the job if its models no longer exist.
  22. *
  23. * @var bool
  24. */
  25. public $deleteWhenMissingModels = true;
  26. /**
  27. * The number of seconds after which the job's unique lock will be released.
  28. *
  29. * @var int
  30. */
  31. public $uniqueFor = 3600;
  32. /**
  33. * Get the unique ID for the job.
  34. */
  35. public function uniqueId(): string
  36. {
  37. return 'avatar:create:' . $this->profile->id;
  38. }
  39. /**
  40. * Get the middleware the job should pass through.
  41. *
  42. * @return array<int, object>
  43. */
  44. public function middleware(): array
  45. {
  46. return [(new WithoutOverlapping("avatar-create:{$this->profile->id}"))->shared()->dontRelease()];
  47. }
  48. /**
  49. * Create a new job instance.
  50. *
  51. * @return void
  52. */
  53. public function __construct(Profile $profile)
  54. {
  55. $this->profile = $profile->withoutRelations();
  56. }
  57. /**
  58. * Execute the job.
  59. *
  60. * @return void
  61. */
  62. public function handle()
  63. {
  64. $profile = $this->profile;
  65. $isRemote = (bool) $profile->private_key == null;
  66. $path = 'public/avatars/default.jpg';
  67. Avatar::updateOrCreate(
  68. [
  69. 'profile_id' => $profile->id,
  70. ],
  71. [
  72. 'media_path' => $path,
  73. 'change_count' => 0,
  74. 'is_remote' => $isRemote,
  75. 'last_processed_at' => now()
  76. ]
  77. );
  78. }
  79. }