1
0
Fork 0
pixelfed/app/Jobs/AvatarPipeline/CreateAvatar.php

90 lines
2.1 KiB
PHP
Raw Normal View History

2018-05-22 23:57:52 +00:00
<?php
namespace App\Jobs\AvatarPipeline;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
2018-05-22 23:57:52 +00:00
use Illuminate\Foundation\Bus\Dispatchable;
2018-08-28 03:07:36 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use App\Avatar;
use App\Profile;
2018-05-22 23:57:52 +00:00
class CreateAvatar implements ShouldQueue, ShouldBeUniqueUntilProcessing
2018-05-22 23:57:52 +00:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $profile;
public $tries = 3;
public $maxExceptions = 3;
public $timeout = 900;
public $failOnTimeout = true;
2018-08-28 03:07:36 +00:00
2019-01-12 22:24:20 +00:00
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
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()];
}
2019-01-12 22:24:20 +00:00
2018-05-22 23:57:52 +00:00
/**
* Create a new job instance.
*
* @return void
*/
2018-05-31 22:04:15 +00:00
public function __construct(Profile $profile)
2018-05-22 23:57:52 +00:00
{
$this->profile = $profile->withoutRelations();
2018-05-22 23:57:52 +00:00
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2018-05-31 22:04:15 +00:00
$profile = $this->profile;
$isRemote = (bool) $profile->private_key == null;
2020-12-15 06:54:22 +00:00
$path = 'public/avatars/default.jpg';
Avatar::updateOrCreate(
[
'profile_id' => $profile->id,
],
[
'media_path' => $path,
'change_count' => 0,
'is_remote' => $isRemote,
'last_processed_at' => now()
]
);
2018-05-22 23:57:52 +00:00
}
}