Update CreateAvatar job, add processing constraints and set is_remote attribute

This commit is contained in:
Daniel Supernault 2023-10-11 01:32:04 -06:00
parent 95a1eddcb2
commit 319ced4054
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -2,19 +2,25 @@
namespace App\Jobs\AvatarPipeline; namespace App\Jobs\AvatarPipeline;
use App\Avatar;
use App\Profile;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use App\Avatar;
use App\Profile;
class CreateAvatar implements ShouldQueue class CreateAvatar implements ShouldQueue, ShouldBeUniqueUntilProcessing
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $profile; public $profile;
public $tries = 3;
public $maxExceptions = 3;
public $timeout = 900;
public $failOnTimeout = true;
/** /**
* Delete the job if its models no longer exist. * Delete the job if its models no longer exist.
@ -22,6 +28,31 @@ class CreateAvatar implements ShouldQueue
* @var bool * @var bool
*/ */
public $deleteWhenMissingModels = true; public $deleteWhenMissingModels = true;
/**
* The number of seconds after which the job's unique lock will be released.
*
* @var int
*/
public $uniqueFor = 3600;
/**
* Get the unique ID for the job.
*/
public function uniqueId(): string
{
return 'avatar:create:' . $this->profile->id;
}
/**
* Get the middleware the job should pass through.
*
* @return array<int, object>
*/
public function middleware(): array
{
return [(new WithoutOverlapping("avatar-create:{$this->profile->id}"))->shared()->dontRelease()];
}
/** /**
* Create a new job instance. * Create a new job instance.
@ -30,7 +61,7 @@ class CreateAvatar implements ShouldQueue
*/ */
public function __construct(Profile $profile) public function __construct(Profile $profile)
{ {
$this->profile = $profile; $this->profile = $profile->withoutRelations();
} }
/** /**
@ -41,12 +72,18 @@ class CreateAvatar implements ShouldQueue
public function handle() public function handle()
{ {
$profile = $this->profile; $profile = $this->profile;
$isRemote = (bool) $profile->private_key == null;
$path = 'public/avatars/default.jpg'; $path = 'public/avatars/default.jpg';
$avatar = new Avatar(); Avatar::updateOrCreate(
$avatar->profile_id = $profile->id; [
$avatar->media_path = $path; 'profile_id' => $profile->id,
$avatar->change_count = 0; ],
$avatar->last_processed_at = \Carbon\Carbon::now(); [
$avatar->save(); 'media_path' => $path,
'change_count' => 0,
'is_remote' => $isRemote,
'last_processed_at' => now()
]
);
} }
} }