Enable specifying a domain for nodelist import, and throwing exceptions when errors occur

This commit is contained in:
Deon George 2022-11-05 10:35:41 +11:00
parent c7187dbb0e
commit 531654724a
2 changed files with 13 additions and 8 deletions

View File

@ -16,6 +16,7 @@ class NodelistImport extends Command
*/
protected $signature = 'nodelist:import'
.' {file : File ID}'
.' {domain? : Domain Name}'
.' {--D|delete : Delete old data for the date}'
.' {--U|unlink : Delete file after import}';
@ -33,6 +34,6 @@ class NodelistImport extends Command
*/
public function handle()
{
return Job::dispatchSync(File::findOrFail($this->argument('file')),$this->option('delete'),$this->option('unlink'));
return Job::dispatchSync(File::findOrFail($this->argument('file')),$this->argument('domain'),$this->option('delete'),$this->option('unlink'));
}
}

View File

@ -8,6 +8,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
@ -24,6 +25,7 @@ class NodelistImport implements ShouldQueue
private const importkey = 'nodelist';
private File $file;
private ?string $domain;
private bool $delete_file;
private bool $delete_recs;
@ -31,12 +33,14 @@ class NodelistImport implements ShouldQueue
* Import Nodelist constructor.
*
* @param File $file
* @param string|null $domain
* @param bool $delete_recs
* @param bool $delete_file
*/
public function __construct(File $file,bool $delete_recs=FALSE,bool $delete_file=TRUE)
public function __construct(File $file,string $domain=NULL,bool $delete_recs=FALSE,bool $delete_file=TRUE)
{
$this->file = $file;
$this->domain = $domain;
$this->delete_file = $delete_file;
$this->delete_recs = $delete_recs;
}
@ -65,23 +69,23 @@ class NodelistImport implements ShouldQueue
$matches = [];
if ((! preg_match('/^;A\ /',$line)) || (! preg_match('/^;A\ (.*)\ Nodelist for ([MTWFS][a-z]+,\ [JFMASOND][a-z]+\ [0-9]{1,2},\ [0-9]{4})\ --\ Day\ number\ ([0-9]+)\ :\ ([0-9a-f]+)$/',$line,$matches))) {
Log::error(sprintf('Nodelist file [%d] is not valid?',$this->file->id),['m'=>$matches,'l'=>$line]);
throw new \Exception('Invalid nodelist for file '.$this->file->id);
Log::error(sprintf('%s:Nodelist file [%d] is not valid?',self::LOGKEY,$this->file->id),['m'=>$matches,'l'=>$line]);
throw new \Exception('Invalid nodelist for file: '.$this->file->id);
}
$file_crc = $matches[4];
$do = Domain::where('name',strtolower($matches[1]))->single();
$do = Domain::where('name',strtolower($matches[1] ?: $this->domain))->single();
if (! $do) {
Log::error(sprintf('%s:! Domain not found [%s].',static::LOGKEY,strtolower($matches[1])));
return;
Log::error(sprintf('%s:! Domain not found [%s].',static::LOGKEY,strtolower($matches[1] ?: $this->domain)));
throw new \Exception('Nodelist Domain not found: '.$this->file->id);
}
$date = Carbon::createFromFormat('D, M d, Y H:i',$matches[2].'0:00');
if ($date->dayOfYear != $matches[3]) {
Log::error(sprintf('%s:! Nodelist date doesnt match [%d] (%d:%s).',static::LOGKEY,$matches[3],$date->dayOfYear,$date->format('Y-m-d')));
return;
throw new \Exception('Nodelist date doesnt match for file: '.$this->file->id);
}
Log::debug(sprintf('%s:Importing nodelist for [%s] dated [%s].',static::LOGKEY,$do->name,$date->format('Y-m-d')));