2024-09-02 12:51:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
use App\Models\Abstracted\Catalog;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
class CatalogMove implements ShouldQueue,ShouldBeUnique
|
|
|
|
{
|
|
|
|
use InteractsWithQueue,Queueable,SerializesModels;
|
|
|
|
|
|
|
|
// Our object
|
|
|
|
private Catalog $o;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Catalog $o) {
|
|
|
|
$this->o = $o->withoutRelations();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function middleware(): array
|
|
|
|
{
|
2024-10-01 01:48:17 +00:00
|
|
|
return [new WithoutOverlapping($this->uniqueId())];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the unique ID for the job.
|
|
|
|
*/
|
|
|
|
public function uniqueId(): string
|
|
|
|
{
|
|
|
|
return $this->o::config.'|'.$this->o->id;
|
2024-09-02 12:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2024-10-04 02:49:09 +00:00
|
|
|
public function handle(): void
|
2024-09-02 12:51:19 +00:00
|
|
|
{
|
2024-09-18 03:30:49 +00:00
|
|
|
$from = $this->o->file_name_rel();
|
2024-09-02 12:51:19 +00:00
|
|
|
$to = $this->o->file_name_rel(FALSE);
|
|
|
|
|
|
|
|
Log::info(sprintf('%s: Moving [%s|%s] from [%s] to [%s]',
|
|
|
|
__METHOD__,
|
2024-09-18 03:30:49 +00:00
|
|
|
$this->o::config,
|
2024-09-02 12:51:19 +00:00
|
|
|
$this->o->id,
|
|
|
|
$from,
|
|
|
|
$to,
|
|
|
|
));
|
|
|
|
|
|
|
|
// If the move is successful, commit the transaction
|
|
|
|
if ($this->o->isMoveable()) {
|
|
|
|
// If our move fails, we'll abort the update
|
|
|
|
DB::beginTransaction();
|
|
|
|
$this->o->filename = $this->o->file_name();
|
|
|
|
$this->o->save();
|
|
|
|
|
|
|
|
if (Storage::disk($this->o::fs)->move($from,$to))
|
|
|
|
DB::commit();
|
|
|
|
else
|
|
|
|
Log::error(sprintf('%s: Move failed for file [%s]',__METHOD__,$from));
|
|
|
|
|
|
|
|
} else
|
|
|
|
Log::alert(sprintf('%s: Unable to move file [%s] with reason [%s]',
|
|
|
|
__METHOD__,
|
|
|
|
$from,
|
|
|
|
$this->o->isMoveableReason(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|