clrghouz/app/Jobs/TicProcess.php

105 lines
2.7 KiB
PHP
Raw Normal View History

2022-11-01 11:24:36 +00:00
<?php
namespace App\Jobs;
use Carbon\Carbon;
2022-11-01 11:24:36 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\UnableToMoveFile;
2022-11-01 11:24:36 +00:00
use App\Classes\FTN\Tic;
use App\Models\Domain;
2022-11-01 11:24:36 +00:00
class TicProcess implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private const LOGKEY = 'JTP';
private ?Domain $do;
2023-08-15 02:25:38 +00:00
public const QUEUE = 'tic';
2022-11-01 11:24:36 +00:00
/**
* Create a new job instance.
*
* @param string $file Received name, stored in Storage::disk('local'), ie: storage/app
* @param string|null $domain
2022-11-01 11:24:36 +00:00
*/
public function __construct(private string $file,private ?string $domain=NULL)
2022-11-01 11:24:36 +00:00
{
$this->do = $domain ? Domain::where('name',$domain)->singleOrFail() : NULL;
2023-08-15 02:25:38 +00:00
$this->onQueue(self::QUEUE);
2022-11-01 11:24:36 +00:00
}
public function __get($key): mixed
{
switch ($key) {
case 'jobname':
return sprintf('%s %s',$this->do?->name,$this->file);
default:
return NULL;
}
}
2022-11-01 11:24:36 +00:00
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$fs = Storage::disk(config('fido.local_disk'));
$rel_name = sprintf('%s/%s',config('fido.dir'),$this->file);
$to = new Tic;
try {
$fo = $to->load($this->file);
} catch (\Exception $e) {
Log::error(sprintf('%s:! Error loading TIC file [%s] (%s)',self::LOGKEY,$rel_name,$e->getMessage()));
return;
}
$fo->save();
Log::info(sprintf('%s:= Processed [%s] storing [%s] as id [%d]',self::LOGKEY,$this->file,$to->file->name,$to->file->id));
if (config('fido.packet_keep')) {
$dir = sprintf('%s/%s/%s/%s',config('fido.dir'),($x=Carbon::now())->format('Y'),$x->format('m'),$x->format('d'));
Log::debug(sprintf('%s:- Moving processed TIC [%s] to [%s]',self::LOGKEY,$rel_name,$dir));
try {
if ($fs->makeDirectory($dir)) {
$fs->move($rel_name,$x=sprintf('%s/%s',$dir,$this->file));
Log::info(sprintf('%s:- Moved processed TIC [%s] to [%s]',self::LOGKEY,$rel_name,$x));
} else
Log::error(sprintf('%s:! Unable to create dir [%s]',self::LOGKEY,$dir));
} catch (UnableToMoveFile $e) {
Log::error(sprintf('%s:! Unable to move TIC [%s] to [%s] (%s)',self::LOGKEY,$rel_name,$dir,$e->getMessage()));
} catch (\Exception $e) {
Log::error(sprintf('%s:! Failed moving TIC [%s] to [%s] (%s)',self::LOGKEY,$rel_name,$dir,$e->getMessage()));
}
} else {
if (! $fs->delete($rel_name))
Log::alert(sprintf('%s:! Failed to delete [%s]',self::LOGKEY,$rel_name));
}
if ($to->isNodelist())
NodelistImport::dispatch($fo->withoutRelations(),$this->domain);
2022-11-01 11:24:36 +00:00
}
}