clrghouz/app/Jobs/NodesNew.php

69 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2024-11-04 01:44:46 +00:00
<?php
namespace App\Jobs;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
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\Notification;
2024-12-08 07:45:29 +00:00
use App\Models\{Address,Domain};
2024-12-08 10:38:10 +00:00
use App\Notifications\Echomails\NodesNew as NodesNewEchomail;
use App\Notifications\Netmails\NodesNew as NodesNewNetmail;
2024-11-04 01:44:46 +00:00
class NodesNew implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private const LOGKEY = 'JNN';
private ?Carbon $since; // New nodes since this date
2024-12-08 10:38:10 +00:00
private ?Address $ao; // Domain we are processing
2024-11-04 01:44:46 +00:00
private Domain $do; // Domain we are processing
/**
* Create a new job instance.
*/
public function __construct(Domain $do,Carbon $since=NULL,Address $ao=NULL)
{
$this->do = $do->withoutRelations();
$this->ao = $ao?->withoutRelations();
$this->since = $since;
}
/**
* Execute the job.
*/
public function handle(): void
{
2024-12-08 10:38:10 +00:00
$since = ($this->since ?: Carbon::parse('last monday'))->startOfDay();
2024-11-04 01:44:46 +00:00
$result = Address::FTN()
->ActiveFTN()
2024-12-08 07:45:29 +00:00
->addSelect('addresses.created_at')
2024-11-04 01:44:46 +00:00
->join('systems',['systems.id'=>'addresses.system_id'])
->join('system_zone',['system_zone.system_id'=>'systems.id','system_zone.zone_id'=>'zones.id'])
->whereIn('zones.id',$this->do->zones->pluck('id'))
->where('systems.active',TRUE)
2024-12-08 10:38:10 +00:00
->where('addresses.created_at','>=',$since)
2024-12-08 07:45:29 +00:00
->orderBy('addresses.created_at')
2024-11-04 01:44:46 +00:00
->get();
if ($result->count()) {
Log::notice(sprintf('%s:- Sending new nodes since [%s] (%d)',self::LOGKEY,$since,$result->count()));
2024-12-08 10:38:10 +00:00
if ($this->ao)
Notification::route('netmail',$this->ao->withoutRelations())
->notify(new NodesNewNetmail($since,$result));
else
Notification::route('echomail',$this->do->nodestatus_echoarea)
->notify(new NodesNewEchomail($since,$result));
2024-11-04 01:44:46 +00:00
} else
Log::notice(sprintf('%s:- No nodes since [%s]',self::LOGKEY,$since));
}
}